]> git.lizzy.rs Git - nhentai.git/blobdiff - nhentai/cmdline.py
Merge pull request #21 from mentaterasmus/master
[nhentai.git] / nhentai / cmdline.py
index 0ab883049f549e3c5206a481e213b497c0c0b162..1cb58a065da0920fce73e5575dcec9e3cef981ee 100644 (file)
@@ -1,18 +1,27 @@
 # coding: utf-8
 from __future__ import print_function
+import sys
 from optparse import OptionParser
-from logger import logger
 try:
     from itertools import ifilter as filter
 except ImportError:
     pass
 
+import nhentai.constant as constant
+from nhentai.utils import urlparse, generate_html
+from nhentai.logger import logger
 
-import constant
+try:
+    reload(sys)
+    sys.setdefaultencoding(sys.stdin.encoding)
+except NameError:
+    # python3
+    pass
 
 
 def banner():
-    print('''       _   _            _        _
+    logger.info(u'''nHentai: あなたも変態。 いいね?
+       _   _            _        _
  _ __ | | | | ___ _ __ | |_ __ _(_)
 | '_ \| |_| |/ _ \ '_ \| __/ _` | |
 | | | |  _  |  __/ | | | || (_| | |
@@ -21,51 +30,90 @@ def banner():
 
 
 def cmd_parser():
-    parser = OptionParser()
-    parser.add_option('--download', dest='is_download', action='store_true', help='download doujinshi or not')
-    parser.add_option('--id', type='int', dest='id', action='store', help='doujinshi id of nhentai')
-    parser.add_option('--ids', type='str', dest='ids', action='store', help='doujinshi id set, e.g. 1,2,3')
-    parser.add_option('--search', type='string', dest='keyword', action='store', help='keyword searched')
+    parser = OptionParser('\n  nhentai --search [keyword] --download'
+                          '\n  NHENTAI=http://h.loli.club nhentai --id [ID ...]'
+                          '\n\nEnvironment Variable:\n'
+                          '  NHENTAI                 nhentai mirror url')
+    parser.add_option('--download', dest='is_download', action='store_true',
+                      help='download doujinshi (for search result)')
+    parser.add_option('--show-info', dest='is_show', action='store_true', help='just show the doujinshi information')
+    parser.add_option('--id', type='string', dest='id', action='store', help='doujinshi ids set, e.g. 1,2,3')
+    parser.add_option('--search', type='string', dest='keyword', action='store', help='search doujinshi by keyword')
     parser.add_option('--page', type='int', dest='page', action='store', default=1,
                       help='page number of search result')
-    parser.add_option('--path', type='string', dest='saved_path', action='store', default='',
-                      help='path which save the doujinshi')
+    parser.add_option('--tags', type='string', dest='tags', action='store', help='download doujinshi by tags')
+    parser.add_option('--output', type='string', dest='output_dir', action='store', default='',
+                      help='output dir')
     parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5,
                       help='thread count of download doujinshi')
     parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
                       help='timeout of download doujinshi')
     parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
-                      help='use proxy, example: socks5://127.0.0.1:1080')
-    args, _ = parser.parse_args()
+                      help='use proxy, example: http://127.0.0.1:1080')
+    parser.add_option('--html', dest='html_viewer', action='store_true',
+                      help='generate a html viewer at current directory')
 
-    if args.ids:
-        _ = map(lambda id: id.strip(), args.ids.split(','))
-        args.ids = set(map(int, filter(lambda id: id.isdigit(), _)))
+    parser.add_option('--login', '-l', type='str', dest='login', action='store',
+                      help='username:password pair of nhentai account')
 
-    if args.is_download and not args.id and not args.ids and not args.keyword:
-        logger.critical('Doujinshi id/ids is required for downloading')
-        parser.print_help()
-        raise SystemExit
+    parser.add_option('--nohtml', dest='is_nohtml', action='store_true',
+                      help='Don\'t generate HTML')
+
+    parser.add_option('--cbz', dest='is_cbz', action='store_true',
+                      help='Generate Comic Book CBZ File')                      
+                      
+    try:
+        sys.argv = list(map(lambda x: unicode(x.decode(sys.stdin.encoding)), sys.argv))
+    except (NameError, TypeError):
+        pass
+    except UnicodeDecodeError:
+        exit(0)
+
+    args, _ = parser.parse_args(sys.argv[1:])
+
+    if args.html_viewer:
+        generate_html()
+        exit(0)
+
+    if args.login:
+        try:
+            _, _ = args.login.split(':', 1)
+        except ValueError:
+            logger.error('Invalid `username:password` pair.')
+            exit(1)
+
+        if not args.is_download:
+            logger.warning('YOU DO NOT SPECIFY `--download` OPTION !!!')
+
+    if args.tags:
+        logger.warning('`--tags` is under construction')
+        exit(1)
 
     if args.id:
-        args.ids = (args.id, ) if not args.ids else args.ids
+        _ = map(lambda id: id.strip(), args.id.split(','))
+        args.id = set(map(int, filter(lambda id_: id_.isdigit(), _)))
 
-    if not args.keyword and not args.ids:
+    if (args.is_download or args.is_show) and not args.id and not args.keyword and not args.login:
+        logger.critical('Doujinshi id(s) are required for downloading')
         parser.print_help()
-        raise SystemExit
+        exit(1)
+
+    if not args.keyword and not args.id and not args.login:
+        parser.print_help()
+        exit(1)
 
     if args.threads <= 0:
         args.threads = 1
-    elif args.threads > 10:
-        logger.critical('Maximum number of used threads is 10')
-        raise SystemExit
+
+    elif args.threads > 15:
+        logger.critical('Maximum number of used threads is 15')
+        exit(1)
 
     if args.proxy:
-        import urlparse
-        proxy_url = urlparse.urlparse(args.proxy)
+        proxy_url = urlparse(args.proxy)
         if proxy_url.scheme not in ('http', 'https'):
-            logger.error('Invalid protocol \'{}\' of proxy, ignored'.format(proxy_url.scheme))
+            logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
         else:
-            constant.PROXY = {proxy_url.scheme: args.proxy}
+            constant.PROXY = {'http': args.proxy, 'https': args.proxy}
 
     return args