From 84749c56bd5d39238a8e334dde23d1f8b0bb2b90 Mon Sep 17 00:00:00 2001 From: RicterZ Date: Sun, 10 Jan 2021 11:40:46 +0800 Subject: [PATCH] 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() -- 2.44.0