From 147eec57cf0a32f393c01859b7316a9d45ec1994 Mon Sep 17 00:00:00 2001 From: Mentat Erasmus Date: Wed, 9 May 2018 15:42:12 -0300 Subject: [PATCH 1/9] 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 From f2573d5f10afecb985fa4c7ff92927a48cf0eccc Mon Sep 17 00:00:00 2001 From: Mentat Erasmus Date: Mon, 14 May 2018 01:52:38 -0300 Subject: [PATCH 2/9] fixing identation --- README.md | 2 ++ nhentai/cmdline.py | 2 +- nhentai/command.py | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c7f7d8e..e7d04d4 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ nhentai --login "username:password" --download `--timeout`:指定下载图片的超时时间,默认为 30 秒。 `--proxy`:指定下载的代理,例如: http://127.0.0.1:8080/ `--login`:nhentai 账号的“用户名:密码”组合 +`--nohtml`:nhentai Don't generate HTML +`--cbz`:nhentai Generate Comic Book CBZ file ### nHentai Mirror 如果想用自建镜像下载 nhentai 的本子,需要搭建 nhentai.net 和 i.nhentai.net 的反向代理。 diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index ffff67c..1cb58a0 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -60,7 +60,7 @@ def cmd_parser(): help='Don\'t generate HTML') parser.add_option('--cbz', dest='is_cbz', action='store_true', - help='Generate ComicBook CBZ File') + help='Generate Comic Book CBZ File') try: sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv)) diff --git a/nhentai/command.py b/nhentai/command.py index 82a2fd6..309306c 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -48,7 +48,7 @@ def main(): doujinshi.downloader = downloader doujinshi.download() if not options.is_nohtml and not options.is_cbz: - generate_html(options.output_dir, doujinshi) + generate_html(options.output_dir, doujinshi) elif options.is_cbz: generate_cbz(options.output_dir, doujinshi) From 99edcef9ac657c9fcd142d89626e5fca7d512348 Mon Sep 17 00:00:00 2001 From: Pizzacus Date: Sat, 2 Jun 2018 17:47:39 +0200 Subject: [PATCH 3/9] Rework the HTML Viewer * More modern and efficient code, particularily for the JS * Also the layout is better, with flexboxes and all * The CSS and JS have their own files * The sidebar has proper margins around the images * You can use A + D and the arrow keys to navigate images, like on nhentai * Images with a lot of width are properly sized * There is a page counter on the bottom left --- nhentai/doujinshi.html | 126 -------------------------------------- nhentai/utils.py | 25 ++++---- nhentai/viewer/index.html | 24 ++++++++ nhentai/viewer/scripts.js | 62 +++++++++++++++++++ nhentai/viewer/styles.css | 69 +++++++++++++++++++++ 5 files changed, 167 insertions(+), 139 deletions(-) delete mode 100644 nhentai/doujinshi.html create mode 100644 nhentai/viewer/index.html create mode 100644 nhentai/viewer/scripts.js create mode 100644 nhentai/viewer/styles.css diff --git a/nhentai/doujinshi.html b/nhentai/doujinshi.html deleted file mode 100644 index eae0e25..0000000 --- a/nhentai/doujinshi.html +++ /dev/null @@ -1,126 +0,0 @@ - - - - - {TITLE} - - - - - - -
-{IMAGES}
-
-
- - - -
-
- - - - \ No newline at end of file diff --git a/nhentai/utils.py b/nhentai/utils.py index fe73730..3cddd50 100644 --- a/nhentai/utils.py +++ b/nhentai/utils.py @@ -30,10 +30,14 @@ def urlparse(url): return urlparse(url) +def readfile(path): + loc = os.path.dirname(__file__) + + with open(os.path.join(loc, path), 'r') as file: + return file.read() def generate_html(output_dir='.', doujinshi_obj=None): image_html = '' - previous = '' if doujinshi_obj is not None: doujinshi_dir = os.path.join(output_dir, format_filename('%s-%s' % (doujinshi_obj.id, @@ -44,28 +48,23 @@ def generate_html(output_dir='.', doujinshi_obj=None): file_list = os.listdir(doujinshi_dir) file_list.sort() - for index, image in enumerate(file_list): + for image in 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) - 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() + html = readfile('viewer/index.html') + css = readfile('viewer/styles.css') + js = readfile('viewer/scripts.js') if doujinshi_obj is not None: title = doujinshi_obj.name else: title = 'nHentai HTML Viewer' - data = html.format(TITLE=title, IMAGES=image_html) + data = html.format(TITLE=title, IMAGES=image_html, SCRIPTS=js, STYLES=css) with open(os.path.join(doujinshi_dir, 'index.html'), 'w') as f: f.write(data) diff --git a/nhentai/viewer/index.html b/nhentai/viewer/index.html new file mode 100644 index 0000000..e7cb006 --- /dev/null +++ b/nhentai/viewer/index.html @@ -0,0 +1,24 @@ + + + + + {TITLE} + + + + + + +
+ +
+
+ + + + \ No newline at end of file diff --git a/nhentai/viewer/scripts.js b/nhentai/viewer/scripts.js new file mode 100644 index 0000000..721f45a --- /dev/null +++ b/nhentai/viewer/scripts.js @@ -0,0 +1,62 @@ +const pages = Array.from(document.querySelectorAll('img.image-item')); +let currentPage = 0; + +function changePage(pageNum) { + const previous = pages[currentPage]; + const current = pages[pageNum]; + + if (current == null) { + return; + } + + previous.classList.remove('current'); + current.classList.add('current'); + + currentPage = pageNum; + + const display = document.getElementById('dest'); + display.style.backgroundImage = `url("${current.src}")`; + + document.getElementById('page-num') + .innerText = [ + (pageNum + 1).toLocaleString(), + pages.length.toLocaleString() + ].join('\u200a/\u200a'); +} + +changePage(0); + +document.getElementById('list').onclick = event => { + if (pages.includes(event.target)) { + changePage(pages.indexOf(event.target)); + } +}; + +document.getElementById('image-container').onclick = event => { + const width = document.getElementById('image-container').clientWidth; + const clickPos = event.clientX / width; + + if (clickPos < 0.5) { + changePage(currentPage - 1); + } else { + changePage(currentPage + 1); + } +}; + +document.onkeypress = event => { + switch (event.key.toLowerCase()) { + // Previous Image + case 'arrowleft': + case 'a': + changePage(currentPage - 1); + break; + + // Next Image + case ' ': + case 'enter': + case 'arrowright': + case 'd': + changePage(currentPage + 1); + break; + } +}; \ No newline at end of file diff --git a/nhentai/viewer/styles.css b/nhentai/viewer/styles.css new file mode 100644 index 0000000..4de6fd9 --- /dev/null +++ b/nhentai/viewer/styles.css @@ -0,0 +1,69 @@ +*, *::after, *::before { + box-sizing: border-box; +} + +img { + vertical-align: middle; +} + +html, body { + display: flex; + background-color: #e8e6e6; + height: 100%; + width: 100%; + padding: 0; + margin: 0; + font-family: sans-serif; +} + +#list { + height: 100%; + overflow: auto; + width: 260px; + text-align: center; +} + +#list img { + width: 200px; + padding: 10px; + border-radius: 10px; + margin: 15px 0; + cursor: pointer; +} + +#list img.current { + background: #0003; +} + +#image-container { + flex: auto; + height: 100vh; + background: #222; + color: #fff; + text-align: center; + cursor: pointer; + -webkit-user-select: none; + user-select: none; + position: relative; +} + +#image-container #dest { + height: 100%; + width: 100%; + background-size: contain; + background-repeat: no-repeat; + background-position: center; +} + +#image-container #page-num { + position: absolute; + font-size: 18pt; + left: 10px; + bottom: 5px; + font-weight: bold; + opacity: 0.75; + text-shadow: /* Duplicate the same shadow to make it very strong */ + 0 0 2px #222, + 0 0 2px #222, + 0 0 2px #222; +} \ No newline at end of file From 0bc83982e4df8c2eb0ec88952656a40504143c83 Mon Sep 17 00:00:00 2001 From: Pizzacus Date: Sun, 3 Jun 2018 11:09:46 +0200 Subject: [PATCH 4/9] Add the viewer to the package_data entry --- setup.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/setup.py b/setup.py index d0f6562..933bb8f 100644 --- a/setup.py +++ b/setup.py @@ -19,6 +19,9 @@ setup( name='nhentai', version=__version__, packages=find_packages(), + package_data={ + 'nhentai': ['viewer/**'] + }, author=__author__, author_email=__email__, From 98bf88d638eca2d38033fd176529aa1974ae55bc Mon Sep 17 00:00:00 2001 From: Pizzacus Date: Sun, 3 Jun 2018 11:32:06 +0200 Subject: [PATCH 5/9] Actually use MANIFEST.ini to specify the package data *considers suicide* --- MANIFEST.in | 4 +++- setup.py | 3 --- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index edb9348..87eea17 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,5 @@ include README.md include requirements.txt -include nhentai/doujinshi.html +include nhentai/viewer/index.html +include nhentai/viewer/styles.css +include nhentai/viewer/scripts.js diff --git a/setup.py b/setup.py index 933bb8f..d0f6562 100644 --- a/setup.py +++ b/setup.py @@ -19,9 +19,6 @@ setup( name='nhentai', version=__version__, packages=find_packages(), - package_data={ - 'nhentai': ['viewer/**'] - }, author=__author__, author_email=__email__, From b5b201f61ca68c37e44ee1dc01233d885fcda4c5 Mon Sep 17 00:00:00 2001 From: Ricter Z Date: Sun, 1 Jul 2018 02:15:26 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=F0=9F=8D=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nhentai/cmdline.py | 5 +++-- nhentai/command.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index 1cb58a0..d3453d2 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -2,6 +2,7 @@ from __future__ import print_function import sys from optparse import OptionParser +from nhentai import __version__ try: from itertools import ifilter as filter except ImportError: @@ -20,13 +21,13 @@ except NameError: def banner(): - logger.info(u'''nHentai: あなたも変態。 いいね? + logger.info(u'''nHentai ver %s: あなたも変態。 いいね? _ _ _ _ _ __ | | | | ___ _ __ | |_ __ _(_) | '_ \| |_| |/ _ \ '_ \| __/ _` | | | | | | _ | __/ | | | || (_| | | |_| |_|_| |_|\___|_| |_|\__\__,_|_| -''') +''' % __version__) def cmd_parser(): diff --git a/nhentai/command.py b/nhentai/command.py index 309306c..5f08cef 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -53,7 +53,7 @@ def main(): 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.') From 206aa3710acf6be1360ea4b28ea24d4bc1341d75 Mon Sep 17 00:00:00 2001 From: Ricter Z Date: Sun, 1 Jul 2018 12:48:05 +0800 Subject: [PATCH 7/9] fix bug --- nhentai/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nhentai/parser.py b/nhentai/parser.py index 7b5adf9..25e2d1e 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -95,7 +95,7 @@ def doujinshi_parser(id_): logger.critical(str(e)) exit(1) - doujinshi['name'] = str(response['title']['english'].encode('utf-8'))[2:] + doujinshi['name'] = response['title']['english'] doujinshi['subtitle'] = response['title']['japanese'] doujinshi['img_id'] = response['media_id'] doujinshi['ext'] = ''.join(map(lambda s: s['t'], response['images']['pages'])) From ddc4a2025191d3c5ef7207cdd9b54eca51be2a45 Mon Sep 17 00:00:00 2001 From: Ricter Z Date: Sun, 1 Jul 2018 12:48:30 +0800 Subject: [PATCH 8/9] 0.2.12 --- nhentai/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nhentai/__init__.py b/nhentai/__init__.py index 59cbd67..5d1982e 100644 --- a/nhentai/__init__.py +++ b/nhentai/__init__.py @@ -1,3 +1,3 @@ -__version__ = '0.2.11' -__author__ = 'Ricter' +__version__ = '0.2.12' +__author__ = 'RicterZ' __email__ = 'ricterzheng@gmail.com' From 6ff9751c30412f0d4331796fad1a6b73dcdefa63 Mon Sep 17 00:00:00 2001 From: Ricter Z Date: Sun, 1 Jul 2018 12:50:37 +0800 Subject: [PATCH 9/9] fix --- nhentai/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nhentai/__init__.py b/nhentai/__init__.py index 5d1982e..67105c6 100644 --- a/nhentai/__init__.py +++ b/nhentai/__init__.py @@ -1,3 +1,3 @@ -__version__ = '0.2.12' +__version__ = '0.2.13' __author__ = 'RicterZ' __email__ = 'ricterzheng@gmail.com'