From 34030aa83bee98c1f512cce3a60e0837659007a4 Mon Sep 17 00:00:00 2001 From: xxm Date: Sat, 22 Aug 2026 12:00:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=82=A1=E7=A5=A8=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E6=B2=BB=E7=90=86=E2=80=94=E2=80=94price=5Fmonitor/mofin=5Fdb?= =?UTF-8?q?=E5=86=99=E5=85=A5stocks=E6=97=B6=E5=90=8D=E7=A7=B0=E5=AE=88?= =?UTF-8?q?=E5=8D=AB(stocks=E5=B7=B2=E6=9C=89=E7=9C=9F=E5=90=8D=E2=86=92?= =?UTF-8?q?=E5=A4=8D=E7=94=A8,=E6=97=A0=E2=86=92=E8=85=BE=E8=AE=AFquote?= =?UTF-8?q?=E5=85=9C=E5=BA=95,REPLACE=E4=B8=8D=E5=86=8D=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E7=9C=9F=E5=90=8D),=E5=9B=9E=E5=A1=ABstocks=20341=E8=A1=8C+can?= =?UTF-8?q?didates=20201=E8=A1=8C=E7=9C=9F=E5=90=8D,=E6=8C=87=E6=95=B0?= =?UTF-8?q?=E8=A1=8C=E6=AD=A3=E5=90=8D,=E5=88=A06=E8=A1=8C=E5=9E=83?= =?UTF-8?q?=E5=9C=BEcode=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/profile-scripts/mofin_db.py | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/deploy/profile-scripts/mofin_db.py b/deploy/profile-scripts/mofin_db.py index 618bf4fa..f82de1d8 100644 --- a/deploy/profile-scripts/mofin_db.py +++ b/deploy/profile-scripts/mofin_db.py @@ -693,6 +693,14 @@ def write_klines(conn: sqlite3.Connection, code: str, name: str, exchange, stype = "SZ", "A" # stocks 表(INSERT OR REPLACE) + # 2026-08-22 名称治理:name 缺失/等于代码时保留已有真名,避免 REPLACE 覆盖好数据 + if not name or str(name) == str(code): + try: + _r = conn.execute("SELECT name FROM stocks WHERE code=?", (str(code),)).fetchone() + if _r and _r[0] and _r[0] != str(code): + name = _r[0] + except Exception: + pass conn.execute( "INSERT OR REPLACE INTO stocks (code, name, exchange, type, updated_at) VALUES (?, ?, ?, ?, ?)", (code, name, exchange, stype, datetime.now().isoformat())) @@ -1085,6 +1093,39 @@ def query_latest_market(conn: sqlite3.Connection) -> dict: # 通用工具 # ═══════════════════════════════════════════════════════════════════ +def fetch_stock_name_tencent(code: str) -> str | None: + """腾讯 quote 单票查名(数据层专用,2026-08-22 名称治理)。 + + A股按 6/9→sh、其余→sz;港股5位0/1开头→hk。查到真名返回,查不到返回 None。 + """ + import urllib.request + code = str(code).strip() + if not code: + return None + if len(code) == 5 and code[0] in "01": + sym = "hk" + code + elif code[0] in "69": + sym = "sh" + code + else: + sym = "sz" + code + try: + url = f"http://qt.gtimg.cn/q={sym}" + req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"}) + with urllib.request.urlopen(req, timeout=5) as resp: + text = resp.read().decode("gbk", errors="ignore") + # 格式: v_sh600110="1~诺德股份~600110~..." + if '="' in text and "~" in text: + payload = text.split('="', 1)[1] + parts = payload.split("~") + if len(parts) > 1: + name = parts[1].strip() + if name and name != code: + return name + except Exception: + pass + return None + + def get_price_from_db(code: str) -> tuple[float | None, float | None]: """从 DB 读取最新价格(price_monitor 维护)。 返回 (price, change_pct) 或 (None, None)