]> git.lizzy.rs Git - nhentai.git/blob - nhentai/cmdline.py
fix unicode error on windows / python2
[nhentai.git] / nhentai / cmdline.py
1 # coding: utf-8
2 from __future__ import print_function
3 import sys
4 from optparse import OptionParser
5 from nhentai import __version__
6 try:
7     from itertools import ifilter as filter
8 except ImportError:
9     pass
10
11 import nhentai.constant as constant
12 from nhentai.utils import urlparse, generate_html
13 from nhentai.logger import logger
14
15 try:
16     if sys.version_info < (3, 0, 0):
17         import codecs
18         import locale
19         sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
20         sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)
21
22 except NameError:
23     # python3
24     pass
25
26
27 def banner():
28     logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
29        _   _            _        _
30  _ __ | | | | ___ _ __ | |_ __ _(_)
31 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
32 | | | |  _  |  __/ | | | || (_| | |
33 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
34 ''' % __version__)
35
36
37 def cmd_parser():
38     parser = OptionParser('\n  nhentai --search [keyword] --download'
39                           '\n  NHENTAI=http://h.loli.club nhentai --id [ID ...]'
40                           '\n\nEnvironment Variable:\n'
41                           '  NHENTAI                 nhentai mirror url')
42     parser.add_option('--download', dest='is_download', action='store_true',
43                       help='download doujinshi (for search result)')
44     parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information')
45     parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
46     parser.add_option('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
47     parser.add_option('--page', type='int', dest='page', action='store', default=1,
48                       help='page number of search result')
49     parser.add_option('--tag', type='string', dest='tag', action='store', help='download doujinshi by tag')
50     parser.add_option('--output', type='string', dest='output_dir', action='store', default='',
51                       help='output dir')
52     parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
53                       help='thread count of download doujinshi')
54     parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
55                       help='timeout of download doujinshi')
56     parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
57                       help='use proxy, example: http://127.0.0.1:1080')
58     parser.add_option('--html', dest='html_viewer', action='store_true',
59                       help='generate a html viewer at current directory')
60
61     parser.add_option('--login', '-l', type='str', dest='login', action='store',
62                       help='username:password pair of nhentai account')
63
64     parser.add_option('--nohtml', dest='is_nohtml', action='store_true',
65                       help='Don\'t generate HTML')
66
67     parser.add_option('--cbz', dest='is_cbz', action='store_true',
68                       help='Generate Comic Book CBZ File')                      
69                       
70     try:
71         sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
72     except (NameError, TypeError):
73         pass
74     except UnicodeDecodeError:
75         exit(0)
76
77     args, _ = parser.parse_args(sys.argv[1:])
78
79     if args.html_viewer:
80         generate_html()
81         exit(0)
82
83     if args.login:
84         try:
85             _, _ = args.login.split(':', 1)
86         except ValueError:
87             logger.error('Invalid `username:password` pair.')
88             exit(1)
89
90         if not args.is_download:
91             logger.warning('YOU DO NOT SPECIFY `--download` OPTION !!!')
92
93     if args.id:
94         _ = map(lambda id: id.strip(), args.id.split(','))
95         args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
96
97     if (args.is_download or args.is_show) and not args.id and not args.keyword and \
98             not args.login and not args.tag:
99         logger.critical('Doujinshi id(s) are required for downloading')
100         parser.print_help()
101         exit(1)
102
103     if not args.keyword and not args.id and not args.login and not args.tag:
104         parser.print_help()
105         exit(1)
106
107     if args.threads <= 0:
108         args.threads = 1
109
110     elif args.threads > 15:
111         logger.critical('Maximum number of used threads is 15')
112         exit(1)
113
114     if args.proxy:
115         proxy_url = urlparse(args.proxy)
116         if proxy_url.scheme not in ('http', 'https'):
117             logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
118         else:
119             constant.PROXY = {'http': args.proxy, 'https': args.proxy}
120
121     return args