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