fix: 九维分析大盘从DB读取+腾讯API兜底
This commit is contained in:
@@ -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和支撑阻力 ──
|
||||
|
||||
Reference in New Issue
Block a user