dsh-stock-analysis 0.2.10 → 0.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1286,12 +1286,128 @@ def _quote_stale_marker(market: str):
1286
1286
  }
1287
1287
 
1288
1288
 
1289
+ _YOUZHIYOUXING_TIAA = {
1290
+ "TIAA001001": "长钱账户",
1291
+ "TIAA001002": "海外长钱",
1292
+ "TIAA001004": "稳钱账户",
1293
+ }
1294
+
1295
+
1296
+ def _quote_tiaa(symbol: str) -> dict:
1297
+ import requests
1298
+
1299
+ disclosure_name = _YOUZHIYOUXING_TIAA.get(symbol)
1300
+ result = {
1301
+ "symbol": symbol,
1302
+ "name": None,
1303
+ "asset_type": "investment_advisory_portfolio",
1304
+ "type_description": "雪球投顾组合,无统一单位净值",
1305
+ "manager": None,
1306
+ "advisor_code": f"A{symbol[4:7]}",
1307
+ "advisor_org": None,
1308
+ "advisor_mapping_basis": "tiaa_prefix_best_effort",
1309
+ "advisor_source_url": None,
1310
+ "advisor_retrieved_at": None,
1311
+ "nav": None,
1312
+ "example_account_performance": None,
1313
+ "quote_type": None,
1314
+ "exchange": None,
1315
+ "source": None,
1316
+ "source_url": f"https://xueqiu.com/S/{symbol}",
1317
+ "data_as_of": None,
1318
+ "fetched_at": datetime.now(timezone.utc).isoformat(),
1319
+ "official_disclosures": [],
1320
+ "public_search_results": [],
1321
+ "disclosure_provider": "youzhiyouxing" if disclosure_name else None,
1322
+ "disclosure_status": "unavailable" if disclosure_name else "unsupported_provider",
1323
+ "disclosures_partial": True,
1324
+ "disclosures_as_of": None,
1325
+ "note": (
1326
+ "投顾组合无统一净值;主理人及完整业绩/持仓需在对应投顾平台登录后查询;"
1327
+ "官方公开披露仅为部分历史信息,持有人收益随买入时点和调仓跟进不同"
1328
+ ),
1329
+ }
1330
+ try:
1331
+ with requests.Session() as session:
1332
+ session.get(
1333
+ "https://xueqiu.com/",
1334
+ headers={"User-Agent": "Mozilla/5.0"},
1335
+ timeout=10,
1336
+ allow_redirects=True,
1337
+ ).raise_for_status()
1338
+ response = session.get(
1339
+ "https://stock.xueqiu.com/v5/stock/quote.json",
1340
+ params={"symbol": symbol, "extend": "detail"},
1341
+ headers={"User-Agent": "Mozilla/5.0", "Referer": "https://xueqiu.com/"},
1342
+ timeout=10,
1343
+ )
1344
+ response.raise_for_status()
1345
+ payload = response.json()
1346
+ if payload.get("error_code") not in (None, 0):
1347
+ raise ValueError("xueqiu portfolio metadata unavailable")
1348
+ quote = (payload.get("data") or {}).get("quote") or {}
1349
+ if quote.get("symbol") != symbol:
1350
+ raise ValueError("xueqiu returned different portfolio metadata")
1351
+ result.update(
1352
+ name=quote.get("name"),
1353
+ quote_type=quote.get("type"),
1354
+ exchange=quote.get("exchange"),
1355
+ source="xueqiu",
1356
+ data_as_of=_unix_quote_time(quote.get("timestamp")),
1357
+ )
1358
+ except Exception:
1359
+ result["warnings"] = ["xueqiu portfolio name unavailable; type guidance remains available"]
1360
+ if disclosure_name and result["name"] in (None, disclosure_name):
1361
+ try:
1362
+ from tiaa_disclosures import public_disclosures
1363
+
1364
+ disclosure = public_disclosures(symbol, disclosure_name)
1365
+ result["official_disclosures"] = disclosure["items"]
1366
+ result["disclosures_as_of"] = disclosure["as_of"]
1367
+ if disclosure["items"]:
1368
+ result["disclosure_status"] = "partial_public"
1369
+ except Exception:
1370
+ result.setdefault("warnings", []).append(
1371
+ "official public disclosures unavailable; type guidance remains available"
1372
+ )
1373
+ elif disclosure_name and result["name"]:
1374
+ result["disclosure_status"] = "unsupported_provider"
1375
+ result.setdefault("warnings", []).append("portfolio name does not match the registered disclosure source")
1376
+ else:
1377
+ from tiaa_disclosures import search_public
1378
+
1379
+ result["public_search_results"] = search_public(symbol, result["name"])
1380
+ if result["public_search_results"]:
1381
+ result["disclosure_provider"] = "web_search"
1382
+ result["disclosure_status"] = "search_partial"
1383
+ try:
1384
+ from tiaa_disclosures import advisor_metadata
1385
+
1386
+ result.update(advisor_metadata(symbol))
1387
+ except Exception:
1388
+ result.setdefault("warnings", []).append("advisor organization metadata unavailable")
1389
+ return result
1390
+
1391
+
1289
1392
  def cmd_quote(args):
