Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit d246dd0

Browse files
committed
fix: add Retry config for request
1 parent 904af59 commit d246dd0

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/fund_info/api.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,16 @@ def get_base_info_from_danjuan(self):
179179
res_json = res.json()
180180
if res_json.get('result_code') == 0:
181181
base_info = res.json().get('data')
182-
183182
total_asset = base_info.get('totshare')
184183
if(total_asset.endswith('万')):
185184
total_asset = round(float(total_asset[0:-1]) / 10000, 3)
186185
elif(total_asset.endswith('亿')):
187186
total_asset = float(total_asset[0:-1])
188187
else:
189-
print(total_asset, "not a number")
188+
print(self.fund_code, ":", total_asset, "not a number")
190189
return
191190
self.total_asset = total_asset
192191
return self.total_asset
193-
else:
194-
pprint(res_json)
195-
print('code:1', self.fund_code)
196192
else:
197193
pprint(res.content)
198194
print('code:2', self.fund_code)

src/utils/index.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77

88
import numpy as np
99
import requests
10+
from requests.adapters import HTTPAdapter
11+
from urllib3.util.retry import Retry
1012
from PIL import Image
1113
from sewar.full_ref import sam, uqi
1214
from skimage import io
1315

14-
requests.adapters.DEFAULT_RETRIES = 10 # 增加重连次数
15-
s = requests.session()
16-
s.keep_alive = False # 关闭多余连接
16+
# requests.adapters.DEFAULT_RETRIES = 10 # 增加重连次数
17+
# s = requests.session()
18+
# s.keep_alive = False # 关闭多余连接
19+
20+
session = requests.Session()
21+
retry = Retry(connect=5, backoff_factor=0.5)
22+
adapter = HTTPAdapter(max_retries=retry)
23+
session.mount('http://', adapter)
24+
session.mount('https://', adapter)
1725

1826
dir = os.getcwd() + '/src/'
1927

@@ -78,7 +86,7 @@ def get_star_count_with_np(morning_star_url):
7886
module_path = os.getcwd() + '/src'
7987
temp_star_url = module_path + '/assets/star/tmp.gif'
8088
try:
81-
r = requests.get(morning_star_url)
89+
r = session.get(morning_star_url)
8290
except BaseException:
8391
raise BaseException('请求失败')
8492
with open(temp_star_url, "wb") as f:
@@ -98,10 +106,10 @@ def get_star_count_with_np(morning_star_url):
98106
def get_star_count(morning_star_url, fund_code, img_ele=None):
99107
# path = './assets/star/star'
100108
try:
101-
return get_star_count_with_sewar(fund_code, img_ele)
109+
return get_star_count_with_np(morning_star_url)
102110
except BaseException:
103-
print('图片相似度比较失败')
104-
return get_star_count_with_np(morning_star_url)
111+
print('图片比较失败')
112+
return get_star_count_with_sewar(fund_code, img_ele)
105113

106114
def parse_csv(datafile):
107115
data = []

0 commit comments

Comments
 (0)