Compare commits

..

24 Commits

Author SHA1 Message Date
3667bc34b7 0.4.10 2021-01-10 11:41:38 +08:00
84749c56bd fix #191 2021-01-10 11:40:46 +08:00
472528e464 Merge pull request #187 from atsushi-hirako/patch-1
fix issue #186
2021-01-02 02:16:50 +08:00
3f5915fd2a fix issue #186
change to blacklist approach (allow 2-bytes character)
2021-01-01 20:11:09 +09:00
0cd2576dab 0.4.9 2020-12-02 07:45:31 +08:00
445a8c052e Merge pull request #180 from RicterZ/dev
0.4.8
2020-12-01 21:01:00 +08:00
7a75afef0a 0.4.8 2020-12-01 20:58:28 +08:00
a5813e19b1 fix bug on first start 2020-12-01 20:56:27 +08:00
8462d2f2aa use dict.update to update config values 2020-11-26 17:52:10 +08:00
51074ee948 support multi viewers 2020-11-26 17:22:23 +08:00
9c7354be32 0.4.6 2020-11-07 12:04:42 +08:00
7f48b3edd1 Merge pull request #175 from RicterZ/dev
add default value of output dir
2020-10-15 02:10:06 +08:00
d84b827241 add default value of output dir 2020-10-15 02:09:09 +08:00
4ac161a38c Merge pull request #174 from Nontre12/fix-gen-main
Fix change directory output_dir option on gen-main
2020-10-15 01:47:51 +08:00
648b6f87bf Added logo.png to the installation 2020-10-14 12:09:39 +02:00
2ec1283ba8 Fix change directory output_dir option on gen-main 2020-10-14 12:02:57 +02:00
a9bd46b426 Merge pull request #173 from Nontre12/db-ignored
Fix db ignored
2020-10-14 02:44:03 +08:00
c52bc271fc Fix db ignored 2020-10-13 13:39:24 +02:00
f2d22f8e7d Merge pull request #169 from Nontre12/master
Fix running without parameters
2020-10-11 03:48:39 +08:00
ea6089ff31 Fix 2020-10-10 21:15:20 +02:00
670d14c3f3 Merge pull request #4 from RicterZ/master
Update master branch
2020-10-10 20:50:01 +02:00
b46106a5bc Merge pull request #167 from RicterZ/0.4.5
0.4.5
2020-10-11 02:00:02 +08:00
4aa34c668a Merge pull request #3 from RicterZ/master
Update master branch from origin
2020-10-10 19:11:56 +02:00
8ad60d9838 Merge pull request #1 from RicterZ/master
Merge pull request #162 from Nontre12/master
2020-10-10 18:31:47 +02:00
9 changed files with 65 additions and 49 deletions

View File

@ -1,8 +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/*
include nhentai/viewer/default/*

View File

@ -1,3 +1,3 @@
__version__ = '0.4.5'
__version__ = '0.4.10'
__author__ = 'RicterZ'
__email__ = 'ricterzheng@gmail.com'

View File

@ -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()
@ -87,7 +76,7 @@ def cmd_parser():
choices=['recent', 'popular', 'popular-today', 'popular-week'])
# 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')
parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
help='thread count for downloading doujinshi')
@ -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]
@ -166,7 +157,7 @@ def cmd_parser():
exit(0)
# TODO: search without language
if args.proxy is not None:
if args.proxy:
proxy_url = urlparse(args.proxy)
if not args.proxy == '' and proxy_url.scheme not in ('http', 'https'):
logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
@ -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:

View File

@ -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()
@ -58,9 +62,9 @@ def main():
if options.is_save_download_history:
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:
for i, id_ in enumerate(doujinshi_ids):
@ -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()

View File

@ -1,6 +1,7 @@
# coding: utf-8
from __future__ import unicode_literals, print_function
import os
import copy
import tempfile
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_CONFIG_FILE = os.path.join(NHENTAI_HOME, 'config.json')
CONFIG = {
'proxy': {},
'proxy': {'http': '', 'https': ''},
'cookie': '',
'language': '',
'template': '',
}

View File

@ -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
@ -64,7 +63,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 +80,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 +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))
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 = '\\\'/:,;*?"<>|'
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()