From 9e0ce4656299d0b2184649b0e43a93bf9f3f7d5e Mon Sep 17 00:00:00 2001 From: Marcel Schmalzl Date: Fri, 16 Jul 2021 20:02:00 +0200 Subject: [PATCH 1/4] Fixed OSError: [Errno 22] Invalid argument error for book ID --- safaribooks.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/safaribooks.py b/safaribooks.py index c3382bb..50e2655 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -1109,6 +1109,16 @@ 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: + bookID = args_parsed.bookid.split("/")[-1] # Only get book ID from URL + 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) From 5d25561b07cf8843e3a1d463d577ee91ea9ed252 Mon Sep 17 00:00:00 2001 From: Marcel Schmalzl Date: Wed, 25 Aug 2021 18:25:26 +0200 Subject: [PATCH 2/4] Added regex for checking book URL --- safaribooks.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/safaribooks.py b/safaribooks.py index a7f6fdb..f7bc00f 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -1109,6 +1109,23 @@ 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: + bookid_regex = r"['\"]*http[s]?://[a-zA-Z0-9.\-/]+(\d{10,15})/*['\"]*" # Matches book URL + pattern = re.compile(bookid_regex) + match = re.search(pattern, args_parsed.bookid) + if match: + bookID = match.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") + + 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 323d47c72d660b3febdceed0152b9e19a13e308d Mon Sep 17 00:00:00 2001 From: Marcel Schmalzl Date: Thu, 7 Oct 2021 11:28:36 +0200 Subject: [PATCH 3/4] Added regex to match also for book ID --- safaribooks.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/safaribooks.py b/safaribooks.py index f7bc00f..6e00b67 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -1110,11 +1110,18 @@ def create_epub(self): arguments.error("invalid option: `--no-cookies` is valid only if you use the `--cred` option") if len(args_parsed.bookid) > 0: - bookid_regex = r"['\"]*http[s]?://[a-zA-Z0-9.\-/]+(\d{10,15})/*['\"]*" # Matches book URL - pattern = re.compile(bookid_regex) - match = re.search(pattern, args_parsed.bookid) - if match: - bookID = match.group(1) + 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") From 5b9b000fa41ac05711caf786438ee57592be67b2 Mon Sep 17 00:00:00 2001 From: Marcel Schmalzl Date: Thu, 7 Oct 2021 18:10:52 +0200 Subject: [PATCH 4/4] Fixed excaping in regex --- safaribooks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/safaribooks.py b/safaribooks.py index 6e00b67..2e6cd39 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -1110,7 +1110,7 @@ def create_epub(self): 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 + 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)