]> git.lizzy.rs Git - nhentai.git/blob - nhentai/cmdline.py
setup.py and add command line support
[nhentai.git] / nhentai / cmdline.py
1 #coding: utf-8
2 from optparse import OptionParser
3 from itertools import ifilter
4 from logger import logger
5
6
7 def banner():
8     print '''       _   _            _        _
9  _ __ | | | | ___ _ __ | |_ __ _(_)
10 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
11 | | | |  _  |  __/ | | | || (_| | |
12 |_| |_|_| |_|\___|_| |_|\__\__,_|_|
13 '''
14
15
16 def cmd_parser():
17     parser = OptionParser()
18     parser.add_option('--download', dest='is_download', action='store_true', help='download dojinshi or not')
19     parser.add_option('--id', type='int', dest='id', action='store', help='dojinshi id of nhentai')
20     parser.add_option('--ids', type='str', dest='ids', action='store', help='dojinshi id set, e.g. 1,2,3')
21     parser.add_option('--search', type='string', dest='keyword', action='store', help='keyword searched')
22     parser.add_option('--page', type='int', dest='page', action='store', default=1,
23                       help='page number of search result')
24     parser.add_option('--path', type='string', dest='saved_path', action='store', default='',
25                       help='path which save the dojinshi')
26     parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=1,
27                       help='thread count of download dojinshi')
28     args, _ = parser.parse_args()
29
30     if args.ids:
31         _ = map(lambda id: id.strip(), args.ids.split(','))
32         args.ids = set(map(int, ifilter(lambda id: id.isdigit(), _)))
33
34     if args.is_download and not args.id and not args.ids and not args.keyword:
35         logger.critical('Dojinshi id/ids is required for downloading')
36         parser.print_help()
37         raise SystemExit
38
39     if args.id:
40         args.ids = (args.id, ) if not args.ids else args.ids
41
42     if not args.keyword and not args.ids:
43         parser.print_help()
44         raise SystemExit
45
46     if args.threads <= 0:
47         args.threads = 1
48     elif args.threads > 10:
49         logger.critical('Maximum number of used threads is 10')
50         raise SystemExit
51
52     return args