fix: price_monitor HKD→CNY + Eastmoney 2s timeout + None-safe + summary fix
This commit is contained in:
+21
-9
@@ -131,21 +131,26 @@ def fetch_hk_eastmoney(codes):
|
||||
|
||||
results = {}
|
||||
|
||||
# 主通道:东方财富实时行情(逐股查询,港股仅~10只,<1秒完成。该API不支持批量)
|
||||
# 主通道:东方财富实时行情(逐股查询,2秒超时。连续失败则立即切腾讯兜底)
|
||||
consecutive_fail = 0
|
||||
for code in hk_codes:
|
||||
try:
|
||||
if consecutive_fail >= 3:
|
||||
break # 前3只都失败,东财不可用,直接切腾讯
|
||||
url = (f"https://push2.eastmoney.com/api/qt/stock/get"
|
||||
f"?secid=116.{code}"
|
||||
f"&fields=f43,f170,f60,f57,f58"
|
||||
f"&fltt=2")
|
||||
req = urllib.request.Request(url, headers={"User-Agent": UA})
|
||||
with urllib.request.urlopen(req, timeout=5) as r:
|
||||
with urllib.request.urlopen(req, timeout=2) as r:
|
||||
resp = json.loads(r.read().decode("utf-8"))
|
||||
|
||||
if resp.get("rc") != 0:
|
||||
consecutive_fail += 1
|
||||
continue
|
||||
item = resp.get("data", {})
|
||||
if not item:
|
||||
consecutive_fail += 1
|
||||
continue
|
||||
price = float(item.get("f43", 0)) if item.get("f43") else 0
|
||||
prev_close = float(item.get("f60", 0)) if item.get("f60") else 0
|
||||
@@ -153,10 +158,12 @@ def fetch_hk_eastmoney(codes):
|
||||
change_pct = str(item.get("f170", "0"))
|
||||
if price > 0:
|
||||
results[code] = (price, change, change_pct)
|
||||
time.sleep(0.1) # 防止触发东财反爬(逐股查询,不支持批量)
|
||||
consecutive_fail = 0 # 成功后重置计数
|
||||
time.sleep(0.1)
|
||||
except Exception as e:
|
||||
print(f"⚠️ 东方财富 {code} 拉取失败: {e}", file=sys.stderr)
|
||||
continue
|
||||
consecutive_fail += 1
|
||||
if consecutive_fail <= 2:
|
||||
print(f"⚠️ 东方财富 {code} 拉取失败: {e}", file=sys.stderr)
|
||||
|
||||
# Fallback: 腾讯 qt.gtimg.cn(15分钟延迟)
|
||||
missing = [c for c in hk_codes if c not in results]
|
||||
@@ -248,12 +255,14 @@ def refresh_data_prices():
|
||||
if s['code'] in prices:
|
||||
price, _, change_pct = prices[s['code']]
|
||||
if price > 0:
|
||||
# 港股API返回HKD,直接存HKD原值。calc_total_mv统一做CNY折算
|
||||
old = s.get('price', 0)
|
||||
# 港股:API返回HKD,需转CNY。系统统一存CNY标价
|
||||
if is_hk_stock(s['code']):
|
||||
price = round(price * HK_RATE, 2)
|
||||
old = s.get('price') or 0
|
||||
if abs(old - price) > 0.001:
|
||||
s['price'] = round(price, 2)
|
||||
s['change_pct'] = float(change_pct) if change_pct else 0
|
||||
s['currency'] = 'HKD' if is_hk_stock(s['code']) else 'CNY'
|
||||
s['currency'] = 'CNY'
|
||||
updated += 1
|
||||
changed = True
|
||||
if changed:
|
||||
@@ -297,7 +306,10 @@ def refresh_data_prices():
|
||||
if s['code'] in prices:
|
||||
price, _, change_pct = prices[s['code']]
|
||||
if price > 0:
|
||||
old = s.get('price', 0)
|
||||
# 港股:API返回HKD,需转CNY
|
||||
if is_hk_stock(s['code']):
|
||||
price = round(price * HK_RATE, 2)
|
||||
old = s.get('price') or 0
|
||||
if abs(old - price) > 0.001:
|
||||
s['price'] = round(price, 2)
|
||||
s['change_pct'] = float(change_pct) if change_pct else 0
|
||||
|
||||
Reference in New Issue
Block a user