]> git.lizzy.rs Git - nhentai.git/blob - nhentai/cmdline.py
39787cd6f28a2123f25cc3c2ba5f05e7948f62d7
[nhentai.git] / nhentai / cmdline.py
1 # coding: utf-8
2 from __future__ import print_function
3 import os
4 import sys
5 from optparse import OptionParser
6 try:
7     from itertools import ifilter as filter
8 except ImportError:
9     pass
10
11 import nhentai.constant as constant
12 from nhentai import __version__
13 from nhentai.utils import urlparse, generate_html, generate_main_html, DB
14 from nhentai.logger import logger
15
16 try:
17     if sys.version_info < (3, 0, 0):
18         import codecs
19         import locale
20         sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
21         sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)
22
23 except NameError:
24     # python3
25     pass
26
27
28 def banner():
29     logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
30        _   _            _        _
31  _ __ | | | | ___ _ __ | |_ __ _(_)
32 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
33 | | | |  _  |  __/ | | | || (_| | |
34 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
35 ''' % __version__)
36
37
38 def cmd_parser():
39     parser = OptionParser('\n  nhentai --search [keyword] --download'
40                           '\n  NHENTAI=http://h.loli.club nhentai --id [ID ...]'
41                           '\n  nhentai --file [filename]'
42                           '\n\nEnvironment Variable:\n'
43                           '  NHENTAI                 nhentai mirror url')
44     # operation options
45     parser.add_option('--download', '-D', dest='is_download', action='store_true',
46                       help='download doujinshi (for search results)')
47     parser.add_option('--show', '-S', dest='is_show', action='store_true', help='just show the doujinshi information')
48
49     # doujinshi options
50     parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
51     parser.add_option('--search', '-s', type='string', dest='keyword', action='store',
52                       help='search doujinshi by keyword')
53     parser.add_option('--favorites', '-F', action='store_true', dest='favorites',
54                       help='list or download your favorites.')
55
56     # page options
57     parser.add_option('--page-all', dest='page_all', action='store_true', default=False,
58                       help='all search results')
59     parser.add_option('--page', '--page-range', type='string', dest='page', action='store', default='',
60                       help='page number of search results. e.g. 1,2-5,14')
61     parser.add_option('--sorting', dest='sorting', action='store', default='recent',
62                       help='sorting of doujinshi (recent / popular / popular-[today|week])',
63                       choices=['recent', 'popular', 'popular-today', 'popular-week'])
64
65     # download options
66     parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='',
67                       help='output dir')
68     parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
69                       help='thread count for downloading doujinshi')
70     parser.add_option('--timeout', '-T', type='int', dest='timeout', action='store', default=30,
71                       help='timeout for downloading doujinshi')
72     parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0,
73                       help='slow down between downloading every doujinshi')
74     parser.add_option('--proxy', '-p', type='string', dest='proxy', action='store', default='',
75                       help='store a proxy, for example: -p \'http://127.0.0.1:1080\'')
76     parser.add_option('--file',  '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
77     parser.add_option('--format', type='string', dest='name_format', action='store',
78                       help='format the saved folder name', default='[%i][%a][%t]')
79
80     # generate options
81     parser.add_option('--html', dest='html_viewer', action='store_true',
82                       help='generate a html viewer at current directory')
83     parser.add_option('--no-html', dest='is_nohtml', action='store_true',
84                       help='don\'t generate HTML after downloading')
85     parser.add_option('--gen-main', dest='main_viewer', action='store_true',
86                       help='generate a main viewer contain all the doujin in the folder')
87     parser.add_option('--cbz', '-C', dest='is_cbz', action='store_true',
88                       help='generate Comic Book CBZ File')
89     parser.add_option('--pdf', '-P', dest='is_pdf', action='store_true',
90                       help='generate PDF file')
91     parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
92                       help='remove downloaded doujinshi dir when generated CBZ or PDF file.')
93
94     # nhentai options
95     parser.add_option('--cookie', type='str', dest='cookie', action='store',
96                       help='set cookie of nhentai to bypass Google recaptcha')
97     parser.add_option('--language', type='str', dest='language', action='store',
98                       help='set default language to parse doujinshis')
99     parser.add_option('--clean-language', dest='clean_language', action='store_true', default=False,
100                       help='set DEFAULT as language to parse doujinshis')
101     parser.add_option('--save-download-history', dest='is_save_download_history', action='store_true',
102                       default=False, help='save downloaded doujinshis, whose will be skipped if you re-download them')
103     parser.add_option('--clean-download-history', action='store_true', default=False, dest='clean_download_history',
104                       help='clean download history')
105
106     try:
107         sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
108         print()
109     except (NameError, TypeError):
110         pass
111     except UnicodeDecodeError:
112         exit(0)
113
114     args, _ = parser.parse_args(sys.argv[1:])
115
116     if args.html_viewer:
117         generate_html()
118         exit(0)
119
120     if args.main_viewer and not args.id and not args.keyword and not args.favorites:
121         generate_main_html()
122         exit(0)
123
124     if args.clean_download_history:
125         with DB() as db:
126             db.clean_all()
127
128         logger.info('Download history cleaned.')
129         exit(0)
130
131     if os.path.exists(constant.NHENTAI_COOKIE):
132         with open(constant.NHENTAI_COOKIE, 'r') as f:
133             constant.COOKIE = f.read()
134
135     if args.cookie:
136         try:
137             if not os.path.exists(constant.NHENTAI_HOME):
138                 os.mkdir(constant.NHENTAI_HOME)
139
140             with open(constant.NHENTAI_COOKIE, 'w') as f:
141                 f.write(args.cookie)
142         except Exception as e:
143             logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
144             exit(1)
145
146         logger.info('Cookie saved.')
147         exit(0)
148
149     if os.path.exists(constant.NHENTAI_LANGUAGE) and not args.language:
150         with open(constant.NHENTAI_LANGUAGE, 'r') as f:
151             constant.LANGUAGE = f.read()
152             args.language = f.read()
153
154     if args.language:
155         try:
156             if not os.path.exists(constant.NHENTAI_HOME):
157                 os.mkdir(constant.NHENTAI_HOME)
158
159             with open(constant.NHENTAI_LANGUAGE, 'w') as f:
160                 f.write(args.language)
161         except Exception as e:
162             logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
163             exit(1)
164
165         logger.info('Default language now is {}.'.format(args.language))
166         exit(0)
167
168     if args.clean_language:
169         try:
170             if not os.path.exists(constant.NHENTAI_HOME):
171                 os.mkdir(constant.NHENTAI_HOME)
172
173             with open(constant.NHENTAI_LANGUAGE, 'w') as f:
174                 f.close()
175         except Exception as e:
176             logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
177             exit(1)
178
179         logger.info('Language now is DEFAULT')
180         exit(0)
181
182     if os.path.exists(constant.NHENTAI_PROXY):
183         with open(constant.NHENTAI_PROXY, 'r') as f:
184             link = f.read()
185             constant.PROXY = {'http': link, 'https': link}
186
187     if args.proxy:
188         try:
189             if not os.path.exists(constant.NHENTAI_HOME):
190                 os.mkdir(constant.NHENTAI_HOME)
191
192             proxy_url = urlparse(args.proxy)
193             if proxy_url.scheme not in ('http', 'https'):
194                 logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
195             else:
196                 with open(constant.NHENTAI_PROXY, 'w') as f:
197                     f.write(args.proxy)
198
199         except Exception as e:
200             logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
201             exit(1)
202
203         logger.info('Proxy \'{0}\' saved.'.format(args.proxy))
204         exit(0)
205
206     if args.favorites:
207         if not constant.COOKIE:
208             logger.warning('Cookie has not been set, please use `nhentai --cookie \'COOKIE\'` to set it.')
209             exit(1)
210
211     if args.id:
212         _ = [i.strip() for i in args.id.split(',')]
213         args.id = set(int(i) for i in _ if i.isdigit())
214
215     if args.file:
216         with open(args.file, 'r') as f:
217             _ = [i.strip() for i in f.readlines()]
218             args.id = set(int(i) for i in _ if i.isdigit())
219
220     if (args.is_download or args.is_show) and not args.id and not args.keyword and not args.favorites:
221         logger.critical('Doujinshi id(s) are required for downloading')
222         parser.print_help()
223         exit(1)
224
225     if not args.keyword and not args.id and not  args.favorites:
226         parser.print_help()
227         exit(1)
228
229     if args.threads <= 0:
230         args.threads = 1
231
232     elif args.threads > 15:
233         logger.critical('Maximum number of used threads is 15')
234         exit(1)
235
236     return args