1393
+ if re.fullmatch(r"TIAA\d{6}", args.symbol, re.IGNORECASE):
1394
+ return _quote_tiaa(args.symbol.upper())
1290
1395
  market = detect_market(args.symbol)
1291
1396
  try:
1292
1397
  result = quote_a(args.symbol, evidence=True) if market == "A" else quote_yf(args.symbol, evidence=True)
1293
1398
  except Exception as e:
1294
- return {"error": str(e)}
1399
+ result = {"error": str(e)}
1400
+ # Exchange quotes cannot see open-end funds. Stock-only prefixes (SH 60/68, ChiNext 30, BSE) keep the bare error.
1401
+ if (
1402
+ isinstance(result, dict)
1403
+ and "error" in result
1404
+ and re.fullmatch(r"\d{6}", args.symbol)
1405
+ and not re.match(r"(?:60|68|30|43|83|87|92)", args.symbol)
1406
+ ):
1407
+ result["hint"] = (
1408
+ f"{args.symbol} 若为场外公募基金,请改用 get_fund_quote(stock_data.py fund_quote {args.symbol})"
1409
+ )
1410
+ return result
1295
1411
  if isinstance(result, dict) and "error" not in result:
1296
1412
  marker = _quote_stale_marker(market)
1297
1413
  if marker:
@@ -1322,6 +1438,109 @@ def cmd_quote(args):
1322
1438
  return result
1323
1439
 
1324
1440
 
