From 064d8a8349c71a845107a986f7af52deee8053a8 Mon Sep 17 00:00:00 2001 From: jnozsc Date: Sun, 16 Feb 2020 13:20:10 -0800 Subject: [PATCH] refactor config proxy --- ADC_function.py | 20 +++++++++++++------- config.ini | 5 ++++- core.py | 10 +--------- core2.py | 10 +--------- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/ADC_function.py b/ADC_function.py index 0158bbf..2b48e0b 100755 --- a/ADC_function.py +++ b/ADC_function.py @@ -63,6 +63,17 @@ else: except: print('[-]Config.ini read failed! Please use the offical file!') +def get_network_settings(): + try: + proxy = config["proxy"]["proxy"] + timeout = int(config["proxy"]["timeout"]) + retry_count = int(config["proxy"]["retry"]) + assert timeout > 0 + assert retry_count > 0 + except: + raise ValueError("[-]Proxy config error! Please check the config.") + return proxy, timeout, retry_count + def getDataState(json_data): # 元数据获取失败检测 if json_data['title'] == '' or json_data['title'] == 'None' or json_data['title'] == 'null': return 0 @@ -87,16 +98,11 @@ def getXpathSingle(htmlcode,xpath): return result1 def get_html(url,cookies = None):#网页请求核心 - try: - proxy = config['proxy']['proxy'] - timeout = int(config['proxy']['timeout']) - retry_count = int(config['proxy']['retry']) - except: - print('[-]Proxy config error! Please check the config.') + proxy, timeout, retry_count = get_network_settings() i = 0 while i < retry_count: try: - if not str(config['proxy']['proxy']) == '': + if not proxy == '': proxies = {"http": "http://" + proxy,"https": "https://" + proxy} headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36'} getweb = requests.get(str(url), headers=headers, timeout=timeout,proxies=proxies, cookies=cookies) diff --git a/config.ini b/config.ini index fbfb546..4f3006a 100644 --- a/config.ini +++ b/config.ini @@ -5,8 +5,11 @@ success_output_folder=JAV_output soft_link=0 [proxy] +# modify this line to your proxy server, leave black proxy= for direct connection proxy=127.0.0.1:1081 +# timeout must always be greater than zero timeout=10 +# retry must always be greater than zero retry=3 [Name_Rule] @@ -25,4 +28,4 @@ literals=\() folders=failed,JAV_output [debug_mode] -switch=0 \ No newline at end of file +switch=0 diff --git a/core.py b/core.py index ce11094..5ba1432 100755 --- a/core.py +++ b/core.py @@ -212,15 +212,7 @@ def creatFolder(success_folder, location_rule, json_data, Config): # 创建文 # =====================资源下载部分=========================== def DownloadFileWithFilename(url, filename, path, Config, filepath, failed_folder): # path = examle:photo , video.in the Project Folder! - retry_count = 0 - proxy = '' - timeout = 0 - try: - proxy = Config['proxy']['proxy'] - timeout = int(Config['proxy']['timeout']) - retry_count = int(Config['proxy']['retry']) - except: - print('[-]Proxy config error! Please check the config.') + proxy, timeout, retry_count = get_network_settings() i = 0 while i < retry_count: diff --git a/core2.py b/core2.py index cdfd637..9f7e4de 100644 --- a/core2.py +++ b/core2.py @@ -207,15 +207,7 @@ def creatFolder(success_folder, location_rule, json_data, Config): # 创建文 # =====================资源下载部分=========================== def DownloadFileWithFilename(url, filename, path, Config, filepath, failed_folder): # path = examle:photo , video.in the Project Folder! - retry_count = 0 - proxy = '' - timeout = 0 - try: - proxy = Config['proxy']['proxy'] - timeout = int(Config['proxy']['timeout']) - retry_count = int(Config['proxy']['retry']) - except: - print('[-]Proxy config error! Please check the config.') + proxy, timeout, retry_count = get_network_settings() i = 0 while i < retry_count: