@weaxs/openclaw-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.
@@ -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)