]> git.lizzy.rs Git - nhentai.git/blob - nhentai/cmdline.py
0.2.1
[nhentai.git] / nhentai / cmdline.py
1 # coding: utf-8
2 from __future__ import print_function, unicode_literals
3 import sys
4 from optparse import OptionParser
5 try:
6     from itertools import ifilter as filter
7 except ImportError:
8     pass
9
10 import nhentai.constant as constant
11 from nhentai.utils import urlparse
12 from nhentai.logger import logger
13
14
15 def banner():
16     logger.info('''nHentai: あなたも変態。 いいね?
17        _   _            _        _
18  _ __ | | | | ___ _ __ | |_ __ _(_)
19 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
20 | | | |  _  |  __/ | | | || (_| | |
21 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
22 ''')
23
24
25 def cmd_parser():
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='',
35                       help='output dir')
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')
42
43     try:
44         sys.argv = list(map(lambda x: unicode(x.decode('utf-8')), sys.argv))
45     except (NameError, TypeError):
46         pass
47
48     args, _ = parser.parse_args(sys.argv[1:])
49
50     if args.tags:
51         logger.warning('`--tags` is under construction')
52         exit(0)
53
54     if args.id:
55         _ = map(lambda id: id.strip(), args.id.split(','))
56         args.id = set(map(int, filter(lambda id: id.isdigit(), _)))
57
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')
60         parser.print_help()
61         exit(0)
62
63     if not args.keyword and not args.id:
64         parser.print_help()
65         exit(0)
66
67     if args.threads <= 0:
68         args.threads = 1
69
70     elif args.threads > 10:
71         logger.critical('Maximum number of used threads is 10')
72         exit(0)
73
74     if args.proxy:
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))
78         else:
79             constant.PROXY = {proxy_url.scheme: args.proxy}
80
81     return args