]> git.lizzy.rs Git - nhentai.git/blob - nhentai/cmdline.py
feature: proxy support
[nhentai.git] / nhentai / cmdline.py
1 # coding: utf-8
2 from __future__ import print_function
3 from optparse import OptionParser
4 from logger import logger
5 try:
6     from itertools import ifilter as filter
7 except ImportError:
8     pass
9
10
11 import constant
12
13
14 def banner():
15     print('''       _   _            _        _
16  _ __ | | | | ___ _ __ | |_ __ _(_)
17 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
18 | | | |  _  |  __/ | | | || (_| | |
19 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
20 ''')
21
22
23 def cmd_parser():
24     parser = OptionParser()
25     parser.add_option('--download', dest='is_download', action='store_true', help='download doujinshi or not')
26     parser.add_option('--id', type='int', dest='id', action='store', help='doujinshi id of nhentai')
27     parser.add_option('--ids', type='str', dest='ids', action='store', help='doujinshi id set, e.g. 1,2,3')
28     parser.add_option('--search', type='string', dest='keyword', action='store', help='keyword searched')
29     parser.add_option('--page', type='int', dest='page', action='store', default=1,
30                       help='page number of search result')
31     parser.add_option('--path', type='string', dest='saved_path', action='store', default='',
32                       help='path which save the doujinshi')
33     parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
34                       help='thread count of download doujinshi')
35     parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
36                       help='timeout of download doujinshi')
37     parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
38                       help='use proxy, example: socks5://127.0.0.1:1080')
39     args, _ = parser.parse_args()
40
41     if args.ids:
42         _ = map(lambda id: id.strip(), args.ids.split(','))
43         args.ids = set(map(int, filter(lambda id: id.isdigit(), _)))
44
45     if args.is_download and not args.id and not args.ids and not args.keyword:
46         logger.critical('Doujinshi id/ids is required for downloading')
47         parser.print_help()
48         raise SystemExit
49
50     if args.id:
51         args.ids = (args.id, ) if not args.ids else args.ids
52
53     if not args.keyword and not args.ids:
54         parser.print_help()
55         raise SystemExit
56
57     if args.threads <= 0:
58         args.threads = 1
59     elif args.threads > 10:
60         logger.critical('Maximum number of used threads is 10')
61         raise SystemExit
62
63     if args.proxy:
64         import urlparse
65         proxy_url = urlparse.urlparse(args.proxy)
66         if proxy_url.scheme not in ('http', 'https'):
67             logger.error('Invalid protocol \'{}\' of proxy, ignored'.format(proxy_url.scheme))
68         else:
69             constant.PROXY = {proxy_url.scheme: args.proxy}
70
71     return args