6 from optparse import OptionParser
9 from itertools import ifilter as filter
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
20 logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
22 _ __ | | | | ___ _ __ | |_ __ _(_)
23 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
24 | | | | _ | __/ | | | || (_| | |
25 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
30 if not os.path.exists(constant.NHENTAI_CONFIG_FILE):
34 with open(constant.NHENTAI_CONFIG_FILE, 'r') as f:
35 constant.CONFIG.update(json.load(f))
36 except json.JSONDecodeError:
37 logger.error('Failed to load config file.')
42 if not os.path.exists(constant.NHENTAI_HOME):
43 os.mkdir(constant.NHENTAI_HOME)
45 with open(constant.NHENTAI_CONFIG_FILE, 'w') as f:
46 f.write(json.dumps(constant.CONFIG))
52 parser = OptionParser('\n nhentai --search [keyword] --download'
53 '\n NHENTAI=http://h.loli.club nhentai --id [ID ...]'
54 '\n nhentai --file [filename]'
55 '\n\nEnvironment Variable:\n'
56 ' NHENTAI nhentai mirror url')
58 parser.add_option('--download', '-D', dest='is_download', action='store_true',
59 help='download doujinshi (for search results)')
60 parser.add_option('--show', '-S', dest='is_show', action='store_true', help='just show the doujinshi information')
63 parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
64 parser.add_option('--search', '-s', type='string', dest='keyword', action='store',
65 help='search doujinshi by keyword')
66 parser.add_option('--favorites', '-F', action='store_true', dest='favorites',
67 help='list or download your favorites.')
70 parser.add_option('--page-all', dest='page_all', action='store_true', default=False,
71 help='all search results')
72 parser.add_option('--page', '--page-range', type='string', dest='page', action='store', default='',
73 help='page number of search results. e.g. 1,2-5,14')
74 parser.add_option('--sorting', dest='sorting', action='store', default='recent',
75 help='sorting of doujinshi (recent / popular / popular-[today|week])',
76 choices=['recent', 'popular', 'popular-today', 'popular-week'])
79 parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='./',
81 parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
82 help='thread count for downloading doujinshi')
83 parser.add_option('--timeout', '-T', type='int', dest='timeout', action='store', default=30,
84 help='timeout for downloading doujinshi')
85 parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0,
86 help='slow down between downloading every doujinshi')
87 parser.add_option('--proxy', type='string', dest='proxy', action='store',
88 help='store a proxy, for example: -p \'http://127.0.0.1:1080\'')
89 parser.add_option('--file', '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
90 parser.add_option('--format', type='string', dest='name_format', action='store',
91 help='format the saved folder name', default='[%i][%a][%t]')
92 parser.add_option('--dry-run', '-r', action='store_true', dest='dryrun', help='Dry run, skip file download.')
95 parser.add_option('--html', dest='html_viewer', action='store_true',
96 help='generate a html viewer at current directory')
97 parser.add_option('--no-html', dest='is_nohtml', action='store_true',
98 help='don\'t generate HTML after downloading')
99 parser.add_option('--gen-main', dest='main_viewer', action='store_true',
100 help='generate a main viewer contain all the doujin in the folder')
101 parser.add_option('--cbz', '-C', dest='is_cbz', action='store_true',
102 help='generate Comic Book CBZ File')
103 parser.add_option('--pdf', '-P', dest='is_pdf', action='store_true',
104 help='generate PDF file')
105 parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
106 help='remove downloaded doujinshi dir when generated CBZ or PDF file.')
107 parser.add_option('--meta', dest='generate_metadata', action='store_true',
108 help='generate a metadata file in doujinshi format')
109 parser.add_option('--regenerate-cbz', dest='regenerate_cbz', action='store_true', default=False,
110 help='regenerate the cbz file if exists')
113 parser.add_option('--cookie', type='str', dest='cookie', action='store',
114 help='set cookie of nhentai to bypass Cloudflare captcha')
115 parser.add_option('--useragent', type='str', dest='useragent', action='store',
116 help='set useragent to bypass Cloudflare captcha')
117 parser.add_option('--language', type='str', dest='language', action='store',
118 help='set default language to parse doujinshis')
119 parser.add_option('--clean-language', dest='clean_language', action='store_true', default=False,
120 help='set DEFAULT as language to parse doujinshis')
121 parser.add_option('--save-download-history', dest='is_save_download_history', action='store_true',
122 default=False, help='save downloaded doujinshis, whose will be skipped if you re-download them')
123 parser.add_option('--clean-download-history', action='store_true', default=False, dest='clean_download_history',
124 help='clean download history')
125 parser.add_option('--template', dest='viewer_template', action='store',
126 help='set viewer template', default='')
129 sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
130 except (NameError, TypeError):
132 except UnicodeDecodeError:
135 args, _ = parser.parse_args(sys.argv[1:])
138 generate_html(template=constant.CONFIG['template'])
141 if args.main_viewer and not args.id and not args.keyword and not args.favorites:
145 if args.clean_download_history:
149 logger.info('Download history cleaned.')
153 if args.cookie is not None:
154 constant.CONFIG['cookie'] = args.cookie
156 logger.info('Cookie saved.')
158 elif args.useragent is not None:
159 constant.CONFIG['useragent'] = args.useragent
161 logger.info('Useragent saved.')
163 elif args.language is not None:
164 constant.CONFIG['language'] = args.language
166 logger.info('Default language now set to \'{0}\''.format(args.language))
168 # TODO: search without language
170 if args.proxy is not None:
171 proxy_url = urlparse(args.proxy)
172 if not args.proxy == '' and proxy_url.scheme not in ('http', 'https', 'socks5', 'socks5h', 'socks4', 'socks4a'):
173 logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
176 constant.CONFIG['proxy'] = {
180 logger.info('Proxy now set to \'{0}\'.'.format(args.proxy))
184 if args.viewer_template is not None:
185 if not args.viewer_template:
186 args.viewer_template = 'default'
188 if not os.path.exists(os.path.join(os.path.dirname(__file__),
189 'viewer/{}/index.html'.format(args.viewer_template))):
190 logger.error('Template \'{}\' does not exists'.format(args.viewer_template))
193 constant.CONFIG['template'] = args.viewer_template
196 # --- end set config ---
199 if not constant.CONFIG['cookie']:
200 logger.warning('Cookie has not been set, please use `nhentai --cookie \'COOKIE\'` to set it.')
204 _ = [i.strip() for i in args.id.split(',')]
205 args.id = set(int(i) for i in _ if i.isdigit())
208 with open(args.file, 'r') as f:
209 _ = [i.strip() for i in f.readlines()]
210 args.id = set(int(i) for i in _ if i.isdigit())
212 if (args.is_download or args.is_show) and not args.id and not args.keyword and not args.favorites:
213 logger.critical('Doujinshi id(s) are required for downloading')
217 if not args.keyword and not args.id and not args.favorites:
221 if args.threads <= 0:
224 elif args.threads > 15:
225 logger.critical('Maximum number of used threads is 15')
228 if args.dryrun and (args.is_cbz or args.is_pdf):
229 logger.critical('Cannot generate PDF or CBZ during dry-run')