fix(stock_news): get_url改curl子进程+原油解析hf_OIL格式

This commit is contained in:
小唯 A06 2026-07-12 09:58:21 +08:00
parent dec34bf88a
commit 76e86f53c9
1 changed files with 17 additions and 6 deletions

View File

@ -30,10 +30,15 @@ def send_feishu(msg):
def get_url(url, timeout=4, enc="gbk"):
"""快速HTTP GET统一处理编码"""
"""快速HTTP GET统一用curl避免urllib在hermes/scripts目录异常"""
import subprocess, shlex, os
env = dict(os.environ)
for k in ["http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY"]:
env.pop(k, None)
cmd = ["curl", "-s", "--max-time", str(timeout), url]
try:
data = urllib.request.urlopen(url, timeout=timeout).read()
return data.decode(enc, errors="ignore")
r = subprocess.run(cmd, capture_output=True, timeout=timeout + 2, env=env)
return r.stdout.decode(enc, errors="ignore")
except Exception:
return ""
@ -60,9 +65,15 @@ def get_oil():
try:
url = "https://qt.gtimg.cn/q=hf_OIL"
text = get_url(url)
parts = text.split("~")
if len(parts) > 10 and parts[3]:
return {"value": float(parts[3]), "change": float(parts[3]) - float(parts[4])}
# hf_OIL 格式: v_hf_OIL="75.23,-1.40,75.22,75.27,77.52,75.22,05:59:59,76.30,76.15,0,77,2,2026-07-11,文字"
# parts[0]="v_hf_OIL=", parts[1]="75.23,-1.40,..."
if "=" in text:
val_part = text.split("=", 1)[1].strip('"; \n')
vals = val_part.split(",")
if len(vals) >= 8:
price = float(vals[0])
yclose = float(vals[7])
return {"value": price, "change": price - yclose}
except Exception:
pass
return None