From 84749c56bd5d39238a8e334dde23d1f8b0bb2b90 Mon Sep 17 00:00:00 2001 From: RicterZ Date: Sun, 10 Jan 2021 11:40:46 +0800 Subject: [PATCH 1/3] fix #191 --- nhentai/cmdline.py | 11 ----------- nhentai/command.py | 8 ++++++-- nhentai/utils.py | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index 532beb4..b14ec4d 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -15,17 +15,6 @@ from nhentai import __version__ from nhentai.utils import urlparse, generate_html, generate_main_html, DB from nhentai.logger import logger -try: - if sys.version_info < (3, 0, 0): - import codecs - import locale - sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout) - sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr) - -except NameError: - # python3 - pass - def banner(): logger.info(u'''nHentai ver %s: あなたも変態。 いいね? diff --git a/nhentai/command.py b/nhentai/command.py index 7341677..16adc22 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -1,8 +1,7 @@ #!/usr/bin/env python2.7 # coding: utf-8 from __future__ import unicode_literals, print_function -import json -import os +import sys import signal import platform import time @@ -113,5 +112,10 @@ def main(): signal.signal(signal.SIGINT, signal_handler) + if __name__ == '__main__': + if sys.version_info < (3, 0, 0): + logger.error('nhentai now only support Python 3.x') + exit(1) + main() diff --git a/nhentai/utils.py b/nhentai/utils.py index 1ee6e7e..de40d94 100644 --- a/nhentai/utils.py +++ b/nhentai/utils.py @@ -4,7 +4,6 @@ from __future__ import unicode_literals, print_function import sys import re import os -import string import zipfile import shutil import requests @@ -226,6 +225,13 @@ def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False): logger.log(15, 'PDF file has been written to \'{0}\''.format(doujinshi_dir)) +def unicode_truncate(s, length, encoding='utf-8'): + """https://stackoverflow.com/questions/1809531/truncating-unicode-so-it-fits-a-maximum-size-when-encoded-for-wire-transfer + """ + encoded = s.encode(encoding)[:length] + return encoded.decode(encoding, 'ignore') + + def format_filename(s): """ It used to be a whitelist approach allowed only alphabet and a part of symbols. @@ -235,9 +241,12 @@ def format_filename(s): """ # maybe you can use `--format` to select a suitable filename ban_chars = '\\\'/:,;*?"<>|' - filename = s.translate(str.maketrans(ban_chars, ' '*len(ban_chars))) + filename = s.translate(str.maketrans(ban_chars, ' '*len(ban_chars))).strip() + while filename.endswith('.'): + filename = filename[:-1] + if len(filename) > 100: - filename = filename[:100] + '...]' + filename = filename[:100] + u'…' # Remove [] from filename filename = filename.replace('[]', '').strip() From 3667bc34b765e405655550433c9349a58dc62887 Mon Sep 17 00:00:00 2001 From: RicterZ Date: Sun, 10 Jan 2021 11:41:38 +0800 Subject: [PATCH 2/3] 0.4.10 --- nhentai/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nhentai/__init__.py b/nhentai/__init__.py index a05cc6d..46ed22e 100644 --- a/nhentai/__init__.py +++ b/nhentai/__init__.py @@ -1,3 +1,3 @@ -__version__ = '0.4.9' +__version__ = '0.4.10' __author__ = 'RicterZ' __email__ = 'ricterzheng@gmail.com' From 2f9386f22c630c461599072f6843d0542fe041d0 Mon Sep 17 00:00:00 2001 From: RicterZ Date: Sun, 10 Jan 2021 11:44:04 +0800 Subject: [PATCH 3/3] fix #188 --- nhentai/parser.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nhentai/parser.py b/nhentai/parser.py index 484c0f6..be6643c 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -116,8 +116,10 @@ def doujinshi_parser(id_): try: response = request('get', url) - if response.status_code in (200,): + if response.status_code in (200, ): response = response.content + elif response.status_code in (404, ): + raise Exception('Seems doujinshi {} does not exist'.format(id_)) else: logger.debug('Slow down and retry ({}) ...'.format(id_)) time.sleep(1)