diff --git a/deploy/profile-scripts/batch_reassess.py b/deploy/profile-scripts/batch_reassess.py index 7d5d5710..0b765df3 100644 --- a/deploy/profile-scripts/batch_reassess.py +++ b/deploy/profile-scripts/batch_reassess.py @@ -164,17 +164,30 @@ def build_prompt(data): except: pass - # 拉取近期消息面 + # 拉取近期消息面(按代码/行业名匹配,无个股消息则带大盘级消息) _news_note = "暂无近期消息" try: import sqlite3 as _sq _db = _sq.connect("/home/hmo/MoFin/data/mofin.db") - _nr = _db.execute( - "SELECT summary, overall_sentiment, created_at FROM signal_news " - "WHERE (code=? OR sector LIKE ?) AND overall_sentiment IN ('利好','利空') " - "ORDER BY id DESC LIMIT 3", - (data['code'], f'%{data.get("name","")[:4]}%') - ).fetchall() + _sector_name = "" + try: + _sr = _db.execute( + "SELECT sector FROM stock_sectors WHERE code=? LIMIT 1", (data['code'],)).fetchone() + _sector_name = _sr[0] if _sr else "" + except Exception: + pass + _nr = [] + if _sector_name: + _nr = _db.execute( + "SELECT summary, overall_sentiment, created_at FROM signal_news " + "WHERE (code=? OR sector LIKE ?) AND overall_sentiment IN ('利好','利空') " + "ORDER BY id DESC LIMIT 3", + (data['code'], f'%{_sector_name}%')).fetchall() + if not _nr: + _nr = _db.execute( + "SELECT summary, overall_sentiment, created_at FROM signal_news " + "WHERE overall_sentiment IN ('利好','利空') " + "ORDER BY id DESC LIMIT 2").fetchall() if _nr: _news_note = " | ".join([f"{r[2][:10]} {r[1]} {r[0][:40]}" for r in _nr]) _db.close() diff --git a/deploy/profile-scripts/capital_flow_collector.py b/deploy/profile-scripts/capital_flow_collector.py index 22477485..86997cb3 100644 --- a/deploy/profile-scripts/capital_flow_collector.py +++ b/deploy/profile-scripts/capital_flow_collector.py @@ -22,7 +22,7 @@ RATE_LIMIT = Semaphore(5) MIN_INTERVAL = 0.3 _last_req = 0 -def _rate_limited_request(url): +def _rate_limited_request(url, referer="https://data.eastmoney.com/"): """带速率限制的HTTP GET,用Semaphore控制并发数""" global _last_req with RATE_LIMIT: @@ -31,7 +31,7 @@ def _rate_limited_request(url): time.sleep(MIN_INTERVAL - elapsed) proxy_handler = urllib.request.ProxyHandler({}) opener = urllib.request.build_opener(proxy_handler) - req = Request(url, headers={"User-Agent": UA, "Referer": "https://data.eastmoney.com/"}) + req = Request(url, headers={"User-Agent": UA, "Referer": referer}) try: resp = opener.open(req, timeout=8) _last_req = time.time() @@ -47,30 +47,38 @@ def secid(code): return f"0.{code}" def fetch_flow(code, days=5): - """拉取个股近N日资金流(带限速+代理绕过)""" - sid = secid(code) - url = f"http://push2his.eastmoney.com/api/qt/stock/fflow/daykline/get?secid={sid}&fields1=f1,f2,f3,f7&fields2=f51,f52,f53,f54,f55,f56,f57&lmt={days}" - data = _rate_limited_request(url) - if not data: + """拉取个股近N日资金流(Sina MoneyFlow — eastmoney 在 246 不可达 2026-07-22)""" + code = str(code).strip() + if code.startswith(("6", "9")): + dm = f"sh{code}" + elif code.startswith(("0", "1")) and len(code) == 5: + dm = f"hk{code}" # 港股 sina 不支持资金流,直接返回 None return None - klines = data.get("data") or {} - if isinstance(klines, dict): - klines = klines.get("klines", []) - if not klines: + else: + dm = f"sz{code}" + url = ("https://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/" + f"MoneyFlow.ssl_qsfx_lscjfb?daima={dm}") + data = _rate_limited_request(url, referer="https://finance.sina.com.cn") + if not data or not isinstance(data, list) or not data: return None result = [] - for k in klines: - p = k.split(",") - if len(p) >= 7: + for d in data[-days:]: + try: + r0n = float(d.get("r0_net", 0) or 0) # 超大单净流入(元) + r1n = float(d.get("r1_net", 0) or 0) # 大单净流入(元) + r2n = float(d.get("r2_net", 0) or 0) # 中单净流入(元) + r3n = float(d.get("r3_net", 0) or 0) # 小单净流入(元) result.append({ - "date": p[0], - "main_net": float(p[1]), # 主力净流入(元) - "super_large": float(p[2]), # 超大单净流入(元) - "large": float(p[3]), # 大单净流入(元) - "medium": float(p[4]), # 中单净流入(元) - "small": float(p[5]), # 小单净流入(元) + "date": d.get("opendate", ""), + "main_net": r0n + r1n, + "super_large": r0n, + "large": r1n, + "medium": r2n, + "small": r3n, }) - return result + except Exception: + continue + return result or None def fetch_flow_intraday(code): """拉取当日分时资金流(用于盘中判断)"""