]> git.lizzy.rs Git - nhentai.git/blob - nhentai/cmdline.py
Merge pull request #21 from mentaterasmus/master
[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, generate_html
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',
38                       help='download doujinshi (for search result)')
39     parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information')
40     parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
41     parser.add_option('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
42     parser.add_option('--page', type='int', dest='page', action='store', default=1,
43                       help='page number of search result')
44     parser.add_option('--tags', type='string', dest='tags', action='store', help='download doujinshi by tags')
45     parser.add_option('--output', type='string', dest='output_dir', action='store', default='',
46                       help='output dir')
47     parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
48                       help='thread count of download doujinshi')
49     parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
50                       help='timeout of download doujinshi')
51     parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
52                       help='use proxy, example: http://127.0.0.1:1080')
53     parser.add_option('--html', dest='html_viewer', action='store_true',
54                       help='generate a html viewer at current directory')
55
56     parser.add_option('--login', '-l', type='str', dest='login', action='store',
57                       help='username:password pair of nhentai account')
58
59     parser.add_option('--nohtml', dest='is_nohtml', action='store_true',
60                       help='Don\'t generate HTML')
61
62     parser.add_option('--cbz', dest='is_cbz', action='store_true',
63                       help='Generate Comic Book CBZ File')                      
64                       
65     try:
66         sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
67     except (NameError, TypeError):
68         pass
69     except UnicodeDecodeError:
70         exit(0)
71
72     args, _ = parser.parse_args(sys.argv[1:])
73
74     if args.html_viewer:
75         generate_html()
76         exit(0)
77
78     if args.login:
79         try:
80             _, _ = args.login.split(':', 1)
81         except ValueError:
82             logger.error('Invalid `username:password` pair.')
83             exit(1)
84
85         if not args.is_download:
86             logger.warning('YOU DO NOT SPECIFY `--download` OPTION !!!')
87
88     if args.tags:
89         logger.warning('`--tags` is under construction')
90         exit(1)
91
92     if args.id:
93         _ = map(lambda id: id.strip(), args.id.split(','))
94         args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
95
96     if (args.is_download or args.is_show) and not args.id and not args.keyword and not args.login:
97         logger.critical('Doujinshi id(s) are required for downloading')
98         parser.print_help()
99         exit(1)
100
101     if not args.keyword and not args.id and not args.login:
102         parser.print_help()
103         exit(1)
104
105     if args.threads <= 0:
106         args.threads = 1
107
108     elif args.threads > 15:
109         logger.critical('Maximum number of used threads is 15')
110         exit(1)
111
112     if args.proxy:
113         proxy_url = urlparse(args.proxy)
114         if proxy_url.scheme not in ('http', 'https'):
115             logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
116         else:
117             constant.PROXY = {'http': args.proxy, 'https': args.proxy}
118
119     return args