store proxy config

This commit is contained in:
symant233 2019-05-28 19:47:48 +08:00
parent 86b3a092c7
commit f10ae3cf58
3 changed files with 35 additions and 24 deletions

View File

@ -69,7 +69,7 @@ def cmd_parser():
parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0, parser.add_option('--delay', '-d', type='int', dest='delay', action='store', default=0,
help='slow down between downloading every doujinshi') help='slow down between downloading every doujinshi')
parser.add_option('--proxy', '-p', type='string', dest='proxy', action='store', default='', 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('--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', parser.add_option('--format', type='string', dest='name_format', action='store',
help='format the saved folder name', default='[%i][%a][%t]') help='format the saved folder name', default='[%i][%a][%t]')
@ -125,6 +125,29 @@ def cmd_parser():
logger.info('Cookie saved.') logger.info('Cookie saved.')
exit(0) 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: if args.login:
try: try:
@ -168,16 +191,4 @@ def cmd_parser():
logger.critical('Maximum number of used threads is 15') logger.critical('Maximum number of used threads is 15')
exit(1) 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 return args

View File

@ -61,6 +61,7 @@ def main():
generate_html(options.output_dir, doujinshi) generate_html(options.output_dir, doujinshi)
elif options.is_cbz: elif options.is_cbz:
generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir) generate_cbz(options.output_dir, doujinshi, options.rm_origin_dir)
if options.main_viewer:
generate_main_html(options.output_dir) generate_main_html(options.output_dir)
if not platform.system() == 'Windows': if not platform.system() == 'Windows':
logger.log(15, '🍻 All done.') logger.log(15, '🍻 All done.')

View File

@ -84,8 +84,7 @@ def generate_html(output_dir='.', doujinshi_obj=None):
def generate_main_html(output_dir='.'): def generate_main_html(output_dir='.'):
"""Generete a main html to show all the contain doujinshi. """Generete a main html to show all the contain doujinshi.
With a link to thier `index.html`. 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 count = 0
image_html = '' image_html = ''
main = readfile('viewer/main.html') main = readfile('viewer/main.html')
@ -115,7 +114,7 @@ def generate_main_html(output_dir='.'):
if 'index.html' in files: if 'index.html' in files:
count += 1 count += 1
else: else:
logger.warning('{} folder does not have index.html, try use --html arg first.'\ logger.warning('{} folder does not have index.html (try use --html arg first).'
.format(folder)) .format(folder))
continue continue
image = files[0] # 001.jpg or 001.png image = files[0] # 001.jpg or 001.png
@ -138,10 +137,10 @@ def generate_main_html(output_dir='.'):
else: else:
with open('./main.html', 'wb') as f: with open('./main.html', 'wb') as f:
f.write(data.encode('utf-8')) 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: except Exception as e:
logger.warning('Writen Main Viewer failed ({})'.format(str(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): def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False):
if doujinshi_obj is not None: if doujinshi_obj is not None: