]> git.lizzy.rs Git - nhentai.git/blobdiff - nhentai/cmdline.py
Merge pull request #224 from RicterZ/pull/221
[nhentai.git] / nhentai / cmdline.py
index f1e8184e4d8bb8c1f3fe92ec6359848e8bab4ec1..3085f2aa8c6b23db90447f0728fbf59250714d64 100644 (file)
@@ -1,5 +1,5 @@
 # coding: utf-8
-from __future__ import print_function
+
 import os
 import sys
 import json
@@ -15,17 +15,6 @@ from nhentai import __version__
 from nhentai.utils import urlparse, generate_html, generate_main_html, DB
 from nhentai.logger import logger
 
-try:
-    if sys.version_info < (3, 0, 0):
-        import codecs
-        import locale
-        sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)
-        sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr)
-
-except NameError:
-    # python3
-    pass
-
 
 def banner():
     logger.info(u'''nHentai ver %s: あなたも変態。 いいね?
@@ -43,7 +32,7 @@ def load_config():
 
     try:
         with open(constant.NHENTAI_CONFIG_FILE, 'r') as f:
-            constant.CONFIG = json.load(f)
+            constant.CONFIG.update(json.load(f))
     except json.JSONDecodeError:
         logger.error('Failed to load config file.')
         write_config()
@@ -87,7 +76,7 @@ def cmd_parser():
                       choices=['recent', 'popular', 'popular-today', 'popular-week'])
 
     # download options
-    parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='',
+    parser.add_option('--output', '-o', 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 for downloading doujinshi')
@@ -95,11 +84,12 @@ def cmd_parser():
                       help='timeout for downloading doujinshi')
     parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0,
                       help='slow down between downloading every doujinshi')
-    parser.add_option('--proxy', '-p', type='string', dest='proxy', action='store', default='',
+    parser.add_option('--proxy', type='string', dest='proxy', action='store',
                       help='store a proxy, for example: -p \'http://127.0.0.1:1080\'')
-    parser.add_option('--file',  '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
+    parser.add_option('--file', '-f', type='string', dest='file', action='store', help='read gallery IDs from file.')
     parser.add_option('--format', type='string', dest='name_format', action='store',
                       help='format the saved folder name', default='[%i][%a][%t]')
+    parser.add_option('--dry-run', '-r', action='store_true', dest='dryrun', help='Dry run, skip file download.')
 
     # generate options
     parser.add_option('--html', dest='html_viewer', action='store_true',
@@ -114,6 +104,8 @@ def cmd_parser():
                       help='generate PDF file')
     parser.add_option('--rm-origin-dir', dest='rm_origin_dir', action='store_true', default=False,
                       help='remove downloaded doujinshi dir when generated CBZ or PDF file.')
+    parser.add_option('--meta', dest='generate_metadata', action='store_true',
+                      help='generate a metadata file in doujinshi format')
 
     # nhentai options
     parser.add_option('--cookie', type='str', dest='cookie', action='store',
@@ -126,10 +118,11 @@ def cmd_parser():
                       default=False, help='save downloaded doujinshis, whose will be skipped if you re-download them')
     parser.add_option('--clean-download-history', action='store_true', default=False, dest='clean_download_history',
                       help='clean download history')
+    parser.add_option('--template', dest='viewer_template', action='store',
+                      help='set viewer template', default='')
 
     try:
         sys.argv = [unicode(i.decode(sys.stdin.encoding)) for i in sys.argv]
-        print()
     except (NameError, TypeError):
         pass
     except UnicodeDecodeError:
@@ -153,30 +146,45 @@ def cmd_parser():
         exit(0)
 
     # --- set config ---
-    if args.cookie:
+    if args.cookie is not None:
         constant.CONFIG['cookie'] = args.cookie
         logger.info('Cookie saved.')
         write_config()
         exit(0)
 
-    if args.language:
+    if args.language is not None:
         constant.CONFIG['language'] = args.language
-        logger.info('LANGUAGE now set to \'{0}\''.format(args.language))
+        logger.info('Default language now set to \'{0}\''.format(args.language))
         write_config()
         exit(0)
         # TODO: search without language
 
-    if args.proxy:
+    if args.proxy is not None:
         proxy_url = urlparse(args.proxy)
-        if proxy_url.scheme not in ('http', 'https'):
+        if not args.proxy == '' and proxy_url.scheme not in ('http', 'https'):
             logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
-        constant.CONFIG['proxy'] = {
-            'http': args.proxy,
-            'https': args.proxy,
-        }
-        logger.info('Proxy \'{0}\' saved.'.format(args.proxy))
-        write_config()
-        exit(0)
+            exit(0)
+        else:
+            constant.CONFIG['proxy'] = {
+                'http': args.proxy,
+                'https': args.proxy,
+            }
+            logger.info('Proxy now set to \'{0}\'.'.format(args.proxy))
+            write_config()
+            exit(0)
+
+    if args.viewer_template is not None:
+        if not args.viewer_template:
+            args.viewer_template = 'default'
+
+        if not os.path.exists(os.path.join(os.path.dirname(__file__),
+                                           'viewer/{}/index.html'.format(args.viewer_template))):
+            logger.error('Template \'{}\' does not exists'.format(args.viewer_template))
+            exit(1)
+        else:
+            constant.CONFIG['template'] = args.viewer_template
+            write_config()
+
     # --- end set config ---
 
     if args.favorites:
@@ -198,7 +206,7 @@ def cmd_parser():
         parser.print_help()
         exit(1)
 
-    if not args.keyword and not args.id and not  args.favorites:
+    if not args.keyword and not args.id and not args.favorites:
         parser.print_help()
         exit(1)
 
@@ -209,4 +217,8 @@ def cmd_parser():
         logger.critical('Maximum number of used threads is 15')
         exit(1)
 
+    if args.dryrun and (args.is_cbz or args.is_pdf):
+        logger.critical('Cannot generate PDF or CBZ during dry-run')
+        exit(1)
+
     return args