2 from __future__ import print_function, unicode_literals
4 from optparse import OptionParser
6 from itertools import ifilter as filter
10 import nhentai.constant as constant
11 from nhentai.utils import urlparse
12 from nhentai.logger import logger
16 logger.info('''nHentai: あなたも変態。 いいね?
18 _ __ | | | | ___ _ __ | |_ __ _(_)
19 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
20 | | | | _ | __/ | | | || (_| | |
21 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
26 parser = OptionParser()
27 parser.add_option('--download', dest='is_download', action='store_true', help='download doujinshi or not')
28 parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information.')
29 parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
30 parser.add_option('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
31 parser.add_option('--page', type='int', dest='page', action='store', default=1,
32 help='page number of search result')
33 parser.add_option('--tags', type='string', dest='tags', action='store', help='download doujinshi by tags')
34 parser.add_option('--output', type='string', dest='output_dir', action='store', default='',
36 parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
37 help='thread count of download doujinshi')
38 parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
39 help='timeout of download doujinshi')
40 parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
41 help='use proxy, example: http://127.0.0.1:1080')
44 sys.argv = list(map(lambda x: unicode(x.decode('utf-8')), sys.argv))
45 except (NameError, TypeError):
48 args, _ = parser.parse_args(sys.argv[1:])
51 logger.warning('`--tags` is under construction')
55 _ = map(lambda id: id.strip(), args.id.split(','))
56 args.id = set(map(int, filter(lambda id: id.isdigit(), _)))
58 if (args.is_download or args.is_show) and not args.id and not args.keyword:
59 logger.critical('Doujinshi id(s) are required for downloading')
63 if not args.keyword and not args.id:
70 elif args.threads > 10:
71 logger.critical('Maximum number of used threads is 10')
75 proxy_url = urlparse(args.proxy)
76 if proxy_url.scheme not in ('http', 'https'):
77 logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
79 constant.PROXY = {proxy_url.scheme: args.proxy}