2 from __future__ import print_function
4 from optparse import OptionParser
5 from nhentai import __version__
7 from itertools import ifilter as filter
11 import nhentai.constant as constant
12 from nhentai.utils import urlparse, generate_html
13 from nhentai.logger import logger
16 if sys.version_info < (3, 0, 0):
19 sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
20 sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)
28 logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
30 _ __ | | | | ___ _ __ | |_ __ _(_)
31 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
32 | | | | _ | __/ | | | || (_| | |
33 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
38 parser = OptionParser('\n nhentai --search [keyword] --download'
39 '\n NHENTAI=http://h.loli.club nhentai --id [ID ...]'
40 '\n\nEnvironment Variable:\n'
41 ' NHENTAI nhentai mirror url')
42 parser.add_option('--download', dest='is_download', action='store_true',
43 help='download doujinshi (for search result)')
44 parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information')
45 parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
46 parser.add_option('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
47 parser.add_option('--page', type='int', dest='page', action='store', default=1,
48 help='page number of search result')
49 parser.add_option('--tag', type='string', dest='tag', action='store', help='download doujinshi by tag')
50 parser.add_option('--output', type='string', dest='output_dir', action='store', default='',
52 parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
53 help='thread count of download doujinshi')
54 parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
55 help='timeout of download doujinshi')
56 parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
57 help='use proxy, example: http://127.0.0.1:1080')
58 parser.add_option('--html', dest='html_viewer', action='store_true',
59 help='generate a html viewer at current directory')
61 parser.add_option('--login', '-l', type='str', dest='login', action='store',
62 help='username:password pair of nhentai account')
64 parser.add_option('--nohtml', dest='is_nohtml', action='store_true',
65 help='Don\'t generate HTML')
67 parser.add_option('--cbz', dest='is_cbz', action='store_true',
68 help='Generate Comic Book CBZ File')
71 sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
72 except (NameError, TypeError):
74 except UnicodeDecodeError:
77 args, _ = parser.parse_args(sys.argv[1:])
85 _, _ = args.login.split(':', 1)
87 logger.error('Invalid `username:password` pair.')
90 if not args.is_download:
91 logger.warning('YOU DO NOT SPECIFY `--download` OPTION !!!')
94 _ = map(lambda id: id.strip(), args.id.split(','))
95 args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
97 if (args.is_download or args.is_show) and not args.id and not args.keyword and \
98 not args.login and not args.tag:
99 logger.critical('Doujinshi id(s) are required for downloading')
103 if not args.keyword and not args.id and not args.login and not args.tag:
107 if args.threads <= 0:
110 elif args.threads > 15:
111 logger.critical('Maximum number of used threads is 15')
115 proxy_url = urlparse(args.proxy)
116 if proxy_url.scheme not in ('http', 'https'):
117 logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
119 constant.PROXY = {'http': args.proxy, 'https': args.proxy}