]> git.lizzy.rs Git - nhentai.git/blob - nhentai/cmdline.py
fix timeout
[nhentai.git] / nhentai / cmdline.py
1 # coding: utf-8
2 from __future__ import print_function
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 try:
15     reload(sys)
16     sys.setdefaultencoding(sys.stdin.encoding)
17 except NameError:
18     # python3
19     pass
20
21
22 def banner():
23     logger.info(u'''nHentai: あなたも変態。 いいね?
24        _   _            _        _
25  _ __ | | | | ___ _ __ | |_ __ _(_)
26 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
27 | | | |  _  |  __/ | | | || (_| | |
28 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
29 ''')
30
31
32 def cmd_parser():
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='',
45                       help='output dir')
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')
52
53     try:
54         sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
55     except (NameError, TypeError):
56         pass
57     except UnicodeDecodeError:
58         exit(0)
59
60     args, _ = parser.parse_args(sys.argv[1:])
61
62     if args.tags:
63         logger.warning('`--tags` is under construction')
64         exit(0)
65
66     if args.id:
67         _ = map(lambda id: id.strip(), args.id.split(','))
68         args.id = set(map(int, filter(lambda id: id.isdigit(), _)))
69
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')
72         parser.print_help()
73         exit(0)
74
75     if not args.keyword and not args.id:
76         parser.print_help()
77         exit(0)
78
79     if args.threads <= 0:
80         args.threads = 1
81
82     elif args.threads > 15:
83         logger.critical('Maximum number of used threads is 15')
84         exit(0)
85
86     if args.proxy:
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))
90         else:
91             constant.PROXY = {proxy_url.scheme: args.proxy}
92
93     return args