From 09bb795bd5352d6663d1a49115d6ef913fb43310 Mon Sep 17 00:00:00 2001 From: Marcel Schmalzl Date: Fri, 16 Jul 2021 20:04:51 +0200 Subject: [PATCH 1/4] Added proxy cmd line arg --- README.md | 1 + safaribooks.py | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c920eb9..094af65 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ optional arguments: "account_mail@mail.com:password01" `. --login Prompt for credentials used to perform the auth login on Safari Books Online. + --proxy PROXY Add proxy URL and port (e.g. `https://127.0.0.1:8080`) --no-cookies Prevent your session data to be saved into `cookies.json` file. --kindle Add some CSS rules that block overflow on `table` and diff --git a/safaribooks.py b/safaribooks.py index c3382bb..03bef02 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -31,10 +31,6 @@ API_ORIGIN_URL = "https://" + API_ORIGIN_HOST PROFILE_URL = SAFARI_BASE_URL + "/profile/" -# DEBUG -USE_PROXY = False -PROXIES = {"https": "https://127.0.0.1:8080"} - class Display: BASE_FORMAT = logging.Formatter( @@ -315,9 +311,9 @@ def __init__(self, args): self.display.intro() self.session = requests.Session() - if USE_PROXY: # DEBUG - self.session.proxies = PROXIES - self.session.verify = False + if args_parsed.proxies: + self.session.proxies = args_parsed.proxies + # self.session.verify = False self.session.headers.update(self.HEADERS) @@ -1062,7 +1058,10 @@ def create_epub(self): "--login", action='store_true', help="Prompt for credentials used to perform the auth login on Safari Books Online." ) - + arguments.add_argument( + "--proxy", + help="Add proxy URL and port (e.g. `https://127.0.0.1:8080`)" + ) arguments.add_argument( "--no-cookies", dest="no_cookies", action='store_true', help="Prevent your session data to be saved into `cookies.json` file." @@ -1109,6 +1108,20 @@ def create_epub(self): if args_parsed.no_cookies: arguments.error("invalid option: `--no-cookies` is valid only if you use the `--cred` option") + if args_parsed.proxy: + proxy_regex = r"http[s]?://[a-zA-Z0-9.-]+:\d{4}" # Matches proxy URL + pattern = re.compile(proxy_regex) + match = re.search(pattern, args_parsed.proxy) + if match: + result = match.group() + args_parsed.proxies = { + "http": result, + "https": result + } + else: + arguments.error(f"Incorrect proxy format (should match the regex: `{proxy_regex}`)") + + SafariBooks(args_parsed) # Hint: do you want to download more then one book once, initialized more than one instance of `SafariBooks`... sys.exit(0) From 5e2024b24f569f00b5c64701f3a7c248434c9421 Mon Sep 17 00:00:00 2001 From: Marcel Schmalzl Date: Wed, 25 Aug 2021 16:48:27 +0200 Subject: [PATCH 2/4] Removed global args; clarified programm flow --- safaribooks.py | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/safaribooks.py b/safaribooks.py index 03bef02..50281a6 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -309,10 +309,11 @@ def __init__(self, args): self.args = args self.display = Display("info_%s.log" % escape(args.bookid)) self.display.intro() + self.args.proxies = args.proxies self.session = requests.Session() - if args_parsed.proxies: - self.session.proxies = args_parsed.proxies + if self.args.proxies: + self.session.proxies = self.args.proxies # self.session.verify = False self.session.headers.update(self.HEADERS) @@ -1040,8 +1041,7 @@ def create_epub(self): os.rename(zip_file + ".zip", os.path.join(self.BOOK_PATH, self.book_id) + ".epub") -# MAIN -if __name__ == "__main__": +def parse_arguments(): arguments = argparse.ArgumentParser(prog="safaribooks.py", description="Download and generate an EPUB of your favorite books" " from Safari Books Online.", @@ -1081,14 +1081,22 @@ def create_epub(self): help="Book digits ID that you want to download. You can find it in the URL (X-es):" " `" + SAFARI_BASE_URL + "/library/view/book-name/XXXXXXXXXXXXX/`" ) + return arguments + - args_parsed = arguments.parse_args() - if args_parsed.cred or args_parsed.login: +def process_arguments(arguments): + """ + Process and check the arguments + :param arguments: arguments + :return: Parsed and processed arguements + """ + parsed_args = arguments.parse_args() + if parsed_args.cred or parsed_args.login: user_email = "" pre_cred = "" - if args_parsed.cred: - pre_cred = args_parsed.cred + if parsed_args.cred: + pre_cred = parsed_args.cred else: user_email = input("Email: ") @@ -1099,29 +1107,40 @@ def create_epub(self): if not parsed_cred: arguments.error("invalid credential: %s" % ( - args_parsed.cred if args_parsed.cred else (user_email + ":*******") + parsed_args.cred if parsed_args.cred else (user_email + ":*******") )) - args_parsed.cred = parsed_cred + parsed_args.cred = parsed_cred else: - if args_parsed.no_cookies: + if parsed_args.no_cookies: arguments.error("invalid option: `--no-cookies` is valid only if you use the `--cred` option") - if args_parsed.proxy: + if parsed_args.proxy: proxy_regex = r"http[s]?://[a-zA-Z0-9.-]+:\d{4}" # Matches proxy URL pattern = re.compile(proxy_regex) - match = re.search(pattern, args_parsed.proxy) + match = re.search(pattern, parsed_args.proxy) if match: result = match.group() - args_parsed.proxies = { + parsed_args.proxies = { "http": result, "https": result } else: arguments.error(f"Incorrect proxy format (should match the regex: `{proxy_regex}`)") + return parsed_args - SafariBooks(args_parsed) +def main(): + """ + Main safaribooks + """ + parsed_args = parse_arguments() + args = process_arguments(parsed_args) + SafariBooks(args) # Hint: do you want to download more then one book once, initialized more than one instance of `SafariBooks`... sys.exit(0) + + +if __name__ == "__main__": + main() From 307f076db4b00657a9b6ba9f599806c8e0c38dfd Mon Sep 17 00:00:00 2001 From: Marcel Schmalzl <12732886+holzkohlengrill@users.noreply.github.com> Date: Wed, 25 Aug 2021 17:34:26 +0200 Subject: [PATCH 3/4] Fixed regex --- safaribooks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/safaribooks.py b/safaribooks.py index 4079ce1..ebf7ba4 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -1117,7 +1117,7 @@ def process_arguments(arguments): arguments.error("invalid option: `--no-cookies` is valid only if you use the `--cred` option") if parsed_args.proxy: - proxy_regex = r"http[s]?://[a-zA-Z0-9.-]+:\d{4}" # Matches proxy URL + proxy_regex = r"http[s]?://[a-zA-Z0-9.\-]+:\d{4}" # Matches proxy URL pattern = re.compile(proxy_regex) match = re.search(pattern, parsed_args.proxy) if match: From 28d425ff081f3a74292b3ca4b804243d92445ac0 Mon Sep 17 00:00:00 2001 From: Marcel Schmalzl Date: Thu, 7 Oct 2021 17:07:26 +0200 Subject: [PATCH 4/4] Fixed proxy port regex + removed unneeded line --- safaribooks.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/safaribooks.py b/safaribooks.py index 4079ce1..738b332 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -309,7 +309,6 @@ def __init__(self, args): self.args = args self.display = Display("info_%s.log" % escape(args.bookid)) self.display.intro() - self.args.proxies = args.proxies self.session = requests.Session() if self.args.proxies: @@ -1117,7 +1116,7 @@ def process_arguments(arguments): arguments.error("invalid option: `--no-cookies` is valid only if you use the `--cred` option") if parsed_args.proxy: - proxy_regex = r"http[s]?://[a-zA-Z0-9.-]+:\d{4}" # Matches proxy URL + proxy_regex = r"http[s]?://[a-zA-Z0-9.-]+:\d{2,5}" # Matches proxy URL pattern = re.compile(proxy_regex) match = re.search(pattern, parsed_args.proxy) if match: