2 from __future__ import print_function
5 from optparse import OptionParser
7 from itertools import ifilter as filter
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
17 if sys.version_info < (3, 0, 0):
20 sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
21 sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)
29 logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
31 _ __ | | | | ___ _ __ | |_ __ _(_)
32 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
33 | | | | _ | __/ | | | || (_| | |
34 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
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')
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')
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.')
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'])
66 parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='',
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]')
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.')
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')
107 sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
109 except (NameError, TypeError):
111 except UnicodeDecodeError:
114 args, _ = parser.parse_args(sys.argv[1:])
120 if args.main_viewer and not args.id and not args.keyword and not args.favorites:
124 if args.clean_download_history:
128 logger.info('Download history cleaned.')
131 if os.path.exists(constant.NHENTAI_COOKIE):
132 with open(constant.NHENTAI_COOKIE, 'r') as f:
133 constant.COOKIE = f.read()
137 if not os.path.exists(constant.NHENTAI_HOME):
138 os.mkdir(constant.NHENTAI_HOME)
140 with open(constant.NHENTAI_COOKIE, 'w') as f:
142 except Exception as e:
143 logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
146 logger.info('Cookie saved.')
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()
156 if not os.path.exists(constant.NHENTAI_HOME):
157 os.mkdir(constant.NHENTAI_HOME)
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)))
165 logger.info('Default language now is {}.'.format(args.language))
168 if args.clean_language:
170 if not os.path.exists(constant.NHENTAI_HOME):
171 os.mkdir(constant.NHENTAI_HOME)
173 with open(constant.NHENTAI_LANGUAGE, 'w') as f:
175 except Exception as e:
176 logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
179 logger.info('Language now is DEFAULT')
182 if os.path.exists(constant.NHENTAI_PROXY):
183 with open(constant.NHENTAI_PROXY, 'r') as f:
185 constant.PROXY = {'http': link, 'https': link}
189 if not os.path.exists(constant.NHENTAI_HOME):
190 os.mkdir(constant.NHENTAI_HOME)
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))
196 with open(constant.NHENTAI_PROXY, 'w') as f:
199 except Exception as e:
200 logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
203 logger.info('Proxy \'{0}\' saved.'.format(args.proxy))
207 if not constant.COOKIE:
208 logger.warning('Cookie has not been set, please use `nhentai --cookie \'COOKIE\'` to set it.')
212 _ = [i.strip() for i in args.id.split(',')]
213 args.id = set(int(i) for i in _ if i.isdigit())
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())
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')
225 if not args.keyword and not args.id and not args.favorites:
229 if args.threads <= 0:
232 elif args.threads > 15:
233 logger.critical('Maximum number of used threads is 15')