]> git.lizzy.rs Git - nhentai.git/blob - nhentai/cmdline.py
e0dc84f41e3b9fe3e4c2607f34850bb13935ea65
[nhentai.git] / nhentai / cmdline.py
1 # coding: utf-8
2 from __future__ import print_function
3 import sys
4 from optparse import OptionParser
5 from nhentai import __version__
6 try:
7     from itertools import ifilter as filter
8 except ImportError:
9     pass
10
11 import nhentai.constant as constant
12 from nhentai.utils import urlparse, generate_html
13 from nhentai.logger import logger
14
15 try:
16     if sys.version_info < (3, 0, 0):
17         import codecs
18         import locale
19         sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
20         sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)
21
22 except NameError:
23     # python3
24     pass
25
26
27 def banner():
28     logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
29        _   _            _        _
30  _ __ | | | | ___ _ __ | |_ __ _(_)
31 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
32 | | | |  _  |  __/ | | | || (_| | |
33 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
34 ''' % __version__)
35
36
37 def cmd_parser():
38     parser = OptionParser('\n  nhentai --search [keyword] --download'
39                           '\n  NHENTAI=http://h.loli.club nhentai --id [ID ...]'
40                           '\n  nhentai --file [filename]'    
41                           '\n\nEnvironment Variable:\n'
42                           '  NHENTAI                 nhentai mirror url')
43     parser.add_option('--download', dest='is_download', action='store_true',
44                       help='download doujinshi (for search results)')
45     parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information')
46     parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
47     parser.add_option('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
48     parser.add_option('--page', type='int', dest='page', action='store', default=1,
49                       help='page number of search results')
50     parser.add_option('--tag', type='string', dest='tag', action='store', help='download doujinshi by tag')
51     parser.add_option('--max-page', type='int', dest='max_page', action='store', default=1,
52                       help='The max page when recursive download tagged doujinshi')
53     parser.add_option('--output', type='string', dest='output_dir', action='store', default='',
54                       help='output dir')
55     parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
56                       help='thread count for downloading doujinshi')
57     parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
58                       help='timeout for downloading doujinshi')
59     parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
60                       help='uses a proxy, for example: http://127.0.0.1:1080')
61     parser.add_option('--html', dest='html_viewer', action='store_true',
62                       help='generate a html viewer at current directory')
63
64     parser.add_option('--login', '-l', type='str', dest='login', action='store',
65                       help='username:password pair of nhentai account')
66
67     parser.add_option('--nohtml', dest='is_nohtml', action='store_true',
68                       help='Don\'t generate HTML')
69
70     parser.add_option('--cbz', dest='is_cbz', action='store_true',
71                       help='Generate Comic Book CBZ File')
72     parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
73                       help='Remove downloaded doujinshi dir when generated CBZ file.')
74     parser.add_option('--file',  '-f', type='string', dest='file', action='store', help='Read gallery IDs from file.')
75
76     try:
77         sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
78     except (NameError, TypeError):
79         pass
80     except UnicodeDecodeError:
81         exit(0)
82
83     args, _ = parser.parse_args(sys.argv[1:])
84
85     if args.html_viewer:
86         generate_html()
87         exit(0)
88
89     if args.login:
90         try:
91             _, _ = args.login.split(':', 1)
92         except ValueError:
93             logger.error('Invalid `username:password` pair.')
94             exit(1)
95
96         if not args.is_download:
97             logger.warning('YOU DO NOT SPECIFY `--download` OPTION !!!')
98
99     if args.id:
100         _ = map(lambda id: id.strip(), args.id.split(','))
101         args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
102
103     if args.file:
104         with open(args.file, 'r') as f:
105             _ = map(lambda id: id.strip(), f.readlines())
106             args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
107
108     if (args.is_download or args.is_show) and not args.id and not args.keyword and \
109             not args.login and not args.tag:
110         logger.critical('Doujinshi id(s) are required for downloading')
111         parser.print_help()
112         exit(1)
113
114     if not args.keyword and not args.id and not args.login and not args.tag:
115         parser.print_help()
116         exit(1)
117
118     if args.threads <= 0:
119         args.threads = 1
120
121     elif args.threads > 15:
122         logger.critical('Maximum number of used threads is 15')
123         exit(1)
124
125     if args.proxy:
126         proxy_url = urlparse(args.proxy)
127         if proxy_url.scheme not in ('http', 'https'):
128             logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
129         else:
130             constant.PROXY = {'http': args.proxy, 'https': args.proxy}
131
132     return args