2 from __future__ import print_function
4 from optparse import OptionParser
6 from itertools import ifilter as filter
10 import nhentai.constant as constant
11 from nhentai.utils import urlparse, generate_html
12 from nhentai.logger import logger
16 sys.setdefaultencoding(sys.stdin.encoding)
23 logger.info(u'''nHentai: あなたも変態。 いいね?
25 _ __ | | | | ___ _ __ | |_ __ _(_)
26 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
27 | | | | _ | __/ | | | || (_| | |
28 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
33 parser = OptionParser('\n nhentai --search [keyword] --download'
34 '\n NHENTAI=http://h.loli.club nhentai --id [ID ...]'
35 '\n\nEnvironment Variable:\n'
36 ' NHENTAI nhentai mirror url')
37 parser.add_option('--download', dest='is_download', action='store_true',
38 help='download doujinshi (for search result)')
39 parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information')
40 parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
41 parser.add_option('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
42 parser.add_option('--page', type='int', dest='page', action='store', default=1,
43 help='page number of search result')
44 parser.add_option('--tags', type='string', dest='tags', action='store', help='download doujinshi by tags')
45 parser.add_option('--output', type='string', dest='output_dir', action='store', default='',
47 parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
48 help='thread count of download doujinshi')
49 parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
50 help='timeout of download doujinshi')
51 parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
52 help='use proxy, example: http://127.0.0.1:1080')
53 parser.add_option('--html', dest='html_viewer', action='store_true',
54 help='generate a html viewer at current directory')
56 parser.add_option('--login', '-l', type='str', dest='login', action='store',
57 help='username:password pair of nhentai account')
59 parser.add_option('--nohtml', dest='is_nohtml', action='store_true',
60 help='Don\'t generate HTML')
62 parser.add_option('--cbz', dest='is_cbz', action='store_true',
63 help='Generate Comic Book CBZ File')
66 sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
67 except (NameError, TypeError):
69 except UnicodeDecodeError:
72 args, _ = parser.parse_args(sys.argv[1:])
80 _, _ = args.login.split(':', 1)
82 logger.error('Invalid `username:password` pair.')
85 if not args.is_download:
86 logger.warning('YOU DO NOT SPECIFY `--download` OPTION !!!')
89 logger.warning('`--tags` is under construction')
93 _ = map(lambda id: id.strip(), args.id.split(','))
94 args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
96 if (args.is_download or args.is_show) and not args.id and not args.keyword and not args.login:
97 logger.critical('Doujinshi id(s) are required for downloading')
101 if not args.keyword and not args.id and not args.login:
105 if args.threads <= 0:
108 elif args.threads > 15:
109 logger.critical('Maximum number of used threads is 15')
113 proxy_url = urlparse(args.proxy)
114 if proxy_url.scheme not in ('http', 'https'):
115 logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
117 constant.PROXY = {'http': args.proxy, 'https': args.proxy}