fix: add Retry config for request

main
jackluson 2 years ago
parent 904af59ea3
commit d246dd058f

@ -179,20 +179,16 @@ class FundApier:
res_json = res.json() res_json = res.json()
if res_json.get('result_code') == 0: if res_json.get('result_code') == 0:
base_info = res.json().get('data') base_info = res.json().get('data')
total_asset = base_info.get('totshare') total_asset = base_info.get('totshare')
if(total_asset.endswith('')): if(total_asset.endswith('')):
total_asset = round(float(total_asset[0:-1]) / 10000, 3) total_asset = round(float(total_asset[0:-1]) / 10000, 3)
elif(total_asset.endswith('亿')): elif(total_asset.endswith('亿')):
total_asset = float(total_asset[0:-1]) total_asset = float(total_asset[0:-1])
else: else:
print(total_asset, "not a number") print(self.fund_code, ":", total_asset, "not a number")
return return
self.total_asset = total_asset self.total_asset = total_asset
return self.total_asset return self.total_asset
else:
pprint(res_json)
print('code:1', self.fund_code)
else: else:
pprint(res.content) pprint(res.content)
print('code:2', self.fund_code) print('code:2', self.fund_code)

@ -7,13 +7,21 @@ from threading import Lock, Thread
import numpy as np import numpy as np
import requests import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from PIL import Image from PIL import Image
from sewar.full_ref import sam, uqi from sewar.full_ref import sam, uqi
from skimage import io from skimage import io
requests.adapters.DEFAULT_RETRIES = 10 # 增加重连次数 # requests.adapters.DEFAULT_RETRIES = 10 # 增加重连次数
s = requests.session() # s = requests.session()
s.keep_alive = False # 关闭多余连接 # s.keep_alive = False # 关闭多余连接
session = requests.Session()
retry = Retry(connect=5, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
dir = os.getcwd() + '/src/' dir = os.getcwd() + '/src/'
@ -78,7 +86,7 @@ def get_star_count_with_np(morning_star_url):
module_path = os.getcwd() + '/src' module_path = os.getcwd() + '/src'
temp_star_url = module_path + '/assets/star/tmp.gif' temp_star_url = module_path + '/assets/star/tmp.gif'
try: try:
r = requests.get(morning_star_url) r = session.get(morning_star_url)
except BaseException: except BaseException:
raise BaseException('请求失败') raise BaseException('请求失败')
with open(temp_star_url, "wb") as f: with open(temp_star_url, "wb") as f:
@ -98,10 +106,10 @@ def get_star_count_with_np(morning_star_url):
def get_star_count(morning_star_url, fund_code, img_ele=None): def get_star_count(morning_star_url, fund_code, img_ele=None):
# path = './assets/star/star' # path = './assets/star/star'
try: try:
return get_star_count_with_sewar(fund_code, img_ele) return get_star_count_with_np(morning_star_url)
except BaseException: except BaseException:
print('图片相似度比较失败') print('图片比较失败')
return get_star_count_with_np(morning_star_url) return get_star_count_with_sewar(fund_code, img_ele)
def parse_csv(datafile): def parse_csv(datafile):
data = [] data = []

Loading…
Cancel
Save