mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-07-01 16:09:28 +02:00
Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
8b8b5f193e | |||
fc99d91ac1 | |||
ba141efba7 | |||
f78d8750f3 | |||
af379c825c | |||
2f9386f22c | |||
3667bc34b7 | |||
84749c56bd | |||
24f79e0945 | |||
edc46a9531 | |||
72035a14e6 | |||
472528e464 | |||
3f5915fd2a | |||
0cd2576dab | |||
445a8c052e | |||
7a75afef0a | |||
a5813e19b1 | |||
8462d2f2aa | |||
51074ee948 |
@ -1,9 +1,4 @@
|
||||
include README.md
|
||||
include requirements.txt
|
||||
include nhentai/viewer/index.html
|
||||
include nhentai/viewer/styles.css
|
||||
include nhentai/viewer/scripts.js
|
||||
include nhentai/viewer/main.html
|
||||
include nhentai/viewer/main.css
|
||||
include nhentai/viewer/main.js
|
||||
include nhentai/viewer/logo.png
|
||||
include nhentai/viewer/*
|
||||
include nhentai/viewer/default/*
|
@ -1,3 +1,3 @@
|
||||
__version__ = '0.4.6'
|
||||
__version__ = '0.4.11'
|
||||
__author__ = 'RicterZ'
|
||||
__email__ = 'ricterzheng@gmail.com'
|
||||
|
@ -1,5 +1,5 @@
|
||||
# coding: utf-8
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
@ -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: あなたも変態。 いいね?
|
||||
@ -43,7 +32,7 @@ def load_config():
|
||||
|
||||
try:
|
||||
with open(constant.NHENTAI_CONFIG_FILE, 'r') as f:
|
||||
constant.CONFIG = json.load(f)
|
||||
constant.CONFIG.update(json.load(f))
|
||||
except json.JSONDecodeError:
|
||||
logger.error('Failed to load config file.')
|
||||
write_config()
|
||||
@ -126,6 +115,8 @@ def cmd_parser():
|
||||
default=False, help='save downloaded doujinshis, whose will be skipped if you re-download them')
|
||||
parser.add_option('--clean-download-history', action='store_true', default=False, dest='clean_download_history',
|
||||
help='clean download history')
|
||||
parser.add_option('--template', dest='viewer_template', action='store',
|
||||
help='set viewer template', default='')
|
||||
|
||||
try:
|
||||
sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
|
||||
@ -179,6 +170,19 @@ def cmd_parser():
|
||||
logger.info('Proxy now set to \'{0}\'.'.format(args.proxy))
|
||||
write_config()
|
||||
exit(0)
|
||||
|
||||
if args.viewer_template:
|
||||
if not args.viewer_template:
|
||||
args.viewer_template = 'default'
|
||||
|
||||
if not os.path.exists(os.path.join(os.path.dirname(__file__),
|
||||
'viewer/{}/index.html'.format(args.viewer_template))):
|
||||
logger.error('Template \'{}\' does not exists'.format(args.viewer_template))
|
||||
exit(1)
|
||||
else:
|
||||
constant.CONFIG['template'] = args.viewer_template
|
||||
write_config()
|
||||
|
||||
# --- end set config ---
|
||||
|
||||
if args.favorites:
|
||||
|
@ -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
|
||||
@ -13,7 +12,7 @@ from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, fav
|
||||
from nhentai.doujinshi import Doujinshi
|
||||
from nhentai.downloader import Downloader
|
||||
from nhentai.logger import logger
|
||||
from nhentai.constant import NHENTAI_CONFIG_FILE, BASE_URL
|
||||
from nhentai.constant import BASE_URL
|
||||
from nhentai.utils import generate_html, generate_cbz, generate_main_html, generate_pdf, \
|
||||
paging, check_cookie, signal_handler, DB
|
||||
|
||||
@ -24,8 +23,13 @@ def main():
|
||||
logger.info('Using mirror: {0}'.format(BASE_URL))
|
||||
|
||||
# CONFIG['proxy'] will be changed after cmd_parser()
|
||||
if constant.CONFIG['proxy']:
|
||||
logger.info('Using proxy: {0}'.format(constant.CONFIG['proxy']))
|
||||
if constant.CONFIG['proxy']['http']:
|
||||
logger.info('Using proxy: {0}'.format(constant.CONFIG['proxy']['http']))
|
||||
|
||||
if not constant.CONFIG['template']:
|
||||
constant.CONFIG['template'] = 'default'
|
||||
|
||||
logger.info('Using viewer template "{}"'.format(constant.CONFIG['template']))
|
||||
|
||||
# check your cookie
|
||||
check_cookie()
|
||||
@ -88,7 +92,7 @@ def main():
|
||||
db.add_one(doujinshi.id)
|
||||
|
||||
if not options.is_nohtml and not options.is_cbz and not options.is_pdf:
|
||||
generate_html(options.output_dir, doujinshi)
|
||||
generate_html(options.output_dir, doujinshi, template=constant.CONFIG['template'])
|
||||
elif options.is_cbz:
|
||||
generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir)
|
||||
elif options.is_pdf:
|
||||
@ -108,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()
|
||||
|
@ -1,5 +1,5 @@
|
||||
# coding: utf-8
|
||||
from __future__ import unicode_literals, print_function
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
@ -29,9 +29,10 @@ NHENTAI_HOME = os.path.join(os.getenv('HOME', tempfile.gettempdir()), '.nhentai'
|
||||
NHENTAI_HISTORY = os.path.join(NHENTAI_HOME, 'history.sqlite3')
|
||||
NHENTAI_CONFIG_FILE = os.path.join(NHENTAI_HOME, 'config.json')
|
||||
|
||||
|
||||
CONFIG = {
|
||||
'proxy': {},
|
||||
'proxy': {'http': '', 'https': ''},
|
||||
'cookie': '',
|
||||
'language': '',
|
||||
'template': '',
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
# coding: utf-8
|
||||
from __future__ import print_function, unicode_literals
|
||||
|
||||
from tabulate import tabulate
|
||||
from future.builtins import range
|
||||
|
||||
from nhentai.constant import DETAIL_URL, IMAGE_URL
|
||||
from nhentai.logger import logger
|
||||
|
@ -1,5 +1,4 @@
|
||||
# coding: utf-
|
||||
from __future__ import unicode_literals, print_function
|
||||
|
||||
import multiprocessing
|
||||
import signal
|
||||
|
@ -1,5 +1,4 @@
|
||||
# coding: utf-8
|
||||
from __future__ import unicode_literals, print_function
|
||||
|
||||
import os
|
||||
import re
|
||||
@ -118,6 +117,9 @@ def doujinshi_parser(id_):
|
||||
response = request('get', url)
|
||||
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_))
|
||||
return []
|
||||
else:
|
||||
logger.debug('Slow down and retry ({}) ...'.format(id_))
|
||||
time.sleep(1)
|
||||
|
@ -1,10 +1,8 @@
|
||||
# coding: utf-8
|
||||
from __future__ import unicode_literals, print_function
|
||||
|
||||
import sys
|
||||
import re
|
||||
import os
|
||||
import string
|
||||
import zipfile
|
||||
import shutil
|
||||
import requests
|
||||
@ -64,7 +62,7 @@ def readfile(path):
|
||||
return file.read()
|
||||
|
||||
|
||||
def generate_html(output_dir='.', doujinshi_obj=None):
|
||||
def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
|
||||
image_html = ''
|
||||
|
||||
if doujinshi_obj is not None:
|
||||
@ -81,9 +79,9 @@ def generate_html(output_dir='.', doujinshi_obj=None):
|
||||
|
||||
image_html += '<img src="{0}" class="image-item"/>\n'\
|
||||
.format(image)
|
||||
html = readfile('viewer/index.html')
|
||||
css = readfile('viewer/styles.css')
|
||||
js = readfile('viewer/scripts.js')
|
||||
html = readfile('viewer/{}/index.html'.format(template))
|
||||
css = readfile('viewer/{}/styles.css'.format(template))
|
||||
js = readfile('viewer/{}/scripts.js'.format(template))
|
||||
|
||||
if doujinshi_obj is not None:
|
||||
serialize_json(doujinshi_obj, doujinshi_dir)
|
||||
@ -226,22 +224,31 @@ 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):
|
||||
"""Take a string and return a valid filename constructed from the string.
|
||||
Uses a whitelist approach: any characters not present in valid_chars are
|
||||
removed. Also spaces are replaced with underscores.
|
||||
|
||||
Note: this method may produce invalid filenames such as ``, `.` or `..`
|
||||
When I use this method I prepend a date string like '2009_01_15_19_46_32_'
|
||||
and append a file extension like '.txt', so I avoid the potential of using
|
||||
an invalid filename.
|
||||
|
||||
"""
|
||||
It used to be a whitelist approach allowed only alphabet and a part of symbols.
|
||||
but most doujinshi's names include Japanese 2-byte characters and these was rejected.
|
||||
so it is using blacklist approach now.
|
||||
if filename include forbidden characters (\'/:,;*?"<>|) ,it replace space character(' ').
|
||||
"""
|
||||
# maybe you can use `--format` to select a suitable filename
|
||||
valid_chars = "-_.()[] %s%s" % (string.ascii_letters, string.digits)
|
||||
filename = ''.join(c for c in s if c in valid_chars)
|
||||
ban_chars = '\\\'/:,;*?"<>|\t'
|
||||
filename = s.translate(str.maketrans(ban_chars, ' '*len(ban_chars))).strip()
|
||||
filename = ' '.join(filename.split())
|
||||
print(repr(filename))
|
||||
|
||||
while filename.endswith('.'):
|
||||
filename = filename[:-1]
|
||||
|
||||
if len(filename) > 100:
|
||||
filename = filename[:100] + '...]'
|
||||
filename = filename[:100] + u'…'
|
||||
|
||||
# Remove [] from filename
|
||||
filename = filename.replace('[]', '').strip()
|
||||
|
Reference in New Issue
Block a user