Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProxyAdded proxy option + proxy sanity checks #277

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ optional arguments:
"[email protected]: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
Expand Down
67 changes: 49 additions & 18 deletions safaribooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 self.args.proxies:
self.session.proxies = self.args.proxies
# self.session.verify = False

self.session.headers.update(self.HEADERS)

Expand Down Expand Up @@ -1044,8 +1040,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.",
Expand All @@ -1062,7 +1057,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."
Expand All @@ -1082,14 +1080,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: ")
Expand All @@ -1100,15 +1106,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")

SafariBooks(args_parsed)
if parsed_args.proxy:
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:
result = match.group()
parsed_args.proxies = {
"http": result,
"https": result
}
else:
arguments.error(f"Incorrect proxy format (should match the regex: `{proxy_regex}`)")
return parsed_args


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()