fix: market_screener market.json→DB, remove all JSON refs

This commit is contained in:
知微
2026-07-03 23:48:29 +08:00
parent b6959f73bc
commit a05b82cd6b
+37 -8
View File
@@ -7,8 +7,7 @@ from pathlib import Path
WEB_DASHBOARD_DIR = Path(__file__).resolve().parent.parent.parent / "web-dashboard" if "hermes" in str(Path(__file__).resolve()) else Path(__file__).parent WEB_DASHBOARD_DIR = Path(__file__).resolve().parent.parent.parent / "web-dashboard" if "hermes" in str(Path(__file__).resolve()) else Path(__file__).parent
DATA_DIR = WEB_DASHBOARD_DIR / "data" DATA_DIR = WEB_DASHBOARD_DIR / "data"
MARKET_JSON = DATA_DIR / "market.json" POOL_JSON = DATA_DIR / "candidate_pool.json" # 筛选缓存(临时)
POOL_JSON = DATA_DIR / "candidate_pool.json"
XIAOGUO_MODEL = "Qwen3.6-27B-OptiQ-4bit" XIAOGUO_MODEL = "Qwen3.6-27B-OptiQ-4bit"
API_TIMEOUT = 60 API_TIMEOUT = 60
MAX_SECTORS = 5 MAX_SECTORS = 5
@@ -208,10 +207,40 @@ def step2(sector, source):
def main(): def main():
market = load_json(MARKET_JSON) # 从 DB 读大盘+板块数据(替代 market.json
if not market or not market.get("sectors"): from mofin_db import get_conn
print("market.json 无数据", flush=True); return conn = get_conn()
sectors, source = market["sectors"], market.get("source", "ths") market = {"sectors": [], "source": "db"}
try:
# latest snapshot
sr = conn.execute("SELECT * FROM market_snapshots ORDER BY id DESC LIMIT 1").fetchone()
if sr:
cols = [d[0] for d in conn.execute("SELECT * FROM market_snapshots LIMIT 0").description]
snap = dict(zip(cols, sr))
market["up_ratio"] = snap.get("up_ratio", 0)
market["mood"] = snap.get("mood", "neutral")
market["source"] = snap.get("source", "db")
# sector data
sectors = conn.execute("""
SELECT s.name as name, s.change_pct as change_pct, s.lead_stock as lead_stock,
s.up_count, s.down_count, s.net_inflow
FROM sector_snapshots s
JOIN market_snapshots ms ON s.snapshot_id = ms.id
WHERE ms.id = (SELECT MAX(id) FROM market_snapshots)
ORDER BY s.change_pct DESC
""").fetchall()
if sectors:
cols = [d[0] for d in conn.execute("SELECT name, change_pct, lead_stock, up_count, down_count, net_inflow FROM sector_snapshots LIMIT 0").description]
market["sectors"] = [dict(zip(cols, r)) for r in sectors]
except Exception as e:
print(f"DB read error: {e}", flush=True)
conn.close()
if not market.get("sectors"):
print("market 无数据", flush=True); return
sectors = market["sectors"]
source = market.get("source", "db")
print(f"板块: {len(sectors)}, 来源: {source}", flush=True) print(f"板块: {len(sectors)}, 来源: {source}", flush=True)
pool = load_pool() pool = load_pool()
existing = {c["code"] for c in pool.get("candidates", []) if not c.get("dropped")} existing = {c["code"] for c in pool.get("candidates", []) if not c.get("dropped")}
@@ -229,7 +258,8 @@ def main():
hot_names = [s["name"] for s in view.get("hot_sectors", [])] hot_names = [s["name"] for s in view.get("hot_sectors", [])]
print(f"热门行业: {hot_names}", flush=True) print(f"热门行业: {hot_names}", flush=True)
if not hot_names: if not hot_names:
save_pool(pool); save_json(MARKET_JSON, market); return save_pool(pool)
return
# 第2步 # 第2步
hot_data = [s for s in sectors if s.get("name", "") in hot_names][:MAX_SECTORS] hot_data = [s for s in sectors if s.get("name", "") in hot_names][:MAX_SECTORS]
@@ -247,7 +277,6 @@ def main():
cleanup(pool) cleanup(pool)
save_pool(pool) save_pool(pool)
save_json(MARKET_JSON, market)
print(f"完成: 新增{new_count}, 池内{pool['total_candidates']}活跃", flush=True) print(f"完成: 新增{new_count}, 池内{pool['total_candidates']}活跃", flush=True)
if __name__ == "__main__": if __name__ == "__main__":