From 45fb35b950ed7d2518ea3b9b78abf06792be188a Mon Sep 17 00:00:00 2001 From: Ricter Z Date: Mon, 1 Jan 2018 17:44:55 +0800 Subject: [PATCH] fix bug and add --html --- nhentai/__init__.py | 2 +- nhentai/cmdline.py | 7 ++++++- nhentai/command.py | 28 ++-------------------------- nhentai/utils.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/nhentai/__init__.py b/nhentai/__init__.py index da57631..6e212f0 100644 --- a/nhentai/__init__.py +++ b/nhentai/__init__.py @@ -1,3 +1,3 @@ -__version__ = '0.2.8' +__version__ = '0.2.9' __author__ = 'Ricter' __email__ = 'ricterzheng@gmail.com' diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index eca33ac..6d049ef 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -8,7 +8,7 @@ except ImportError: pass import nhentai.constant as constant -from nhentai.utils import urlparse +from nhentai.utils import urlparse, generate_html from nhentai.logger import logger try: @@ -49,6 +49,7 @@ def cmd_parser(): help='timeout of download doujinshi') parser.add_option('--proxy', type='string', dest='proxy', action='store', default='', help='use proxy, example: http://127.0.0.1:1080') + parser.add_option('--html', dest='html_viewer', action='store_true', help='generate a html viewer at current directory') try: sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv)) @@ -59,6 +60,10 @@ def cmd_parser(): args, _ = parser.parse_args(sys.argv[1:]) + if args.html_viewer: + generate_html() + exit(0) + if args.tags: logger.warning('`--tags` is under construction') exit(0) diff --git a/nhentai/command.py b/nhentai/command.py index f7157da..fbd9ae4 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -11,6 +11,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 def main(): @@ -43,32 +44,7 @@ def main(): for doujinshi in doujinshi_list: doujinshi.downloader = downloader doujinshi.download() - - image_html = '' - previous = '' - - doujinshi_dir = os.path.join(options.output_dir, str(doujinshi.id)) - file_list = os.listdir(doujinshi_dir) - file_list.sort() - - for index, image in enumerate(file_list): - try: - next_ = file_list[file_list.index(image) + 1] - except IndexError: - next_ = '' - - image_html += '\n'\ - .format(image, 'current' if index == 0 else '', previous, next_) - previous = image - - with open(os.path.join(os.path.dirname(__file__), 'doujinshi.html'), 'r') as template: - html = template.read() - - data = html.format(TITLE=doujinshi.name, IMAGES=image_html) - with open(os.path.join(doujinshi_dir, 'index.html'), 'w') as f: - f.write(data) - - logger.log(15, 'HTML Viewer has been write to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html'))) + generate_html(doujinshi, output_dir) if not platform.system() == 'Windows': logger.log(15, '🍺 All done.') diff --git a/nhentai/utils.py b/nhentai/utils.py index 6377b14..4f48036 100644 --- a/nhentai/utils.py +++ b/nhentai/utils.py @@ -1,6 +1,9 @@ # coding: utf-8 from __future__ import unicode_literals, print_function +import os +from nhentai.logger import logger + class _Singleton(type): """ A metaclass that creates a Singleton base class when called. """ @@ -23,3 +26,43 @@ def urlparse(url): from urllib.parse import urlparse return urlparse(url) + + +def generate_html(output_dir='.', doujinshi_obj=None): + image_html = '' + previous = '' + + if doujinshi_obj is not None: + doujinshi_dir = os.path.join(output_dir, str(doujinshi_obj.id)) + else: + doujinshi_dir = '.' + + file_list = os.listdir(doujinshi_dir) + file_list.sort() + + for index, image in enumerate(file_list): + if not os.path.splitext(image)[1] in ('.jpg', '.png'): + continue + + try: + next_ = file_list[file_list.index(image) + 1] + except IndexError: + next_ = '' + + image_html += '\n'\ + .format(image, 'current' if index == 0 else '', previous, next_) + previous = image + + with open(os.path.join(os.path.dirname(__file__), 'doujinshi.html'), 'r') as template: + html = template.read() + + if doujinshi_obj is not None: + title = doujinshi_obj.name + else: + title = 'nHentai HTML Viewer' + + data = html.format(TITLE=title, IMAGES=image_html) + with open(os.path.join(doujinshi_dir, 'index.html'), 'w') as f: + f.write(data) + + logger.log(15, 'HTML Viewer has been write to \'{0}\''.format(os.path.join(doujinshi_dir, 'index.html')))