1441
+ # --------------- open-end fund quote ---------------
1442
+
1443
+
1444
+ def _fund_js_value(text: str, name: str):
1445
+ match = re.search(rf"\b{re.escape(name)}\s*=\s*", text)
1446
+ if not match:
1447
+ raise ValueError(f"eastmoney fund data missing {name}")
1448
+ try:
1449
+ return json.JSONDecoder().raw_decode(text[match.end() :])[0]
1450
+ except json.JSONDecodeError as exc:
1451
+ raise ValueError(f"eastmoney fund data has invalid {name}") from exc
1452
+
1453
+
1454
+ def _fund_nav_date(stamp) -> str:
1455
+ return datetime.fromtimestamp(stamp / 1000, timezone.utc).astimezone(ZoneInfo("Asia/Shanghai")).date().isoformat()
1456
+
1457
+
1458
+ def _fund_estimate(symbol: str) -> dict:
1459
+ import akshare as ak
1460
+
1461
+ with socket_timeout(25):
1462
+ df = ak.fund_value_estimation_em()
1463
+ if df is None or df.empty or "基金代码" not in df.columns:
1464
+ return {}
1465
+ rows = df[df["基金代码"] == symbol]
1466
+ value_col = next((c for c in df.columns if c.endswith("-估算数据-估算值")), None)
1467
+ change_col = next((c for c in df.columns if c.endswith("-估算数据-估算增长率")), None)
1468
+ if rows.empty or not value_col:
1469
+ return {}
1470
+ row = rows.iloc[0]
1471
+ estimated_nav = _parse_float(row.get(value_col))
1472
+ if estimated_nav is None:
1473
+ return {}
1474
+ return {
1475
+ "estimated_nav": estimated_nav,
1476
+ "estimated_change_pct": _parse_float(str(row.get(change_col)).rstrip("%")) if change_col else None,
1477
+ "estimated_at": value_col.split("-估算数据-", 1)[0],
1478
+ "estimate_status": "available",
1479
+ }
1480
+
1481
+
1482
+ def cmd_fund_quote(args):
1483
+ symbol = args.symbol
1484
+ if not re.fullmatch(r"\d{6}", symbol):
1485
+ return {"error": "open-end fund symbol must be exactly 6 digits"}
1486
+ try:
1487
+ import requests
1488
+
1489
+ response = requests.get(f"https://fund.eastmoney.com/pingzhongdata/{symbol}.js", timeout=15)
1490
+ response.raise_for_status()
1491
+ text = response.text
1492
+ if _fund_js_value(text, "fS_code") != symbol:
1493
+ raise ValueError(f"eastmoney returned a different fund for {symbol}")
1494
+ name = _fund_js_value(text, "fS_name")
1495
+ nav_rows = _fund_js_value(text, "Data_netWorthTrend")
1496
+ accumulated_rows = _fund_js_value(text, "Data_ACWorthTrend")
1497
+ if not nav_rows:
1498
+ raise ValueError(f"eastmoney returned no NAV for {symbol}")
1499
+ latest = nav_rows[-1]
1500
+ nav_date = _fund_nav_date(latest["x"])
1501
+ accumulated_by_date = {_fund_nav_date(row[0]): row[1] for row in accumulated_rows}
1502
+ today = datetime.now(ZoneInfo("Asia/Shanghai")).date().isoformat()
1503
+ estimate = {
1504
+ "estimated_nav": None,
1505
+ "estimated_change_pct": None,
1506
+ "estimated_at": None,
1507
+ "estimate_status": "unavailable",
1508
+ }
1509
+ warnings = []
1510
+ try:
1511
+ estimate.update(_fund_estimate(symbol))
1512
+ except Exception:
1513
+ warnings.append("estimated NAV provider request failed; published NAV remains available")
1514
+ result = _clean_row(
1515
+ {
1516
+ "symbol": symbol,
1517
+ "name": name,
1518
+ "asset_type": "open_end_fund",
1519
+ "nav": latest.get("y"),
1520
+ "accumulated_nav": accumulated_by_date.get(nav_date),
1521
+ "change_pct": latest.get("equityReturn"),
1522
+ "nav_date": nav_date,
1523
+ "data_as_of": nav_date,
1524
+ "as_of": nav_date,
1525
+ "as_of_basis": "provider",
1526
+ "stale": nav_date < today,
1527
+ "source": "eastmoney",
1528
+ "source_url": f"https://fund.eastmoney.com/{symbol}.html",
1529
+ "fetched_at": datetime.now(timezone.utc).isoformat(),
1530
+ "note": (
1531
+ "nav is published end-of-day NAV, not a real-time price; QDII disclosure may lag "
1532
+ "1-2 trading days; estimated_nav is separate and may be unavailable"
1533
+ ),
1534
+ **estimate,
1535
+ }
1536
+ )
1537
+ if warnings:
1538
+ result["warnings"] = warnings
1539
+ return result
1540
+ except Exception as e:
1541
+ return {"error": str(e)}
1542
+
1543
+
1325
1544
  # --------------- capital_flow ---------------
1326
1545
 
1327
1546
 
@@ -1872,7 +2091,8 @@ def snapshot_hk() -> list:
1872
2091
  try:
1873
2092
  import akshare as ak
1874
2093
 
