#!/usr/bin/env python3 """mofin_collect.py — MoFin 数据采集链 每轮盯盘 cron 前运行,顺序执行: 1. market_watch — 拉90个行业板块数据(9:30前跳过,市场未开) 2. trend_detector — 检测17种信号(依赖板块数据,同跳) 3. mofin_news — 搜新闻+小果分析 4. stock_quote — 所有持仓最新行情(CRITICAL: LLM唯一价格源) """ import subprocess, sys, time from pathlib import Path from datetime import datetime BASE = Path(__file__).parent.parent if "hermes" in str(Path(__file__).resolve()) else Path(__file__).parent now = datetime.now() market_open = (now.hour >= 9 and now.minute >= 30) or now.hour >= 10 # 步骤1-3: 行业/新闻数据 SCRIPTS = [] if market_open: SCRIPTS.append(("market_watch.py", 60)) SCRIPTS.append(("trend_detector.py", 60)) else: print(f"[{now.strftime('%H:%M')}] 市场未开盘(9:30),跳过板块采集", flush=True) SCRIPTS.append(("mofin_news.py", 50)) for script, timeout in SCRIPTS: path = BASE / script if not path.exists(): path = Path("/home/hmo/MoFin") / script print(f"--- {script} ---", flush=True) start = time.time() try: result = subprocess.run( [sys.executable, str(path)], capture_output=True, text=True, timeout=timeout ) elapsed = time.time() - start if result.returncode == 0: print(f"OK ({elapsed:.0f}s)", flush=True) if result.stdout.strip(): for line in result.stdout.strip().split("\n")[-3:]: print(f" {line}", flush=True) else: print(f"FAIL ({elapsed:.0f}s): {result.stderr[:200]}", flush=True) except subprocess.TimeoutExpired: print(f"TIMEOUT ({timeout}s)", flush=True) except Exception as e: print(f"ERROR: {e}", flush=True) # ── 步骤4: 个股行情注入(唯一权威价格源)── # 所有持仓最新行情,注入到 LLM context # LLM 禁止自行调用原始API解析价格 PRICE_SCRIPT = BASE / "stock_quote.py" if not PRICE_SCRIPT.exists(): PRICE_SCRIPT = Path("/home/hmo/MoFin/scripts/stock_quote.py") if PRICE_SCRIPT.exists(): print("--- stock_quote.py ---", flush=True) try: result = subprocess.run( [sys.executable, str(PRICE_SCRIPT), "--all-holdings"], capture_output=True, text=True, timeout=30 ) if result.returncode == 0 and result.stdout.strip(): lines = [l for l in result.stdout.strip().split("\n") if l.strip()] print(f"OK ({len(lines)}只持仓)", flush=True) for line in lines[:50]: print(f" {line}", flush=True) else: print(f"WARN: stock_quote stderr={result.stderr[:100]}", flush=True) except Exception as e: print(f"WARN: stock_quote skipped ({e})", flush=True) else: print("WARN: stock_quote.py not found", flush=True) print("采集链完成", flush=True)