From 4a5658791c54454294cedde46c67ecf85530df1d Mon Sep 17 00:00:00 2001 From: gudata Date: Sat, 25 Sep 2021 14:17:31 +0300 Subject: [PATCH 1/5] make the images relative to the content rather than to the assets check 9780596800611 for example --- safaribooks.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/safaribooks.py b/safaribooks.py index a7f6fdb..2e1e0da 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -553,7 +553,7 @@ class SafariBooks: self.display.exit("API: unable to retrieve book chapters.") response = response.json() - + if not isinstance(response, dict) or len(response.keys()) == 1: self.display.exit(self.display.api_error(response)) @@ -814,7 +814,7 @@ class SafariBooks: # Images if "images" in next_chapter and len(next_chapter["images"]): - self.images.extend(urljoin(next_chapter['asset_base_url'], img_url) + self.images.extend(urljoin(next_chapter['content'], img_url) for img_url in next_chapter['images']) # Stylesheets @@ -863,6 +863,7 @@ class SafariBooks: self.css_done_queue.put(1) self.display.state(len(self.css), self.css_done_queue.qsize()) + def _thread_download_images(self, url): image_name = url.split("/")[-1] image_path = os.path.join(self.images_path, image_name) From 10b400a677928e2c4729a38c956089fd35f0d3e9 Mon Sep 17 00:00:00 2001 From: gudata Date: Sat, 25 Sep 2021 14:25:05 +0300 Subject: [PATCH 2/5] remove trailing spaces --- safaribooks.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/safaribooks.py b/safaribooks.py index 2e1e0da..9ef990f 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -138,20 +138,20 @@ class Display: def intro(self): output = self.SH_YELLOW + (""" - ____ ___ _ - / __/__ _/ _/__ _____(_) - _\ \/ _ `/ _/ _ `/ __/ / - /___/\_,_/_/ \_,_/_/ /_/ - / _ )___ ___ / /__ ___ - / _ / _ \/ _ \/ '_/(_-< - /____/\___/\___/_/\_\/___/ + ____ ___ _ + / __/__ _/ _/__ _____(_) + _\ \/ _ `/ _/ _ `/ __/ / + /___/\_,_/_/ \_,_/_/ /_/ + / _ )___ ___ / /__ ___ + / _ / _ \/ _ \/ '_/(_-< + /____/\___/\___/_/\_\/___/ """ if random() > 0.5 else """ - ██████╗ ██████╗ ██╗ ██╗ ██╗██████╗ + ██████╗ ██████╗ ██╗ ██╗ ██╗██████╗ ██╔═══██╗ ██╔══██╗██║ ╚██╗ ██╔╝╚════██╗ ██║ ██║ ██████╔╝██║ ╚████╔╝ ▄███╔╝ -██║ ██║ ██╔══██╗██║ ╚██╔╝ ▀▀══╝ -╚██████╔╝ ██║ ██║███████╗██║ ██╗ - ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ +██║ ██║ ██╔══██╗██║ ╚██╔╝ ▀▀══╝ +╚██████╔╝ ██║ ██║███████╗██║ ██╗ + ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ """) + self.SH_DEFAULT output += "\n" + "~" * (self.columns // 2) @@ -160,7 +160,7 @@ class Display: def parse_description(self, desc): if not desc: return "n/d" - + try: return html.fromstring(desc).text_content() @@ -540,7 +540,7 @@ class SafariBooks: if "last_chapter_read" in response: del response["last_chapter_read"] - + for key, value in response.items(): if value is None: response[key] = 'n/a' @@ -553,7 +553,7 @@ class SafariBooks: self.display.exit("API: unable to retrieve book chapters.") response = response.json() - + if not isinstance(response, dict) or len(response.keys()) == 1: self.display.exit(self.display.api_error(response)) From 2260ffc674658fb6eeeaea10ff7c66bcc2f8cddb Mon Sep 17 00:00:00 2001 From: gudata Date: Mon, 27 Sep 2021 13:32:38 +0300 Subject: [PATCH 3/5] FIX-IMAGE make it work with api v1 and v2 --- .gitignore | 1 + safaribooks.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 539ad1a..a21a47d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ Books/ cookies.json *.log *.txt +*.code-workspace diff --git a/safaribooks.py b/safaribooks.py index 9ef990f..81a14a9 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -812,10 +812,19 @@ class SafariBooks: self.chapter_title = next_chapter["title"] self.filename = next_chapter["filename"] - # Images + asset_base_url = next_chapter['asset_base_url'] + api_v2_detected = False + if 'v2' in next_chapter['content']: + asset_base_url = SAFARI_BASE_URL + "/api/v2/epubs/urn:orm:book:{}/files".format(self.book_id) + api_v2_detected = True + if "images" in next_chapter and len(next_chapter["images"]): - self.images.extend(urljoin(next_chapter['content'], img_url) - for img_url in next_chapter['images']) + for img_url in next_chapter['images']: + if api_v2_detected: + self.images.extend([asset_base_url + '/' + img_url]) + else: + self.images.extend(urljoin(next_chapter['asset_base_url'], img_url) + # Stylesheets self.chapter_stylesheets = [] From e8b8d61143891bc80ce9cb84627da288864c01aa Mon Sep 17 00:00:00 2001 From: gudata Date: Mon, 27 Sep 2021 13:43:25 +0300 Subject: [PATCH 4/5] add forgotten bracket --- safaribooks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/safaribooks.py b/safaribooks.py index 81a14a9..73a566d 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -823,7 +823,7 @@ class SafariBooks: if api_v2_detected: self.images.extend([asset_base_url + '/' + img_url]) else: - self.images.extend(urljoin(next_chapter['asset_base_url'], img_url) + self.images.extend(urljoin(next_chapter['asset_base_url'], img_url)) # Stylesheets From 9445253c4caf9f83da48645542c474cd4c1da634 Mon Sep 17 00:00:00 2001 From: gudata Date: Fri, 22 Oct 2021 10:10:33 +0300 Subject: [PATCH 5/5] switch to append while iterating over the images --- safaribooks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/safaribooks.py b/safaribooks.py index 73a566d..f3931ad 100755 --- a/safaribooks.py +++ b/safaribooks.py @@ -821,9 +821,9 @@ class SafariBooks: if "images" in next_chapter and len(next_chapter["images"]): for img_url in next_chapter['images']: if api_v2_detected: - self.images.extend([asset_base_url + '/' + img_url]) + self.images.append(asset_base_url + '/' + img_url) else: - self.images.extend(urljoin(next_chapter['asset_base_url'], img_url)) + self.images.append(urljoin(next_chapter['asset_base_url'], img_url)) # Stylesheets