mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-07-02 00:19:29 +02:00
Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
3667bc34b7 | |||
84749c56bd | |||
472528e464 | |||
3f5915fd2a | |||
0cd2576dab | |||
445a8c052e | |||
7a75afef0a | |||
a5813e19b1 | |||
8462d2f2aa | |||
51074ee948 | |||
9c7354be32 | |||
7f48b3edd1 | |||
d84b827241 | |||
4ac161a38c | |||
648b6f87bf | |||
2ec1283ba8 | |||
a9bd46b426 | |||
c52bc271fc | |||
f2d22f8e7d | |||
ea6089ff31 | |||
670d14c3f3 | |||
b46106a5bc | |||
f04359e486 | |||
6861cbcbc1 | |||
e0938c5a0e | |||
4aa34c668a | |||
8ad60d9838 |
@ -1,8 +1,4 @@
|
|||||||
include README.md
|
include README.md
|
||||||
include requirements.txt
|
include requirements.txt
|
||||||
include nhentai/viewer/index.html
|
include nhentai/viewer/*
|
||||||
include nhentai/viewer/styles.css
|
include nhentai/viewer/default/*
|
||||||
include nhentai/viewer/scripts.js
|
|
||||||
include nhentai/viewer/main.html
|
|
||||||
include nhentai/viewer/main.css
|
|
||||||
include nhentai/viewer/main.js
|
|
@ -1,3 +1,3 @@
|
|||||||
__version__ = '0.4.4'
|
__version__ = '0.4.10'
|
||||||
__author__ = 'RicterZ'
|
__author__ = 'RicterZ'
|
||||||
__email__ = 'ricterzheng@gmail.com'
|
__email__ = 'ricterzheng@gmail.com'
|
||||||
|
@ -15,17 +15,6 @@ from nhentai import __version__
|
|||||||
from nhentai.utils import urlparse, generate_html, generate_main_html, DB
|
from nhentai.utils import urlparse, generate_html, generate_main_html, DB
|
||||||
from nhentai.logger import logger
|
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():
|
def banner():
|
||||||
logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
|
logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
|
||||||
@ -43,7 +32,7 @@ def load_config():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
with open(constant.NHENTAI_CONFIG_FILE, 'r') as f:
|
with open(constant.NHENTAI_CONFIG_FILE, 'r') as f:
|
||||||
constant.CONFIG = json.load(f)
|
constant.CONFIG.update(json.load(f))
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
logger.error('Failed to load config file.')
|
logger.error('Failed to load config file.')
|
||||||
write_config()
|
write_config()
|
||||||
@ -87,7 +76,7 @@ def cmd_parser():
|
|||||||
choices=['recent', 'popular', 'popular-today', 'popular-week'])
|
choices=['recent', 'popular', 'popular-today', 'popular-week'])
|
||||||
|
|
||||||
# download options
|
# download options
|
||||||
parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='',
|
parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='./',
|
||||||
help='output dir')
|
help='output dir')
|
||||||
parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
|
parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
|
||||||
help='thread count for downloading doujinshi')
|
help='thread count for downloading doujinshi')
|
||||||
@ -95,7 +84,7 @@ def cmd_parser():
|
|||||||
help='timeout for downloading doujinshi')
|
help='timeout for downloading doujinshi')
|
||||||
parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0,
|
parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0,
|
||||||
help='slow down between downloading every doujinshi')
|
help='slow down between downloading every doujinshi')
|
||||||
parser.add_option('--proxy', '-p', type='string', dest='proxy', action='store', default='',
|
parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
|
||||||
help='store a proxy, for example: -p \'http://127.0.0.1:1080\'')
|
help='store a proxy, for example: -p \'http://127.0.0.1:1080\'')
|
||||||
parser.add_option('--file', '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
|
parser.add_option('--file', '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
|
||||||
parser.add_option('--format', type='string', dest='name_format', action='store',
|
parser.add_option('--format', type='string', dest='name_format', action='store',
|
||||||
@ -126,6 +115,8 @@ def cmd_parser():
|
|||||||
default=False, help='save downloaded doujinshis, whose will be skipped if you re-download them')
|
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',
|
parser.add_option('--clean-download-history', action='store_true', default=False, dest='clean_download_history',
|
||||||
help='clean download history')
|
help='clean download history')
|
||||||
|
parser.add_option('--template', dest='viewer_template', action='store',
|
||||||
|
help='set viewer template', default='')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
|
sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
|
||||||
@ -153,30 +144,45 @@ def cmd_parser():
|
|||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
# --- set config ---
|
# --- set config ---
|
||||||
if args.cookie:
|
if args.cookie is not None:
|
||||||
constant.CONFIG['cookie'] = args.cookie
|
constant.CONFIG['cookie'] = args.cookie
|
||||||
logger.info('Cookie saved.')
|
logger.info('Cookie saved.')
|
||||||
write_config()
|
write_config()
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
if args.language:
|
if args.language is not None:
|
||||||
constant.CONFIG['language'] = args.language
|
constant.CONFIG['language'] = args.language
|
||||||
logger.info('LANGUAGE now set to \'{0}\''.format(args.language))
|
logger.info('Default language now set to \'{0}\''.format(args.language))
|
||||||
write_config()
|
write_config()
|
||||||
exit(0)
|
exit(0)
|
||||||
# TODO: search without language
|
# TODO: search without language
|
||||||
|
|
||||||
if args.proxy:
|
if args.proxy:
|
||||||
proxy_url = urlparse(args.proxy)
|
proxy_url = urlparse(args.proxy)
|
||||||
if proxy_url.scheme not in ('http', 'https'):
|
if not args.proxy == '' and proxy_url.scheme not in ('http', 'https'):
|
||||||
logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
|
logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
|
||||||
|
exit(0)
|
||||||
|
else:
|
||||||
constant.CONFIG['proxy'] = {
|
constant.CONFIG['proxy'] = {
|
||||||
'http': args.proxy,
|
'http': args.proxy,
|
||||||
'https': args.proxy,
|
'https': args.proxy,
|
||||||
}
|
}
|
||||||
logger.info('Proxy \'{0}\' saved.'.format(args.proxy))
|
logger.info('Proxy now set to \'{0}\'.'.format(args.proxy))
|
||||||
write_config()
|
write_config()
|
||||||
exit(0)
|
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 ---
|
# --- end set config ---
|
||||||
|
|
||||||
if args.favorites:
|
if args.favorites:
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
#!/usr/bin/env python2.7
|
#!/usr/bin/env python2.7
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals, print_function
|
from __future__ import unicode_literals, print_function
|
||||||
import json
|
import sys
|
||||||
import os
|
|
||||||
import signal
|
import signal
|
||||||
import platform
|
import platform
|
||||||
import time
|
import time
|
||||||
@ -13,7 +12,7 @@ from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, fav
|
|||||||
from nhentai.doujinshi import Doujinshi
|
from nhentai.doujinshi import Doujinshi
|
||||||
from nhentai.downloader import Downloader
|
from nhentai.downloader import Downloader
|
||||||
from nhentai.logger import logger
|
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, \
|
from nhentai.utils import generate_html, generate_cbz, generate_main_html, generate_pdf, \
|
||||||
paging, check_cookie, signal_handler, DB
|
paging, check_cookie, signal_handler, DB
|
||||||
|
|
||||||
@ -24,8 +23,13 @@ def main():
|
|||||||
logger.info('Using mirror: {0}'.format(BASE_URL))
|
logger.info('Using mirror: {0}'.format(BASE_URL))
|
||||||
|
|
||||||
# CONFIG['proxy'] will be changed after cmd_parser()
|
# CONFIG['proxy'] will be changed after cmd_parser()
|
||||||
if constant.CONFIG['proxy']:
|
if constant.CONFIG['proxy']['http']:
|
||||||
logger.info('Using proxy: {0}'.format(constant.CONFIG['proxy']))
|
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 your cookie
|
||||||
check_cookie()
|
check_cookie()
|
||||||
@ -58,9 +62,9 @@ def main():
|
|||||||
|
|
||||||
if options.is_save_download_history:
|
if options.is_save_download_history:
|
||||||
with DB() as db:
|
with DB() as db:
|
||||||
data = set(db.get_all())
|
data = map(int, db.get_all())
|
||||||
|
|
||||||
doujinshi_ids = list(set(doujinshi_ids) - data)
|
doujinshi_ids = list(set(doujinshi_ids) - set(data))
|
||||||
|
|
||||||
if doujinshi_ids:
|
if doujinshi_ids:
|
||||||
for i, id_ in enumerate(doujinshi_ids):
|
for i, id_ in enumerate(doujinshi_ids):
|
||||||
@ -88,7 +92,7 @@ def main():
|
|||||||
db.add_one(doujinshi.id)
|
db.add_one(doujinshi.id)
|
||||||
|
|
||||||
if not options.is_nohtml and not options.is_cbz and not options.is_pdf:
|
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:
|
elif options.is_cbz:
|
||||||
generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir)
|
generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir)
|
||||||
elif options.is_pdf:
|
elif options.is_pdf:
|
||||||
@ -108,5 +112,10 @@ def main():
|
|||||||
|
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
if sys.version_info < (3, 0, 0):
|
||||||
|
logger.error('nhentai now only support Python 3.x')
|
||||||
|
exit(1)
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
from __future__ import unicode_literals, print_function
|
from __future__ import unicode_literals, print_function
|
||||||
import os
|
import os
|
||||||
|
import copy
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -29,9 +30,10 @@ NHENTAI_HOME = os.path.join(os.getenv('HOME', tempfile.gettempdir()), '.nhentai'
|
|||||||
NHENTAI_HISTORY = os.path.join(NHENTAI_HOME, 'history.sqlite3')
|
NHENTAI_HISTORY = os.path.join(NHENTAI_HOME, 'history.sqlite3')
|
||||||
NHENTAI_CONFIG_FILE = os.path.join(NHENTAI_HOME, 'config.json')
|
NHENTAI_CONFIG_FILE = os.path.join(NHENTAI_HOME, 'config.json')
|
||||||
|
|
||||||
|
|
||||||
CONFIG = {
|
CONFIG = {
|
||||||
'proxy': {},
|
'proxy': {'http': '', 'https': ''},
|
||||||
'cookie': '',
|
'cookie': '',
|
||||||
'language': '',
|
'language': '',
|
||||||
|
'template': '',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ from __future__ import unicode_literals, print_function
|
|||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import string
|
|
||||||
import zipfile
|
import zipfile
|
||||||
import shutil
|
import shutil
|
||||||
import requests
|
import requests
|
||||||
@ -64,7 +63,7 @@ def readfile(path):
|
|||||||
return file.read()
|
return file.read()
|
||||||
|
|
||||||
|
|
||||||
def generate_html(output_dir='.', doujinshi_obj=None):
|
def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
|
||||||
image_html = ''
|
image_html = ''
|
||||||
|
|
||||||
if doujinshi_obj is not None:
|
if doujinshi_obj is not None:
|
||||||
@ -81,9 +80,9 @@ def generate_html(output_dir='.', doujinshi_obj=None):
|
|||||||
|
|
||||||
image_html += '<img src="{0}" class="image-item"/>\n'\
|
image_html += '<img src="{0}" class="image-item"/>\n'\
|
||||||
.format(image)
|
.format(image)
|
||||||
html = readfile('viewer/index.html')
|
html = readfile('viewer/{}/index.html'.format(template))
|
||||||
css = readfile('viewer/styles.css')
|
css = readfile('viewer/{}/styles.css'.format(template))
|
||||||
js = readfile('viewer/scripts.js')
|
js = readfile('viewer/{}/scripts.js'.format(template))
|
||||||
|
|
||||||
if doujinshi_obj is not None:
|
if doujinshi_obj is not None:
|
||||||
serialize_json(doujinshi_obj, doujinshi_dir)
|
serialize_json(doujinshi_obj, doujinshi_dir)
|
||||||
@ -226,22 +225,28 @@ 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))
|
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):
|
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
|
It used to be a whitelist approach allowed only alphabet and a part of symbols.
|
||||||
removed. Also spaces are replaced with underscores.
|
but most doujinshi's names include Japanese 2-byte characters and these was rejected.
|
||||||
|
so it is using blacklist approach now.
|
||||||
Note: this method may produce invalid filenames such as ``, `.` or `..`
|
if filename include forbidden characters (\'/:,;*?"<>|) ,it replace space character(' ').
|
||||||
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.
|
|
||||||
|
|
||||||
"""
|
|
||||||
# maybe you can use `--format` to select a suitable filename
|
# maybe you can use `--format` to select a suitable filename
|
||||||
valid_chars = "-_.()[] %s%s" % (string.ascii_letters, string.digits)
|
ban_chars = '\\\'/:,;*?"<>|'
|
||||||
filename = ''.join(c for c in s if c in valid_chars)
|
filename = s.translate(str.maketrans(ban_chars, ' '*len(ban_chars))).strip()
|
||||||
|
while filename.endswith('.'):
|
||||||
|
filename = filename[:-1]
|
||||||
|
|
||||||
if len(filename) > 100:
|
if len(filename) > 100:
|
||||||
filename = filename[:100] + '...]'
|
filename = filename[:100] + u'…'
|
||||||
|
|
||||||
# Remove [] from filename
|
# Remove [] from filename
|
||||||
filename = filename.replace('[]', '').strip()
|
filename = filename.replace('[]', '').strip()
|
||||||
|
Reference in New Issue
Block a user