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()
if res_json.get('result_code') == 0:
base_info = res.json().get('data')
total_asset = base_info.get('totshare')
if(total_asset.endswith('')):
total_asset = round(float(total_asset[0:-1]) / 10000, 3)
elif(total_asset.endswith('亿')):
total_asset = float(total_asset[0:-1])
else:
print(total_asset, "not a number")
print(self.fund_code, ":", total_asset, "not a number")
return
self.total_asset = total_asset
return self.total_asset
else:
pprint(res_json)
print('code:1', self.fund_code)
else:
pprint(res.content)
print('code:2', self.fund_code)

@ -7,13 +7,21 @@ from threading import Lock, Thread
import numpy as np
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from PIL import Image
from sewar.full_ref import sam, uqi
from skimage import io
requests.adapters.DEFAULT_RETRIES = 10 # 增加重连次数
s = requests.session()
s.keep_alive = False # 关闭多余连接
# requests.adapters.DEFAULT_RETRIES = 10 # 增加重连次数
# s = requests.session()
# 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/'
@ -78,7 +86,7 @@ def get_star_count_with_np(morning_star_url):
module_path = os.getcwd() + '/src'
temp_star_url = module_path + '/assets/star/tmp.gif'
try:
r = requests.get(morning_star_url)
r = session.get(morning_star_url)
except BaseException:
raise BaseException('请求失败')
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):
# path = './assets/star/star'
try:
return get_star_count_with_sewar(fund_code, img_ele)
except BaseException:
print('图片相似度比较失败')
return get_star_count_with_np(morning_star_url)
except BaseException:
print('图片比较失败')
return get_star_count_with_sewar(fund_code, img_ele)
def parse_csv(datafile):
data = []

Loading…
Cancel
Save