默认新浪行情,普通用户无需同花顺token

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-15 11:16:51 +08:00
parent fb61153a4d
commit 314d3206c0
5 changed files with 51 additions and 50 deletions
+26 -9
View File
@@ -1,5 +1,6 @@
"""
行情拉取:优先同花顺 iFinD HTTP API,失败或未配置时回退新浪
行情拉取:默认新浪(免费,普通用户可用)
同花顺 iFinD HTTP 仅面向机构用户,需单独申请 token,可选开启。
"""
import os
import time
@@ -27,7 +28,23 @@ _token_cache: dict = {"token": "", "expires": 0.0, "refresh": ""}
def _quote_source() -> str:
return os.getenv("QUOTE_SOURCE", "auto").strip().lower()
return os.getenv("QUOTE_SOURCE", "sina").strip().lower()
def _has_ths_token() -> bool:
return bool(_get_refresh_token())
def get_quote_source_label() -> str:
"""界面展示用行情源说明。"""
source = _quote_source()
if source == "sina":
return "新浪(免费)"
if source == "ths":
return "同花顺 iFinD" if _has_ths_token() else "同花顺(未配置 token,无法使用)"
if _has_ths_token():
return "同花顺优先,失败回退新浪"
return "新浪(免费)"
def _sina_headers() -> dict:
@@ -154,23 +171,23 @@ def get_ths_price(ths_full_code: str, refresh_token: str = "") -> Optional[float
def get_price(market_code: str, sina_fallback: str = "") -> Optional[float]:
"""
统一取价入口。
market_code: 同花顺完整代码 ag2608.SHFE(优先
sina_fallback: 新浪代码 nf_AG2608(回退
sina_fallback: 新浪代码 nf_AG2608(普通用户默认使用
market_code: 同花顺完整代码 ag2608.SHFE(仅机构 token 可用时
"""
source = _quote_source()
if source in ("ths", "auto") and market_code and "." in market_code:
# 仅在有 token 且配置为 ths/auto 时才尝试同花顺
use_ths = source == "ths" or (source == "auto" and _has_ths_token())
if use_ths and market_code and "." in market_code:
price = get_ths_price(market_code)
if price is not None:
return price
if source == "ths":
return None
if source == "ths":
return None
if sina_fallback:
return get_sina_price(sina_fallback)
# market_code 本身就是新浪格式
if market_code.startswith("nf_") or market_code.startswith("CFF_RE_"):
return get_sina_price(market_code)