diff --git a/scripts/stock_news.py b/scripts/stock_news.py index 59fe3e97..fc02f716 100644 --- a/scripts/stock_news.py +++ b/scripts/stock_news.py @@ -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