2 from __future__ import print_function
4 from optparse import OptionParser
5 from nhentai import __version__
7 from itertools import ifilter as filter
11 import nhentai.constant as constant
12 from nhentai.utils import urlparse, generate_html
13 from nhentai.logger import logger
16 if sys.version_info < (3, 0, 0):
19 sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
20 sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)
28 logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
30 _ __ | | | | ___ _ __ | |_ __ _(_)
31 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
32 | | | | _ | __/ | | | || (_| | |
33 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
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='',
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')
64 parser.add_option('--login', '-l', type='str', dest='login', action='store',
65 help='username:password pair of nhentai account')
67 parser.add_option('--nohtml', dest='is_nohtml', action='store_true',
68 help='Don\'t generate HTML')
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.')
77 sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
78 except (NameError, TypeError):
80 except UnicodeDecodeError:
83 args, _ = parser.parse_args(sys.argv[1:])
91 _, _ = args.login.split(':', 1)
93 logger.error('Invalid `username:password` pair.')
96 if not args.is_download:
97 logger.warning('YOU DO NOT SPECIFY `--download` OPTION !!!')
100 _ = map(lambda id: id.strip(), args.id.split(','))
101 args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
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(), _)))
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')
114 if not args.keyword and not args.id and not args.login and not args.tag:
118 if args.threads <= 0:
121 elif args.threads > 15:
122 logger.critical('Maximum number of used threads is 15')
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))
130 constant.PROXY = {'http': args.proxy, 'https': args.proxy}