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
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', help='download doujinshi (for search result)')
38 parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information')
39 parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
40 parser.add_option('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
41 parser.add_option('--page', type='int', dest='page', action='store', default=1,
42 help='page number of search result')
43 parser.add_option('--tags', type='string', dest='tags', action='store', help='download doujinshi by tags')
44 parser.add_option('--output', type='string', dest='output_dir', action='store', default='',
46 parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
47 help='thread count of download doujinshi')
48 parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
49 help='timeout of download doujinshi')
50 parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
51 help='use proxy, example: http://127.0.0.1:1080')
54 sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
55 except (NameError, TypeError):
57 except UnicodeDecodeError:
60 args, _ = parser.parse_args(sys.argv[1:])
63 logger.warning('`--tags` is under construction')
67 _ = map(lambda id: id.strip(), args.id.split(','))
68 args.id = set(map(int, filter(lambda id: id.isdigit(), _)))
70 if (args.is_download or args.is_show) and not args.id and not args.keyword:
71 logger.critical('Doujinshi id(s) are required for downloading')
75 if not args.keyword and not args.id:
82 elif args.threads > 15:
83 logger.critical('Maximum number of used threads is 15')
87 proxy_url = urlparse(args.proxy)
88 if proxy_url.scheme not in ('http', 'https'):
89 logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
91 constant.PROXY = {proxy_url.scheme: args.proxy}