Skip to content

Give your AI agent eyes to see the entire internet. Read & search Twitter, Reddit, YouTube, GitHub, Bilibili, XiaoHongShu — one CLI, zero API fees.

fix(xueqiu): return detail quote fields safely

Pnantaa0981830a86
diff --git a/agent_reach/channels/xueqiu.py b/agent_reach/channels/xueqiu.py
index c5bc528d..a87a73b4 100644
--- a/agent_reach/channels/xueqiu.py
+++ b/agent_reach/channels/xueqiu.py
@@ -71,45 +71,13 @@ def _load_cookies_from_config() -> bool:
return False
-def _load_cookies_from_browser() -> bool:
- """Try to silently load Xueqiu cookies from the local Chrome browser.
-
- Only succeeds when browser_cookie3 is installed AND the user is logged in
- (xq_a_token present). Failures are silently ignored so that agents without
- a local browser keep working.
- """
- try:
- try:
- import rookiepy
- cookies = rookiepy.chrome([".xueqiu.com"])
- if not any(c.get("name") == "xq_a_token" for c in cookies):
- return False
- for c in cookies:
- name = c.get("name")
- value = c.get("value")
- if name and value is not None:
- _inject_cookie_string(f"{name}={value}")
- return True
- except ImportError:
- import browser_cookie3
- cookies = list(browser_cookie3.chrome(domain_name=".xueqiu.com"))
- if not any(c.name == "xq_a_token" for c in cookies):
- return False
- for c in cookies:
- _cookie_jar.set_cookie(c)
- return True
- except Exception:
- return False
-
-
def _ensure_cookies() -> None:
- """Populate session cookies using the best available source.
+ """Populate session cookies without touching browser credential stores.
Priority order:
1. Saved cookie string in ~/.agent-reach/config.yaml (set by configure --from-browser)
- 2. Live Chrome browser cookies via rookiepy/browser_cookie3 (if installed + logged in)
- 3. Homepage visit fallback (only yields anti-DDoS acw_tc,
- not enough for stock APIs)
+ 2. Homepage visit fallback (only yields public session cookies and may not
+ be sufficient when Xueqiu requires a logged-in session)
"""
global _cookies_initialized
if _cookies_initialized:
@@ -117,9 +85,6 @@ def _ensure_cookies() -> None:
if _load_cookies_from_config():
_cookies_initialized = True
return
- if _load_cookies_from_browser():
- _cookies_initialized = True
- return
# Fallback: visit homepage to pick up acw_tc anti-DDoS cookie.
# This is not sufficient for authenticated APIs but avoids hard failures
# on public endpoints that only need the session cookie.
@@ -168,17 +133,20 @@ class XueqiuChannel(Channel):
self.active_backend = None
try:
data = _get_json(
- "https://stock.xueqiu.com/v5/stock/batch/quote.json?symbol=SH000001"
+ "https://stock.xueqiu.com/v5/stock/quote.json"
+ "?symbol=SH601138&extend=detail"
)
- items = (data.get("data") or {}).get("items") or []
- if items:
+ quote = (data.get("data") or {}).get("quote") or {}
+ if quote:
self.active_backend = self.backends[0]
return "ok", "公开 API 可用(行情、搜索、热帖、热股)"
return "warn", "API 响应异常(返回数据为空)"
except Exception as e:
+ detail = str(e).rstrip(": ")
return "warn", (
- f"Xueqiu API 连接失败:{e}。"
- "请先登录雪球后运行:agent-reach configure --from-browser chrome"
+ f"Xueqiu API 连接失败:{detail}。"
+ "如需登录 Cookie,请通过显式配置流程保存后重试;"
+ "doctor 不会自动读取浏览器 Cookie。"
)
# ------------------------------------------------------------------ #
@@ -193,13 +161,15 @@ class XueqiuChannel(Channel):
Returns a dict with keys:
symbol, name, current, percent, chg, high, low, open, last_close,
- volume, amount, market_capital, turnover_rate, pe_ttm, timestamp
+ volume, amount, market_capital, turnover_rate, pe_ttm, pe_forecast,
+ pb, eps, timestamp
"""
+ encoded_symbol = urllib.parse.quote(symbol, safe="")
data = _get_json(
- f"https://stock.xueqiu.com/v5/stock/batch/quote.json?symbol={symbol}"
+ "https://stock.xueqiu.com/v5/stock/quote.json"
+ f"?symbol={encoded_symbol}&extend=detail"
)
- items = (data.get("data") or {}).get("items") or []
- q = (items[0].get("quote") or {}) if items else {}
+ q = (data.get("data") or {}).get("quote") or {}
return {
"symbol": q.get("symbol", symbol),
"name": q.get("name", ""),
@@ -215,6 +185,9 @@ class XueqiuChannel(Channel):
"market_capital": q.get("market_capital"),
"turnover_rate": q.get("turnover_rate"),
"pe_ttm": q.get("pe_ttm"),
+ "pe_forecast": q.get("pe_forecast"),
+ "pb": q.get("pb"),
+ "eps": q.get("eps"),
"timestamp": q.get("timestamp"),
}
diff --git a/tests/test_channels.py b/tests/test_channels.py
index d066066e..95419633 100644
--- a/tests/test_channels.py
+++ b/tests/test_channels.py
@@ -406,9 +406,12 @@ class TestXueqiuChannel:
fake_response_data = {
"data": {
- "items": [
- {"quote": {"symbol": "SH000001", "name": "上证指数", "current": 3200.0}}
- ]
+ "quote": {
+ "symbol": "SH601138",
+ "name": "工业富联",
+ "current": 52.0,
+ "pe_ttm": 38.1,
+ }
}
}
@@ -451,27 +454,26 @@ class TestXueqiuChannel:
fake_data = {
"data": {
- "items": [
- {
- "quote": {
- "symbol": "SH600519",
- "name": "贵州茅台",
- "current": 1800.0,
- "percent": 1.5,
- "chg": 26.6,
- "high": 1810.0,
- "low": 1770.0,
- "open": 1775.0,
- "last_close": 1773.4,
- "volume": 12345678,
- "amount": 22000000000,
- "market_capital": 2260000000000,
- "turnover_rate": 0.098,
- "pe_ttm": 30.5,
- "timestamp": 1700000000000,
- }
- }
- ]
+ "quote": {
+ "symbol": "SH600519",
+ "name": "贵州茅台",
+ "current": 1800.0,
+ "percent": 1.5,
+ "chg": 26.6,
+ "high": 1810.0,
+ "low": 1770.0,
+ "open": 1775.0,
+ "last_close": 1773.4,
+ "volume": 12345678,
+ "amount": 22000000000,
+ "market_capital": 2260000000000,
+ "turnover_rate": 0.098,
+ "pe_ttm": 30.5,
+ "pe_forecast": 25.2,
+ "pb": 8.5,
+ "eps": 59.0,
+ "timestamp": 1700000000000,
+ }
}
}
@@ -670,7 +672,6 @@ class TestXueqiuChannel:
"_load_cookies_from_config",
lambda: (xq_mod._inject_cookie_string("xq_a_token=TESTTOKEN; xq_is_login=1") or True),
)
- monkeypatch.setattr(xq_mod, "_load_cookies_from_browser", lambda: False)
# Patch opener so no real HTTP call is made
class FakeResp:
@@ -685,35 +686,27 @@ class TestXueqiuChannel:
cookie_names = {c.name for c in xq_mod._cookie_jar}
assert "xq_a_token" in cookie_names
- def test_load_cookies_from_browser_rookiepy_path(self, monkeypatch):
- import sys
- import types
-
+ def test_ensure_cookies_uses_public_homepage_fallback(self, monkeypatch):
import agent_reach.channels.xueqiu as xueqiu_mod
- xueqiu_mod._cookie_jar.clear()
- fake_rookiepy = types.SimpleNamespace(
- chrome=lambda domains=None: [
- {
- "name": "xq_a_token",
- "value": "TOKEN_FROM_BROWSER",
- "domain": ".xueqiu.com",
- },
- {
- "name": "xq_is_login",
- "value": "1",
- "domain": ".xueqiu.com",
- },
- ]
- )
- monkeypatch.setitem(sys.modules, "rookiepy", fake_rookiepy)
+ monkeypatch.setattr(xueqiu_mod, "_cookies_initialized", False)
+ monkeypatch.setattr(xueqiu_mod, "_load_cookies_from_config", lambda: False)
+ requested = []
- assert xueqiu_mod._load_cookies_from_browser() is True
- assert any(
- cookie.name == "xq_a_token" and cookie.value == "TOKEN_FROM_BROWSER"
- for cookie in xueqiu_mod._cookie_jar
+ class FakeResponse:
+ pass
+
+ monkeypatch.setattr(
+ xueqiu_mod._opener,
+ "open",
+ lambda req, timeout=None: requested.append(req.full_url) or FakeResponse(),
)
+ xueqiu_mod._ensure_cookies()
+
+ assert requested == ["https://xueqiu.com"]
+ assert xueqiu_mod._cookies_initialized is True
+
def test_get_json_sends_referer_and_browser_ua(self, monkeypatch):
"""_get_json() must send Referer and a browser-like User-Agent."""
import agent_reach.channels.xueqiu as xueqiu_mod
diff --git a/tests/test_xueqiu_channel.py b/tests/test_xueqiu_channel.py
index 2a48d62c..8922f546 100644
--- a/tests/test_xueqiu_channel.py
+++ b/tests/test_xueqiu_channel.py
@@ -10,12 +10,13 @@ offline. Follow-up to #331 — extends dedicated channel coverage after rss
"""
import json
+import sys
+import types
from unittest.mock import patch
from agent_reach.channels import xueqiu as xq
from agent_reach.channels.xueqiu import XueqiuChannel, _strip_html
-
# --- can_handle ---
def test_can_handle_matches_xueqiu_hosts():
@@ -36,17 +37,28 @@ def test_strip_html_removes_tags_and_decodes_entities():
# --- check(): single public endpoint, items present/empty/error ---
-def test_check_ok_when_items_present():
+def test_check_validates_detail_quote_endpoint():
ch = XueqiuChannel()
- with patch.object(xq, "_get_json", return_value={"data": {"items": [{"quote": {}}]}}):
+ requested = []
+ with patch.object(
+ xq,
+ "_get_json",
+ side_effect=lambda url: requested.append(url)
+ or {"data": {"quote": {"symbol": "SH601138", "pe_ttm": 38.1}}},
+ ):
status, message = ch.check()
+
assert status == "ok"
assert ch.active_backend == ch.backends[0]
+ assert requested == [
+ "https://stock.xueqiu.com/v5/stock/quote.json"
+ "?symbol=SH601138&extend=detail"
+ ]
-def test_check_warn_when_items_empty():
+def test_check_warn_when_quote_empty():
ch = XueqiuChannel()
- with patch.object(xq, "_get_json", return_value={"data": {"items": []}}):
+ with patch.object(xq, "_get_json", return_value={"data": {"quote": {}}}):
status, message = ch.check()
assert status == "warn"
assert "为空" in message
@@ -63,22 +75,60 @@ def test_check_warn_on_exception():
assert ch.active_backend is None
+def test_check_never_reads_browser_cookie_store_implicitly(monkeypatch):
+ browser_reads = []
+ fake_rookiepy = types.SimpleNamespace(
+ chrome=lambda *_args, **_kwargs: browser_reads.append("rookiepy") or []
+ )
+ monkeypatch.setitem(sys.modules, "rookiepy", fake_rookiepy)
+ monkeypatch.setattr(xq, "_cookies_initialized", False)
+ monkeypatch.setattr(xq, "_load_cookies_from_config", lambda: False)
+
+ class FakeResponse:
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *_args):
+ return None
+
+ def read(self):
+ return b'{"data":{"quote":{"symbol":"SH601138","pe_ttm":38.1}}}'
+
+ monkeypatch.setattr(xq._opener, "open", lambda *_args, **_kwargs: FakeResponse())
+
+ status, _message = XueqiuChannel().check()
+
+ assert status == "ok"
+ assert browser_reads == []
+
+
# --- get_stock_quote: field mapping + missing-data fallback ---
def test_get_stock_quote_maps_fields():
ch = XueqiuChannel()
- payload = {"data": {"items": [{"quote": {
+ payload = {"data": {"quote": {
"symbol": "SH600519", "name": "贵州茅台", "current": 1700.5,
- "percent": 1.23, "pe_ttm": 30.1,
- }}]}}
- with patch.object(xq, "_get_json", return_value=payload):
+ "percent": 1.23, "volume": 1234, "pe_ttm": 30.1,
+ "pe_forecast": 27.4, "pb": 8.2, "eps": 59.0,
+ }}}
+ requested = []
+ with patch.object(
+ xq, "_get_json", side_effect=lambda url: requested.append(url) or payload
+ ):
q = ch.get_stock_quote("SH600519")
+
+ assert requested == [
+ "https://stock.xueqiu.com/v5/stock/quote.json"
+ "?symbol=SH600519&extend=detail"
+ ]
assert q["symbol"] == "SH600519"
assert q["name"] == "贵州茅台"
assert q["current"] == 1700.5
+ assert q["volume"] == 1234
assert q["pe_ttm"] == 30.1
- # keys are always present even when the source omits them
- assert q["volume"] is None
+ assert q["pe_forecast"] == 27.4
+ assert q["pb"] == 8.2
+ assert q["eps"] == 59.0
def test_get_stock_quote_falls_back_when_no_items():