Skip to content

Commit

Permalink
Added proxy cmd line arg
Browse files Browse the repository at this point in the history
  • Loading branch information
holzkohlengrill committed Jul 16, 2021
1 parent c872f22 commit 09bb795
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
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
29 changes: 21 additions & 8 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 args_parsed.proxies:
self.session.proxies = args_parsed.proxies
# self.session.verify = False

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

Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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)

0 comments on commit 09bb795

Please sign in to comment.