1875
- df = _akshare_retry(ak.stock_hk_spot_em)
2094
+ with socket_timeout(25):
2095
+ df = _akshare_retry(ak.stock_hk_spot_em)
1876
2096
  col_map = {
1877
2097
  "代码": "symbol",
1878
2098
  "名称": "name",
@@ -1900,7 +2120,8 @@ def snapshot_us() -> list:
1900
2120
  try:
1901
2121
  import akshare as ak
1902
2122
 
1903
- df = _akshare_retry(ak.stock_us_spot_em)
2123
+ with socket_timeout(25):
2124
+ df = _akshare_retry(ak.stock_us_spot_em)
1904
2125
  col_map = {
1905
2126
  "代码": "symbol",
1906
2127
  "名称": "name",
@@ -1996,33 +2217,39 @@ def cmd_market_indices(args):
1996
2217
  )
1997
2218
  return results
1998
2219
  elif region in _REGION_INDEX_SYMBOLS:
2220
+ from concurrent.futures import ThreadPoolExecutor
2221
+
1999
2222
  import yfinance as yf
2000
2223
 
2001
2224
  symbols = _REGION_INDEX_SYMBOLS[region]
2002
- results = []
2003
- for sym, name in symbols.items():
2225
+
2226
+ def fetch_index(item):
2227
+ sym, name = item
2004
2228
  try:
2005
- t = yf.Ticker(sym)
2006
- info = t.info
2007
- results.append(
2008
- _clean_row(
2009
- {
2010
- "code": sym,
2011
- "name": name,
2012
- "price": info.get("regularMarketPrice"),
2013
- "change": info.get("regularMarketChange"),
2014
- "change_pct": info.get("regularMarketChangePercent"),
2015
- "volume": info.get("regularMarketVolume"),
2016
- "high": info.get("regularMarketDayHigh"),
2017
- "low": info.get("regularMarketDayLow"),
2018
- "open": info.get("regularMarketOpen"),
2019
- "prev_close": info.get("regularMarketPreviousClose"),
2020
- }
2021
- )
2229
+ info = yf.Ticker(sym).info
2230
+ return _clean_row(
2231
+ {
2232
+ "code": sym,
2233
+ "name": name,
2234
+ "price": info.get("regularMarketPrice"),
2235
+ "change": info.get("regularMarketChange"),
2236
+ "change_pct": info.get("regularMarketChangePercent"),
2237
+ "volume": info.get("regularMarketVolume"),
2238
+ "high": info.get("regularMarketDayHigh"),
2239
+ "low": info.get("regularMarketDayLow"),
2240
+ "open": info.get("regularMarketOpen"),
2241
+ "prev_close": info.get("regularMarketPreviousClose"),
2242
+ "source": "yfinance",
2243
+ "data_as_of": _unix_quote_time(info.get("regularMarketTime")),
2244
+ "fetched_at": datetime.now(timezone.utc).isoformat(),
2245
+ }
2022
2246
  )
2023
2247
  except Exception:
2024
- results.append({"code": sym, "name": name, "note": "data unavailable"})
2025
- return results
2248
+ return {"code": sym, "name": name, "note": "data unavailable"}
2249
+
2250
+ # Preserve provider order while overlapping independent quote requests.
2251
+ with ThreadPoolExecutor(max_workers=min(4, len(symbols))) as pool:
2252
+ return list(pool.map(fetch_index, symbols.items()))
2026
2253
  return {"error": f"Unknown region: {region}"}
2027
2254
  except Exception as e:
2028
2255
  return {"error": str(e)}
@@ -3382,6 +3609,9 @@ def main():
3382
3609
  p_quote = sub.add_parser("quote")
3383
3610
  p_quote.add_argument("symbol")
3384
3611
 
3612
+ p_fund_quote = sub.add_parser("fund_quote")
3613
+ p_fund_quote.add_argument("symbol")
3614
+
3385
3615
  p_cf = sub.add_parser("capital_flow")
3386
3616
  p_cf.add_argument("symbol", nargs="?", default="")
3387
3617
  p_cf.add_argument("--mode", default="detail", choices=["detail", "summary", "sector_flow"])
@@ -3451,6 +3681,7 @@ def main():
3451
3681
  dispatch = {
3452
3682
  "kline": cmd_kline,
3453
3683
  "quote": cmd_quote,
3684
+ "fund_quote": cmd_fund_quote,
3454
3685
  "capital_flow": cmd_capital_flow,
3455
3686
  "news": cmd_news,
3456
3687
  "financials": cmd_financials,
@@ -0,0 +1,263 @@
1
+ """Public, login-free TIAA disclosures from the official YouZhiYouXing site."""
2
+
3
+ import re
4
+ import sys
5
+ import threading
6
+ import time
7
+ import xml.etree.ElementTree as ET
8
+ from concurrent.futures import TimeoutError as FutureTimeoutError
9
+ from datetime import datetime, timezone
10
+ from html import unescape
11
+ from html.parser import HTMLParser
12
+ from pathlib import Path
13
+ from urllib.parse import urlparse
14
+
15
+ import requests
16
+
17
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
18
+ from _cache import cached # noqa: E402
19
+
20
+ _SITEMAP = "https://youzhiyouxing.cn/sitemap.xml"
21
+ _ADVISOR_URL = "https://www.csisc.cn/zbscbzw/c100264/list_jjbm.shtml"
22
+ _ARTICLE_URL = re.compile(r"https://youzhiyouxing\.cn/(?:n/)?lite-posts/(\d+)(?:[/?#].*)?")
23
+ _DISCLOSURE_TERMS = ("示例账户", "成分基金", "配置", "业绩", "收益", "调仓", "持仓")
24
+ _MAX_ARTICLES = 40
25
+ _DISCLOSURE_DEADLINE = 30
26
+
27
+
28
+ class _DisclosureTimeout(Exception):
29
+ """A bounded disclosure attempt failed and must not be cached as an empty result."""
30
+
31
+
32
+ def _until(deadline, fn, *args):
33
+ """Run a blocking provider call in a daemon thread and stop waiting at the deadline.
34
+
35
+ A provider that ignores its own timeout can then neither stretch the request nor keep
36
+ the CLI process alive after it has answered.
37
+ """
38
+ box = {}
39
+
40
+ def run():
41
+ try:
42
+ box["value"] = fn(*args)
43
+ except BaseException as exc: # re-raised in the caller's thread
44
+ box["error"] = exc
45
+
46
+ worker = threading.Thread(target=run, daemon=True)
47
+ worker.start()
48
+ worker.join(max(0.0, deadline - time.monotonic()))
49
+ if worker.is_alive():
50
+ raise _DisclosureTimeout
51
+ if "error" in box:
52
+ raise box["error"]
53
+ return box.get("value")
54
+
55
+
56
+ class _ArticleParser(HTMLParser):
57
+ def __init__(self):
58
+ super().__init__(convert_charrefs=True)
59
+ self.meta = {}
60
+ self.parts = []
61
+ self.ignored = 0
62
+
63
+ def handle_starttag(self, tag, attrs):
64
+ if tag in ("script", "style"):
65
+ self.ignored += 1
66
+ if tag == "meta":
67
+ values = dict(attrs)
68
+ if values.get("property") in ("og:title", "og:description"):
69
+ self.meta[values["property"]] = values.get("content", "")
70
+ if tag in ("p", "div", "h1", "h2", "h3", "li", "br"):
71
+ self.parts.append("\n")
72
+
73
+ def handle_endtag(self, tag):
74
+ if tag in ("script", "style") and self.ignored:
75
+ self.ignored -= 1
76
+ if tag in ("p", "div", "h1", "h2", "h3", "li"):
77
+ self.parts.append("\n")
78
+
79
+ def handle_data(self, data):
80
+ if not self.ignored:
81
+ self.parts.append(data)
82
+
83
+ def text(self):
84
+ lines = []
85
+ for line in "".join(self.parts).splitlines():
86
+ clean = re.sub(r"\s+", " ", line).strip()
87
+ if clean:
88
+ lines.append(clean)
89
+ title = self.meta.get("og:title")
90
+ title_lines = [index for index, line in enumerate(lines) if line == title]
91
+ if title_lines:
92
+ lines = lines[title_lines[-1] + 1 :]
93
+ comments = next((index for index, line in enumerate(lines) if line.endswith("条回应")), len(lines))
94
+ return "\n".join(lines[:comments])
95
+
96
+
97
+ def _sitemap_urls():
98
+ response = requests.get(_SITEMAP, headers={"User-Agent": "Mozilla/5.0"}, timeout=(2, 8))
99
+ response.raise_for_status()
100
+ root = ET.fromstring(response.content)
101
+ namespace = {"s": "http://www.sitemaps.org/schemas/sitemap/0.9"}
102
+ entries = []
103
+ for node in root.findall("s:url", namespace):
104
+ location = node.findtext("s:loc", default="", namespaces=namespace)
105
+ match = _ARTICLE_URL.fullmatch(location)
106
+ if match:
107
+ entries.append((node.findtext("s:lastmod", default="", namespaces=namespace), match.group(1)))
108
+ return [
109
+ f"https://youzhiyouxing.cn/n/lite-posts/{article_id}" for _, article_id in sorted(entries, reverse=True)[:40]
110
+ ]
111
+
112
+
113
+ def _search_urls(portfolio_name, budget_seconds=45):
114
+ try:
115
+ from search_intel import search_news
116
+
117
+ query = f"site:youzhiyouxing.cn/n/lite-posts/ {portfolio_name} 示例账户 成分基金"
118
+ result = search_news(query, max_results=10, budget_seconds=budget_seconds)
119
+ if any("time budget exhausted" in str(error).lower() for error in result.get("engines_errors", {}).values()):
120
+ raise _DisclosureTimeout
121
+ urls = []
122
+ for row in result.get("results", []):
123
+ match = _ARTICLE_URL.fullmatch(row.get("url", ""))
124
+ if match:
125
+ urls.append(f"https://youzhiyouxing.cn/n/lite-posts/{match.group(1)}")
126
+ return urls
127
+ except Exception:
128
+ return []
129
+
130
+
131
+ def _article(url, portfolio_name):
132
+ try:
133
+ response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=(1, 3))
134
+ response.raise_for_status()
135
+ parser = _ArticleParser()
136
+ parser.feed(response.text)
137
+ text = parser.text()
138
+ if portfolio_name not in text or not any(term in text for term in _DISCLOSURE_TERMS):
139
+ return None
140
+ date_match = re.search(r"(20\d{2})\s*年\s*(\d{1,2})\s*月\s*(\d{1,2})\s*日", response.text)
141
+ published_at = None
142
+ if date_match:
143
+ published_at = "-".join([date_match.group(1), *(part.zfill(2) for part in date_match.groups()[1:])])
144
+ return {
145
+ "title": parser.meta.get("og:title", ""),
146
+ "url": url,
147
+ "published_at": published_at,
148
+ "summary": parser.meta.get("og:description", ""),
149
+ "text": text[:3000],
150
+ "source": "youzhiyouxing",
151
+ "partial": True,
152
+ }
153
+ except Exception:
154
+ return None
155
+
156
+
157
+ def advisor_metadata(symbol):
158
+ advisor_code = f"A{symbol[4:7]}"
159
+
160
+ def fetch():
161
+ response = requests.get(_ADVISOR_URL, headers={"User-Agent": "Mozilla/5.0"}, timeout=15)
162
+ response.raise_for_status()
163
+ text = response.content.decode("utf-8-sig")
164
+ rows = re.findall(
165
+ r"<tr\b[^>]*>\s*<td\b[^>]*>\s*\d+\s*</td>\s*"
166
+ r"<td\b[^>]*>\s*(.*?)\s*</td>\s*<td\b[^>]*>\s*(A\d{3})\s*</td>",
167
+ text,
168
+ re.S,
169
+ )
170
+ return {
171
+ "organizations": {code: unescape(re.sub(r"<[^>]+>", "", name)).strip() for name, code in rows},
172
+ "retrieved_at": datetime.now(timezone.utc).isoformat(),
173
+ }
174
+
175
+ data = cached("csisc-fund-advisor-organizations", 24 * 60 * 60, fetch)
176
+ return {
177
+ "advisor_code": advisor_code,
178
+ "advisor_org": data["organizations"].get(advisor_code),
179
+ "advisor_mapping_basis": "tiaa_prefix_best_effort",
180
+ "advisor_source_url": _ADVISOR_URL,
181
+ "advisor_retrieved_at": data["retrieved_at"],
182
+ }
183
+
184
+
185
+ def public_disclosures(symbol, portfolio_name):
186
+ def fetch():
187
+ deadline = time.monotonic() + _DISCLOSURE_DEADLINE
188
+ urls = _sitemap_urls()
189
+ search_results = []
190
+
191
+ timeout_state = {"search": False, "articles": False}
192
+ if time.monotonic() < deadline:
193
+ try:
194
+ search_results = _until(
195
+ deadline, _search_urls, portfolio_name, max(1, int(deadline - time.monotonic()))
196
+ )
197
+ # concurrent.futures.TimeoutError only aliases the builtin from Python 3.11.
198
+ except (TimeoutError, FutureTimeoutError, _DisclosureTimeout):
199
+ timeout_state["search"] = True
200
+ if time.monotonic() >= deadline:
201
+ timeout_state["search"] = True
202
+ else:
203
+ timeout_state["search"] = True
204
+ urls = list(dict.fromkeys([*search_results, *urls]))[:_MAX_ARTICLES]
205
+ items = []
206
+ for url in urls:
207
+ if time.monotonic() >= deadline:
208
+ timeout_state["articles"] = True
209
+ break
210
+ try:
211
+ item = _until(deadline, _article, url, portfolio_name)
212
+ if item:
213
+ items.append(item)
214
+ except _DisclosureTimeout:
215
+ timeout_state["articles"] = True
216
+ break
217
+ except (TimeoutError, FutureTimeoutError):
218
+ timeout_state["articles"] = True
219
+ if time.monotonic() >= deadline and urls:
220
+ timeout_state["articles"] = True
221
+ items.sort(key=lambda item: item["published_at"] or "", reverse=True)
222
+ items = items[:3]
223
+ result = {
224
+ "items": items,
225
+ "as_of": max((item["published_at"] for item in items if item["published_at"]), default=None),
226
+ }
227
+ if any(timeout_state.values()):
228
+ if not items:
229
+ raise _DisclosureTimeout
230
+ result["error"] = "disclosure_timeout"
231
+ return result
232
+
233
+ return cached(f"tiaa-public-disclosures:{symbol}", 6 * 60 * 60, fetch)
234
+
235
+
236
+ def search_public(symbol, portfolio_name):
237
+ def fetch():
238
+ try:
239
+ from search_intel import search_news
240
+
241
+ label = portfolio_name or symbol
242
+ result = search_news(f'"{symbol}" "{label}" 示例账户 成分基金 业绩', max_results=3)
243
+ rows = []
244
+ for row in result.get("results", []):
245
+ parsed = urlparse(row.get("url", ""))
246
+ if parsed.scheme not in ("http", "https") or not parsed.netloc:
247
+ continue
248
+ rows.append(
249
+ {
250
+ "title": row.get("title", ""),
251
+ "url": row["url"],
252
+ "snippet": row.get("snippet", ""),
253
+ "source": row.get("source", ""),
254
+ "publisher": parsed.netloc,
255
+ "verification": "unverified_search_result",
256
+ "partial": True,
257
+ }
258
+ )
259
+ return rows
260
+ except Exception:
261
+ return []
262
+
263
+ return cached(f"tiaa-public-search:{symbol}", 6 * 60 * 60, fetch)