feat: all remaining Tencent API price fetchers now DB-first (8 files)
This commit is contained in:
@@ -23,6 +23,10 @@ SCANNER_STATE = "/home/hmo/web-dashboard/data/scanner_state.json"
|
|||||||
|
|
||||||
|
|
||||||
def get_price(code):
|
def get_price(code):
|
||||||
|
# DB 优先
|
||||||
|
try: from mofin_db import get_price_from_db; p, _ = get_price_from_db(code); return p if p else 0
|
||||||
|
except: pass
|
||||||
|
# Fallback: 腾讯
|
||||||
mkt = "sh" if code.startswith("6") or code.startswith("5") else "sz"
|
mkt = "sh" if code.startswith("6") or code.startswith("5") else "sz"
|
||||||
url = f"http://qt.gtimg.cn/q={mkt}{code}"
|
url = f"http://qt.gtimg.cn/q={mkt}{code}"
|
||||||
req = Request(url, headers={"User-Agent": "Mozilla/5.0"})
|
req = Request(url, headers={"User-Agent": "Mozilla/5.0"})
|
||||||
|
|||||||
@@ -47,9 +47,18 @@ def save_json(path, data):
|
|||||||
|
|
||||||
|
|
||||||
def fetch_tencent_data(symbols):
|
def fetch_tencent_data(symbols):
|
||||||
"""批量拉腾讯行情,返回 {code: fields_dict}"""
|
"""批量拉行情。DB 优先,腾讯 API fallback"""
|
||||||
if not symbols:
|
if not symbols:
|
||||||
return {}
|
return {}
|
||||||
|
# DB 优先
|
||||||
|
try:
|
||||||
|
from mofin_db import get_prices_batch_from_db
|
||||||
|
db = get_prices_batch_from_db(symbols)
|
||||||
|
if db:
|
||||||
|
return {code: {"name": "", "price": p, "prev_close": 0, "change_pct": chg or 0,
|
||||||
|
"high": 0, "low": 0} for code, (p, chg) in db.items()}
|
||||||
|
except: pass
|
||||||
|
# Fallback: 腾讯
|
||||||
code_map = {}
|
code_map = {}
|
||||||
query_symbols = []
|
query_symbols = []
|
||||||
for c in symbols:
|
for c in symbols:
|
||||||
|
|||||||
@@ -54,6 +54,13 @@ def get_quote(code: str) -> dict:
|
|||||||
prefix = "sz"
|
prefix = "sz"
|
||||||
fields = F
|
fields = F
|
||||||
|
|
||||||
|
# DB 优先
|
||||||
|
try:
|
||||||
|
from mofin_db import get_price_from_db
|
||||||
|
p, chg = get_price_from_db(raw)
|
||||||
|
if p: return {"price": p, "name": name, "code": raw, "change_pct": chg or 0}
|
||||||
|
except: pass
|
||||||
|
# Fallback: 腾讯
|
||||||
url = f"http://qt.gtimg.cn/q={prefix}{raw}"
|
url = f"http://qt.gtimg.cn/q={prefix}{raw}"
|
||||||
try:
|
try:
|
||||||
req = urllib.request.Request(url, headers={
|
req = urllib.request.Request(url, headers={
|
||||||
|
|||||||
@@ -55,7 +55,14 @@ def _market_prefix(code):
|
|||||||
|
|
||||||
|
|
||||||
def get_quote(code):
|
def get_quote(code):
|
||||||
"""获取腾讯API行情数据(带60秒缓存)"""
|
"""获取行情数据。DB 优先(price_monitor 维护),腾讯 API fallback"""
|
||||||
|
# DB 优先
|
||||||
|
try:
|
||||||
|
from mofin_db import get_price_from_db
|
||||||
|
p, chg = get_price_from_db(code)
|
||||||
|
if p: return {"code": code, "price": p, "change_pct": chg or 0}
|
||||||
|
except: pass
|
||||||
|
# Fallback: 腾讯 API
|
||||||
import time
|
import time
|
||||||
_cache = get_quote.__dict__.get("_cache", {})
|
_cache = get_quote.__dict__.get("_cache", {})
|
||||||
now = time.time()
|
now = time.time()
|
||||||
|
|||||||
Reference in New Issue
Block a user