From 538a6d3bbaaa876a953d0e37e49fe9f0e668e4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=A5=E5=BE=AE?= Date: Thu, 9 Jul 2026 21:08:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B9=9D=E7=BB=B4=E5=88=86=E6=9E=90?= =?UTF-8?q?=E5=A4=A7=E7=9B=98=E4=BB=8EDB=E8=AF=BB=E5=8F=96+=E8=85=BE?= =?UTF-8?q?=E8=AE=AFAPI=E5=85=9C=E5=BA=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/per_stock_reassess.py | 63 +++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/scripts/per_stock_reassess.py b/scripts/per_stock_reassess.py index a8e0fc70..f64206a5 100644 --- a/scripts/per_stock_reassess.py +++ b/scripts/per_stock_reassess.py @@ -36,34 +36,61 @@ def _build_full_analysis(code, entry, result): # ── 从DB拉取大盘、基本面、资金流 ── macro_desc = "" pe_val = pb_val = "" - fund_flow = "" try: - import sqlite3 as _sq + import sqlite3 as _sq, json as _j _db = _sq.connect("/home/hmo/MoFin/data/mofin.db") - # 大盘 - _m = _db.execute("SELECT structure FROM macro_context_log ORDER BY id DESC LIMIT 1").fetchone() + # 大盘(从structure列读取) + _m = _db.execute("SELECT structure, sector_mood FROM macro_context_log ORDER BY id DESC LIMIT 1").fetchone() if _m and _m[0]: - import json as _j - _s = _j.loads(_m[0]) - _indices = _s.get("indices", {}) - _parts = [] - for _k in ["上证指数", "深证成指", "创业板指"]: - if _k in _indices: - _d = _indices[_k] - _chg = _d.get("change_pct", 0) - _parts.append(f"{_k}({_chg:+.1f}%)") - if _parts: + _st = _j.loads(_m[0]) + _ix = _st.get("indices", {}) + _desc = _st.get("description", "") + if _ix: + _parts = [] + for _name in ["上证指数", "深证成指", "创业板指", "科创50", "恒生指数"]: + if _name in _ix: + _d = _ix[_name] + if isinstance(_d, dict): + _p = _d.get("price", 0) + _c = _d.get("change_pct", 0) + _parts.append(f"{_name}({_p:.0f},{_c:+.1f}%)") + elif isinstance(_d, (int, float)): + _parts.append(f"{_name}({_d})") macro_desc = " ".join(_parts) - _mood = _s.get("sector_mood", "") - if _mood: + elif _desc: + macro_desc = _desc + _mood = str(_m[1] or "") + if _mood and not macro_desc: + macro_desc = f"情绪={_mood}" + elif _mood: macro_desc += f" 情绪={_mood}" + if not macro_desc: + # fallback: 直接用腾讯API拉大盘 + try: + _r2 = __import__('subprocess').run(["curl", "-s", "http://qt.gtimg.cn/q=sh000001,sz399001,sz399006,sh000688"], + capture_output=True, timeout=10) + _txt = _r2.stdout.decode("gbk", errors="ignore") + _parts = [] + for _line in _txt.strip().split("\n"): + if "~" not in _line: continue + _p = _line.split("~") + if len(_p) < 4: continue + _name2 = _p[1] + _price2 = _p[3] + _chg2 = _p[32] if len(_p) > 32 else "0" + _parts.append(f"{_name2}({_price2},{_chg2}%)") + if _parts: + macro_desc = "腾讯实时 " + " ".join(_parts[:3]) + except: + pass # 基本面 - _f = _db.execute("SELECT pe, pb FROM stock_fundamentals WHERE code=?", (code,)).fetchone() + _f = _db.execute("SELECT pe, pb, eps FROM stock_fundamentals WHERE code=?", (code,)).fetchone() if _f: if _f[0]: pe_val = f"PE={_f[0]:.1f}" if _f[1]: pb_val = f"PB={_f[1]:.2f}" + if _f[2]: pe_val += f" EPS={_f[2]:.2f}" if pe_val else f"EPS={_f[2]:.2f}" _db.close() - except: + except Exception as _e: pass # ── 从tech_snapshot提取MA和支撑阻力 ──