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