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

Fixed OSError: [Errno 22] Invalid argument error for book ID #276

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions safaribooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,40 @@ 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 len(args_parsed.bookid) > 0:
book_url_regex = r"['\"]*http[s]?:\/\/[a-zA-Z0-9.\-/]+\/(\d{10,15})\/*['\"]*" # Matches book URL
pattern = re.compile(book_url_regex)
matchURL = re.search(pattern, args_parsed.bookid)

book_id_regex = r"['\"]*(\d{10,15})/*['\"]*" # Matches book ID
pattern = re.compile(book_id_regex)
matchID = re.search(pattern, args_parsed.bookid)

if matchURL:
bookID = matchURL.group(1)
elif matchID:
bookID = matchID.group(1)
else:
bookID = None
arguments.error("Invalid book ID or URL")
if str.isdecimal(bookID):
args_parsed.bookid = bookID
else:
arguments.error("Invalid book ID")
else:
arguments.error("Book ID must not be empty")


if len(args_parsed.bookid) > 0:
bookID = args_parsed.bookid.split("/")[-1] # Only get book ID from URL
holzkohlengrill marked this conversation as resolved.
Show resolved Hide resolved
if str.isdecimal(bookID):
args_parsed.bookid = bookID
else:
arguments.error("Invalid book ID")
else:
arguments.error("Book ID must not be empty")


SafariBooks(args_parsed)
# Hint: do you want to download more then one book once, initialized more than one instance of `SafariBooks`...
sys.exit(0)