From f370662043ddb0663a96082a48fa3daba6aa10d3 Mon Sep 17 00:00:00 2001 From: chau vinh tuan Date: Fri, 2 Aug 2019 15:53:08 +0700 Subject: [PATCH 1/3] Add image max size and image quality arguments --- safaribooks.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/safaribooks.py b/safaribooks.py index d66730b..ce63e92 100644 --- a/safaribooks.py +++ b/safaribooks.py @@ -1007,6 +1007,14 @@ def create_epub(self): "--no-cookies", dest="no_cookies", action='store_true', help="Prevent your session data to be saved into `cookies.json` file." ) + arguments.add_argument( + "--image-max-size", metavar="", dest="image_max_size", default=False, + help="Resize image if the image width/height is large than max size. 0 (default) for no resize" + ) + arguments.add_argument( + "--image-quality", metavar="", dest="image_quality", default=False, + help="Compress JPEG images with given quality. 0 (default) for keeping the original image's quality" + ) arguments.add_argument( "--no-kindle", dest="no_kindle", action='store_true', help="Remove some CSS rules that block overflow on `table` and `pre` elements." @@ -1035,5 +1043,19 @@ def create_epub(self): else: if args_parsed.no_cookies: arguments.error("invalid option: `--no-cookies` is valid only if you use the `--cred` option") - + + if args_parsed.image_max_size: + if not args_parsed.image_max_size.isdigit(): + arguments.error("invalid size: %s" % args_parsed.image_max_size) + args_parsed.image_max_size = int(args_parsed.image_max_size) + else: + args_parsed.image_max_size = 0 + + if args_parsed.image_quality: + if not args_parsed.image_quality.isdigit(): + arguments.error("invalid quality: %s" % args_parsed.image_quality) + args_parsed.image_quality = int(args_parsed.image_quality) + else: + args_parsed.image_quality = 0 + SafariBooks(args_parsed) From 9a2ebb247d3375290ab12d2dec0e9d50b09d3a2f Mon Sep 17 00:00:00 2001 From: chau vinh tuan Date: Fri, 2 Aug 2019 15:53:54 +0700 Subject: [PATCH 2/3] Resize image by given arguments --- requirements.txt | 1 + safaribooks.py | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index 2666c38..2740e3b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +Pillow lxml>=4.1.1 requests>=2.20.0 diff --git a/safaribooks.py b/safaribooks.py index ce63e92..8ff9102 100644 --- a/safaribooks.py +++ b/safaribooks.py @@ -13,6 +13,7 @@ from random import random from multiprocessing import Process, Queue, Value from urllib.parse import urljoin, urlsplit, urlparse +from PIL import Image PATH = os.path.dirname(os.path.realpath(__file__)) @@ -819,14 +820,32 @@ def _thread_download_images(self, url): stream=True) if response == 0: self.display.error("Error trying to retrieve this image: %s\n From: %s" % (image_name, url)) - - with open(image_path, 'wb') as img: + + with open(image_path, 'wb') as image_file: for chunk in response.iter_content(1024): - img.write(chunk) - + image_file.write(chunk) + + self._resize_image(image_path) + self.images_done_queue.put(1) self.display.state(len(self.images), self.images_done_queue.qsize()) - + + def _resize_image(self, image_path): + image_max_size = self.args.image_max_size + image_quality = self.args.image_quality + if image_max_size == 0 or image_quality == 0: + # No resize or changing quality + return + image = Image.open(image_path) + + if image_max_size > 0: + # Resize image if it's bigger than (image_max_size x image_max_size) + image.thumbnail((image_max_size, image_max_size)) + if image_quality > 0: + image.save(image_path, quality=image_quality) + else: + image.save(image_path) + def _start_multiprocessing(self, operation, full_queue): if len(full_queue) > 5: for i in range(0, len(full_queue), 5): From 41c4d9a491aa1bfce457d95317786a0e4d4ce74f Mon Sep 17 00:00:00 2001 From: chau vinh tuan Date: Fri, 2 Aug 2019 17:18:12 +0700 Subject: [PATCH 3/3] Catch exception if any --- safaribooks.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/safaribooks.py b/safaribooks.py index 8ff9102..022b053 100644 --- a/safaribooks.py +++ b/safaribooks.py @@ -824,8 +824,11 @@ def _thread_download_images(self, url): with open(image_path, 'wb') as image_file: for chunk in response.iter_content(1024): image_file.write(chunk) - - self._resize_image(image_path) + try: + self._resize_image(image_path) + except: + # There are some image file which cannot open. Should we delete it? + pass self.images_done_queue.put(1) self.display.state(len(self.images), self.images_done_queue.qsize())