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' 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/parser.py b/nhentai/parser.py index 0df5710..582add2 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -116,7 +116,7 @@ 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,): logger.error("Doujinshi with id {0} cannot be found".format(id_)) 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()