# coding: utf-8
-from __future__ import print_function
+
import os
import sys
import json
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: あなたも変態。 いいね?
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()
help='page number of search results. e.g. 1,2-5,14')
parser.add_option('--sorting', dest='sorting', action='store', default='recent',
help='sorting of doujinshi (recent / popular / popular-[today|week])',
- choices=['recent', 'popular', 'popular-today', 'popular-week'])
+ choices=['recent', 'popular', 'popular-today', 'popular-week', 'date'])
# download options
parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='./',
help='timeout for downloading doujinshi')
parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0,
help='slow down between downloading every doujinshi')
- parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
+ parser.add_option('--proxy', type='string', dest='proxy', action='store',
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',
help='format the saved folder name', default='[%i][%a][%t]')
+ parser.add_option('--dry-run', '-r', action='store_true', dest='dryrun', help='Dry run, skip file download.')
# generate options
parser.add_option('--html', dest='html_viewer', action='store_true',
help='generate PDF file')
parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
help='remove downloaded doujinshi dir when generated CBZ or PDF file.')
+ parser.add_option('--meta', dest='generate_metadata', action='store_true',
+ help='generate a metadata file in doujinshi format')
+ parser.add_option('--regenerate-cbz', dest='regenerate_cbz', action='store_true', default=False,
+ help='regenerate the cbz file if exists')
# nhentai options
parser.add_option('--cookie', type='str', dest='cookie', action='store',
- help='set cookie of nhentai to bypass Google recaptcha')
+ help='set cookie of nhentai to bypass Cloudflare captcha')
+ parser.add_option('--useragent', '--user-agent', type='str', dest='useragent', action='store',
+ help='set useragent to bypass Cloudflare captcha')
parser.add_option('--language', type='str', dest='language', action='store',
help='set default language to parse doujinshis')
parser.add_option('--clean-language', dest='clean_language', action='store_true', default=False,
try:
sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
- print()
except (NameError, TypeError):
pass
except UnicodeDecodeError:
args, _ = parser.parse_args(sys.argv[1:])
if args.html_viewer:
- generate_html()
+ generate_html(template=constant.CONFIG['template'])
exit(0)
if args.main_viewer and not args.id and not args.keyword and not args.favorites:
# --- set config ---
if args.cookie is not None:
constant.CONFIG['cookie'] = args.cookie
+ write_config()
logger.info('Cookie saved.')
+ exit(0)
+ elif args.useragent is not None:
+ constant.CONFIG['useragent'] = args.useragent
write_config()
+ logger.info('User-Agent saved.')
exit(0)
-
- if args.language is not None:
+ elif args.language is not None:
constant.CONFIG['language'] = args.language
- logger.info('Default language now set to \'{0}\''.format(args.language))
write_config()
+ logger.info('Default language now set to \'{0}\''.format(args.language))
exit(0)
# TODO: search without language
- if args.proxy:
+ if args.proxy is not None:
proxy_url = urlparse(args.proxy)
- if not args.proxy == '' and proxy_url.scheme not in ('http', 'https'):
+ if not args.proxy == '' and proxy_url.scheme not in ('http', 'https', 'socks5', 'socks5h', 'socks4', 'socks4a'):
logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
exit(0)
else:
write_config()
exit(0)
- if args.viewer_template:
+ if args.viewer_template is not None:
if not args.viewer_template:
args.viewer_template = 'default'
parser.print_help()
exit(1)
- if not args.keyword and not args.id and not args.favorites:
+ if not args.keyword and not args.id and not args.favorites:
parser.print_help()
exit(1)
logger.critical('Maximum number of used threads is 15')
exit(1)
+ if args.dryrun and (args.is_cbz or args.is_pdf):
+ logger.critical('Cannot generate PDF or CBZ during dry-run')
+ exit(1)
+
return args