]> git.lizzy.rs Git - nhentai.git/commitdiff
store proxy config
authorsymant233 <symant233@gmail.com>
Tue, 28 May 2019 11:47:48 +0000 (19:47 +0800)
committersymant233 <symant233@gmail.com>
Tue, 28 May 2019 11:47:48 +0000 (19:47 +0800)
nhentai/cmdline.py
nhentai/command.py
nhentai/utils.py

index de6455caa6da973f3c0e8dd768663c358b3180d8..9e153596a78a844486e023255c9d035e6715e00e 100644 (file)
@@ -69,7 +69,7 @@ def cmd_parser():
     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='',
-                      help='uses a proxy, for example: http://127.0.0.1:1080')
+                      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('--format', type='string', dest='name_format', action='store',
                       help='format the saved folder name', default='[%i][%a][%t]')
@@ -125,6 +125,29 @@ def cmd_parser():
         logger.info('Cookie saved.')
         exit(0)
 
+    if os.path.exists(os.path.join(constant.NHENTAI_HOME, 'proxy')):
+        with open(os.path.join(constant.NHENTAI_HOME, 'proxy'), 'r') as f:
+            link = f.read()
+            constant.PROXY = {'http': link, 'https': link}
+
+    if args.proxy:
+        try:
+            if not os.path.exists(constant.NHENTAI_HOME):
+                os.mkdir(constant.NHENTAI_HOME)
+
+            proxy_url = urlparse(args.proxy)
+            if proxy_url.scheme not in ('http', 'https'):
+                logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
+            else:
+                with open(os.path.join(constant.NHENTAI_HOME, 'proxy'), 'w') as f:
+                    f.write(args.proxy)
+        except Exception as e:
+            logger.error('Cannot create NHENTAI_HOME: {}'.format(str(e)))
+            exit(1)
+
+        logger.info('Proxy \'{0}\' saved.'.format(args.proxy))
+        exit(0)
+
     '''
     if args.login:
         try:
@@ -168,16 +191,4 @@ def cmd_parser():
         logger.critical('Maximum number of used threads is 15')
         exit(1)
 
-    if args.proxy:
-        proxy_url = urlparse(args.proxy)
-        if args.proxy == 'default' or 'd':
-            constant.PROXY = {
-                'http': "http://127.0.0.1:1080",
-                'https': "http://127.0.0.1:1080"
-            }
-        elif proxy_url.scheme not in ('http', 'https'):
-            logger.error('Invalid protocol \'{0}\' of proxy, ignored'.format(proxy_url.scheme))
-        else:
-            constant.PROXY = {'http': args.proxy, 'https': args.proxy}
-
     return args
index 71d0669f28c81c2af244318699eced0ed7eefd24..88a8c97df0bd2f606636010fb51c31df3b489a67 100644 (file)
@@ -61,7 +61,8 @@ def main():
                 generate_html(options.output_dir, doujinshi)
             elif options.is_cbz:
                 generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir)
-        generate_main_html(options.output_dir)
+        if options.main_viewer:
+            generate_main_html(options.output_dir)
         if not platform.system() == 'Windows':
             logger.log(15, '🍻 All done.')
         else:
index ce61aa9ccd853becce8e00d8cde6b202acbe2522..61c535e56902c9040ec3bc9d00fa4208c7ebd158 100644 (file)
@@ -84,8 +84,7 @@ def generate_html(output_dir='.', doujinshi_obj=None):
 def generate_main_html(output_dir='.'):
     """Generete a main html to show all the contain doujinshi.
     With a link to thier `index.html`. 
-    Default output folder will be the CLI path.
-    """
+    Default output folder will be the CLI path."""
     count = 0
     image_html = ''
     main = readfile('viewer/main.html')
@@ -99,13 +98,13 @@ def generate_main_html(output_dir='.'):
                     </a>\n\
                 </div>\n\
             </div>\n'
-    
+
     if output_dir == '':
         os.chdir('.')
     else:
-        os.chdir(output_dir) 
+        os.chdir(output_dir)
     # switch to given dir
-    doujinshi_dirs = next(os.walk('.'))[1] 
+    doujinshi_dirs = next(os.walk('.'))[1]
     # https://stackoverflow.com/questions/141291/how-to-list-only-top-level-directories-in-python
 
     for folder in doujinshi_dirs:
@@ -115,10 +114,10 @@ def generate_main_html(output_dir='.'):
         if 'index.html' in files:
             count += 1
         else:
-            logger.warning('{} folder does not have index.html, try use --html arg first.'\
-            .format(folder))
+            logger.warning('{} folder does not have index.html (try use --html arg first).'
+                           .format(folder))
             continue
-        image = files[0] # 001.jpg or 001.png
+        image = files[0]  # 001.jpg or 001.png
         if folder is not None:
             title = folder.replace('_', ' ')
             # if sys.version_info > (3, 0):
@@ -138,10 +137,10 @@ def generate_main_html(output_dir='.'):
         else:
             with open('./main.html', 'wb') as f:
                 f.write(data.encode('utf-8'))
-        logger.log(15, 'Main Viewer has been write to \'{0}/main.html\''.format(output_dir))
+        logger.log(
+            15, 'Main Viewer has been write to \'{0}/main.html\''.format(output_dir))
     except Exception as e:
         logger.warning('Writen Main Viewer failed ({})'.format(str(e)))
-    logger.info('==>Process finished.')
 
 def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
     if doujinshi_obj is not None: