From f10ae3cf58bc4dc486cdb3efe04850257581eac2 Mon Sep 17 00:00:00 2001 From: symant233 Date: Tue, 28 May 2019 19:47:48 +0800 Subject: [PATCH] store proxy config --- nhentai/cmdline.py | 37 ++++++++++++++++++++++++------------- nhentai/command.py | 3 ++- nhentai/utils.py | 19 +++++++++---------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index de6455c..9e15359 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -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 diff --git a/nhentai/command.py b/nhentai/command.py index 71d0669..88a8c97 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -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: diff --git a/nhentai/utils.py b/nhentai/utils.py index ce61aa9..61c535e 100644 --- a/nhentai/utils.py +++ b/nhentai/utils.py @@ -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='.'): \n\ \n\ \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: