From 688328e92d62085ce5110ca8a08d112f15cb35b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szeremeta?= Date: Tue, 6 Dec 2016 19:56:46 +0100 Subject: [PATCH 1/4] Download ebooks into separate directories If path.separate is set to true in the configuration file, downloads books and extras into separate directory. Examples: ebooks/TITLE - AUTHOR/TITLE.pdf ... ebooks/TITLE - AUTHOR/extras/TITLE.png Note that if you use separate directories path.extras behaves differently. The code is only a suggestion (preview) of new functionality, but works properly with local download. Other features to check (possible complications with Google Drive upload). Instead of setting this in the configuration file, separate or not directory can be changed by additional option from the command line. --- config/dev.cfg | 3 ++- config/prod_example.cfg | 3 ++- script/packtpub.py | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/config/dev.cfg b/config/dev.cfg index 67c691c..abeafb7 100644 --- a/config/dev.cfg +++ b/config/dev.cfg @@ -16,7 +16,8 @@ credential.password=testPwd [path] path.ebooks=ebooks -path.extras=ebooks/extras +path.extras=extras +path.separate=true [drive] drive.oauth2_scope=https://www.googleapis.com/auth/drive diff --git a/config/prod_example.cfg b/config/prod_example.cfg index e2d4f47..c909a1f 100644 --- a/config/prod_example.cfg +++ b/config/prod_example.cfg @@ -14,7 +14,8 @@ credential.password=PACKTPUB_PASSWORD [path] path.ebooks=ebooks -path.extras=ebooks/extras +path.extras=extras +path.separate=true [drive] drive.oauth2_scope=https://www.googleapis.com/auth/drive diff --git a/script/packtpub.py b/script/packtpub.py index 9963a3c..7cc0287 100644 --- a/script/packtpub.py +++ b/script/packtpub.py @@ -124,6 +124,10 @@ def download_ebooks(self, types): for type in types] directory = self.__config.get('path', 'path.ebooks') + + if self.__config.get('path', 'path.separate') == "true": + directory = directory + '/' + self.info['title'] + ' - ' + self.info['author'] + for download in downloads_info: self.info['paths'].append( download_file(self.__session, download['url'], directory, download['filename'], self.__headers)) @@ -134,6 +138,9 @@ def download_extras(self): directory = self.__config.get('path', 'path.extras') + if self.__config.get('path', 'path.separate') == "true": + directory = self.__config.get('path', 'path.ebooks') + '/' + self.info['title'] + ' - ' + self.info['author'] + '/' + directory + url_image = self.info['url_image'] filename = self.info['filename'] + '_' + split(url_image)[1] self.info['paths'].append(download_file(self.__session, url_image, directory, filename, self.__headers)) From 5015a87dfe54a4d13e3a604286457d965a93e6d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szeremeta?= Date: Tue, 6 Dec 2016 23:10:45 +0100 Subject: [PATCH 2/4] path.separate improvements --- config/prod_example.cfg | 5 +++-- script/packtpub.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/config/prod_example.cfg b/config/prod_example.cfg index c909a1f..2f96335 100644 --- a/config/prod_example.cfg +++ b/config/prod_example.cfg @@ -14,8 +14,9 @@ credential.password=PACKTPUB_PASSWORD [path] path.ebooks=ebooks -path.extras=extras -path.separate=true +path.extras=ebooks/extras +#path.separate.ebooks=MY/EBOOKS/FOLDER +#path.separate.extras=MY/EXTRAS/FOLDER [drive] drive.oauth2_scope=https://www.googleapis.com/auth/drive diff --git a/script/packtpub.py b/script/packtpub.py index 7cc0287..55f1cf2 100644 --- a/script/packtpub.py +++ b/script/packtpub.py @@ -123,10 +123,10 @@ def download_ebooks(self, types): filename=self.info['filename'] + '.' + type) for type in types] - directory = self.__config.get('path', 'path.ebooks') - - if self.__config.get('path', 'path.separate') == "true": - directory = directory + '/' + self.info['title'] + ' - ' + self.info['author'] + if self.__config.has_option('path', 'path.separate.extras'): + directory = self.__config.get('path', 'path.separate.ebooks') + '/' + self.info['title'] + ' - ' + self.info['author'] + else: + directory = self.__config.get('path', 'path.ebooks') for download in downloads_info: self.info['paths'].append( @@ -136,10 +136,10 @@ def download_extras(self): """ """ - directory = self.__config.get('path', 'path.extras') - - if self.__config.get('path', 'path.separate') == "true": - directory = self.__config.get('path', 'path.ebooks') + '/' + self.info['title'] + ' - ' + self.info['author'] + '/' + directory + if self.__config.has_option('path', 'path.separate.extras'): + directory = self.__config.get('path', 'path.separate.ebooks') + '/' + self.info['title'] + ' - ' + self.info['author'] + '/' + self.__config.get('path', 'path.separate.extras') + else: + directory = self.__config.get('path', 'path.extras') url_image = self.info['url_image'] filename = self.info['filename'] + '_' + split(url_image)[1] From 18754ede53e13dcb6d740dfd21475b99543b2d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szeremeta?= Date: Tue, 6 Dec 2016 23:37:26 +0100 Subject: [PATCH 3/4] Update dev.cfg --- config/dev.cfg | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/dev.cfg b/config/dev.cfg index abeafb7..2e6735a 100644 --- a/config/dev.cfg +++ b/config/dev.cfg @@ -16,8 +16,9 @@ credential.password=testPwd [path] path.ebooks=ebooks -path.extras=extras -path.separate=true +path.extras=ebooks/extras +#path.separate.ebooks=MY/EBOOKS/FOLDER +#path.separate.extras=MY/EXTRAS/FOLDER [drive] drive.oauth2_scope=https://www.googleapis.com/auth/drive From d0aae838cc46c5a376a8a18a6502753792a1991d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Szeremeta?= Date: Wed, 7 Dec 2016 00:17:18 +0100 Subject: [PATCH 4/4] path.separate.space & sanity (regex) for title and author --- config/prod_example.cfg | 1 + script/packtpub.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/config/prod_example.cfg b/config/prod_example.cfg index 2f96335..25c8eed 100644 --- a/config/prod_example.cfg +++ b/config/prod_example.cfg @@ -17,6 +17,7 @@ path.ebooks=ebooks path.extras=ebooks/extras #path.separate.ebooks=MY/EBOOKS/FOLDER #path.separate.extras=MY/EXTRAS/FOLDER +path.separate.space=_ [drive] drive.oauth2_scope=https://www.googleapis.com/auth/drive diff --git a/script/packtpub.py b/script/packtpub.py index 55f1cf2..74e8098 100644 --- a/script/packtpub.py +++ b/script/packtpub.py @@ -124,7 +124,12 @@ def download_ebooks(self, types): for type in types] if self.__config.has_option('path', 'path.separate.extras'): - directory = self.__config.get('path', 'path.separate.ebooks') + '/' + self.info['title'] + ' - ' + self.info['author'] + space = self.__config.get('path', 'path.separate.space') + + if space == '?': + space = ' ' + + directory = self.__config.get('path', 'path.separate.ebooks') + '/' + self.info['title'].encode('ascii', 'ignore').replace(' ', space) + space + '-' + space + self.info['author'].encode('ascii', 'ignore').replace(' ', space) else: directory = self.__config.get('path', 'path.ebooks') @@ -137,7 +142,12 @@ def download_extras(self): """ if self.__config.has_option('path', 'path.separate.extras'): - directory = self.__config.get('path', 'path.separate.ebooks') + '/' + self.info['title'] + ' - ' + self.info['author'] + '/' + self.__config.get('path', 'path.separate.extras') + space = self.__config.get('path', 'path.separate.space') + + if space == '?': + space = ' ' + + directory = self.__config.get('path', 'path.separate.ebooks') + '/' + self.info['title'].encode('ascii', 'ignore').replace(' ', space) + space + '-' + space + self.info['author'].encode('ascii', 'ignore').replace(' ', space) + '/' + self.__config.get('path', 'path.separate.extras') else: directory = self.__config.get('path', 'path.extras')