From 147eec57cf0a32f393c01859b7316a9d45ec1994 Mon Sep 17 00:00:00 2001 From: Mentat Erasmus Date: Wed, 9 May 2018 15:42:12 -0300 Subject: [PATCH] fixing issue 16 and adding functionalities --- nhentai/cmdline.py | 6 ++++++ nhentai/command.py | 7 +++++-- nhentai/parser.py | 6 +++--- nhentai/utils.py | 31 ++++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index 7995e4f..ffff67c 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -56,6 +56,12 @@ def cmd_parser(): parser.add_option('--login', '-l', type='str', dest='login', action='store', help='username:password pair of nhentai account') + parser.add_option('--nohtml', dest='is_nohtml', action='store_true', + help='Don\'t generate HTML') + + parser.add_option('--cbz', dest='is_cbz', action='store_true', + help='Generate ComicBook CBZ File') + try: sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv)) except (NameError, TypeError): diff --git a/nhentai/command.py b/nhentai/command.py index 840b076..82a2fd6 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -10,7 +10,7 @@ from nhentai.doujinshi import Doujinshi from nhentai.downloader import Downloader from nhentai.logger import logger from nhentai.constant import BASE_URL -from nhentai.utils import generate_html +from nhentai.utils import generate_html, generate_cbz def main(): @@ -47,10 +47,13 @@ def main(): for doujinshi in doujinshi_list: doujinshi.downloader = downloader doujinshi.download() + if not options.is_nohtml and not options.is_cbz: generate_html(options.output_dir, doujinshi) + elif options.is_cbz: + generate_cbz(options.output_dir, doujinshi) if not platform.system() == 'Windows': - logger.log(15, '🍺 All done.') + logger.log(15, '? All done.') else: logger.log(15, 'All done.') diff --git a/nhentai/parser.py b/nhentai/parser.py index 6e9e117..7b5adf9 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -27,7 +27,7 @@ def login_parser(username, password): s.get(constant.LOGIN_URL) content = s.get(constant.LOGIN_URL).content - html = BeautifulSoup(content, 'html.parser') + html = BeautifulSoup(content, 'html.parser').encode("ascii") csrf_token_elem = html.find('input', attrs={'name': 'csrfmiddlewaretoken'}) if not csrf_token_elem: @@ -44,7 +44,7 @@ def login_parser(username, password): logger.error('Login failed, please check your username and password') exit(1) - html = BeautifulSoup(s.get(constant.FAV_URL).content, 'html.parser') + html = BeautifulSoup(s.get(constant.FAV_URL).content, 'html.parser').encode("ascii") count = html.find('span', attrs={'class': 'count'}) if not count: logger.error('Cannot get count of your favorites, maybe login failed.') @@ -95,7 +95,7 @@ def doujinshi_parser(id_): logger.critical(str(e)) exit(1) - doujinshi['name'] = response['title']['english'] + doujinshi['name'] = str(response['title']['english'].encode('utf-8'))[2:] doujinshi['subtitle'] = response['title']['japanese'] doujinshi['img_id'] = response['media_id'] doujinshi['ext'] = ''.join(map(lambda s: s['t'], response['images']['pages'])) diff --git a/nhentai/utils.py b/nhentai/utils.py index 21e47f1..fe73730 100644 --- a/nhentai/utils.py +++ b/nhentai/utils.py @@ -3,6 +3,8 @@ from __future__ import unicode_literals, print_function import os import string +import zipfile +import shutil from nhentai.logger import logger @@ -35,7 +37,7 @@ def generate_html(output_dir='.', doujinshi_obj=None): if doujinshi_obj is not None: doujinshi_dir = os.path.join(output_dir, format_filename('%s-%s' % (doujinshi_obj.id, - doujinshi_obj.name[:200]))) + str(doujinshi_obj.name[:200])))) else: doujinshi_dir = '.' @@ -70,6 +72,33 @@ def generate_html(output_dir='.', doujinshi_obj=None): logger.log(15, 'HTML Viewer has been write to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html'))) +def generate_cbz(output_dir='.', doujinshi_obj=None): + if doujinshi_obj is not None: + doujinshi_dir = os.path.join(output_dir, format_filename('%s-%s' % (doujinshi_obj.id, + str(doujinshi_obj.name[:200])))) + cbz_filename = os.path.join(output_dir, format_filename('%s-%s.cbz' % (doujinshi_obj.id, + str(doujinshi_obj.name[:200])))) + else: + cbz_filename = './doujinshi.cbz' + doujinshi_dir = '.' + + file_list = os.listdir(doujinshi_dir) + file_list.sort() + + with zipfile.ZipFile(cbz_filename, 'w') as cbz_pf: + for image in file_list: + image_path = os.path.join(doujinshi_dir, image) + cbz_pf.write(image_path, image) + + shutil.rmtree(doujinshi_dir, ignore_errors=True) + logger.log(15, 'Comic Book CBZ file has been write to \'{0}\''.format(doujinshi_dir)) + + + + + + + def format_filename(s): """Take a string and return a valid filename constructed from the string. Uses a whitelist approach: any characters not present in valid_chars are