stock-analysis-plugin 0.1.10
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.
- package/README.md +498 -0
- package/package.json +88 -0
- package/pi/index.ts +1142 -0
- package/schemas/market_review_schema.json +176 -0
- package/schemas/report_schema.json +131 -0
- package/scripts/setup-python.mjs +57 -0
- package/scripts/sync-version.sh +56 -0
- package/scripts/venv-python.mjs +20 -0
- package/skills/bottom-volume/SKILL.md +63 -0
- package/skills/bottom-volume/scripts/gather.py +22 -0
- package/skills/box-oscillation/SKILL.md +72 -0
- package/skills/box-oscillation/scripts/gather.py +22 -0
- package/skills/bull-trend/SKILL.md +60 -0
- package/skills/bull-trend/scripts/gather.py +22 -0
- package/skills/chan-theory/SKILL.md +71 -0
- package/skills/chan-theory/scripts/gather.py +22 -0
- package/skills/dragon-head/SKILL.md +69 -0
- package/skills/dragon-head/scripts/gather.py +22 -0
- package/skills/emotion-cycle/SKILL.md +65 -0
- package/skills/emotion-cycle/scripts/gather.py +22 -0
- package/skills/event-driven/SKILL.md +73 -0
- package/skills/event-driven/scripts/gather.py +22 -0
- package/skills/expectation-repricing/SKILL.md +72 -0
- package/skills/expectation-repricing/scripts/gather.py +22 -0
- package/skills/growth-quality/SKILL.md +74 -0
- package/skills/growth-quality/scripts/gather.py +22 -0
- package/skills/hot-theme/SKILL.md +74 -0
- package/skills/hot-theme/scripts/gather.py +22 -0
- package/skills/ma-crossover/SKILL.md +63 -0
- package/skills/ma-crossover/scripts/gather.py +22 -0
- package/skills/market-review/SKILL.md +70 -0
- package/skills/market-review/scripts/gather.py +23 -0
- package/skills/one-yang-three-yin/SKILL.md +74 -0
- package/skills/one-yang-three-yin/scripts/gather.py +22 -0
- package/skills/shrink-pullback/SKILL.md +59 -0
- package/skills/shrink-pullback/scripts/gather.py +22 -0
- package/skills/stock-analysis/SKILL.md +134 -0
- package/skills/stock-analysis/scripts/gather.py +22 -0
- package/skills/stock-screener/SKILL.md +68 -0
- package/skills/stock-screener/scripts/gather.py +22 -0
- package/skills/strategy-backtest/SKILL.md +129 -0
- package/skills/strategy-backtest/scripts/gather.py +23 -0
- package/skills/volume-breakout/SKILL.md +58 -0
- package/skills/volume-breakout/scripts/gather.py +22 -0
- package/skills/wave-theory/SKILL.md +67 -0
- package/skills/wave-theory/scripts/gather.py +22 -0
- package/skills/wisburg-research/SKILL.md +119 -0
- package/strategies/examples/bottom_volume.yaml +47 -0
- package/strategies/examples/box_oscillation.yaml +41 -0
- package/strategies/examples/bull_trend.yaml +42 -0
- package/strategies/examples/dragon_head.yaml +45 -0
- package/strategies/examples/emotion_cycle.yaml +43 -0
- package/strategies/examples/ma_crossover.yaml +34 -0
- package/strategies/examples/rsi_oversold.yaml +35 -0
- package/strategies/examples/shrink_pullback.yaml +45 -0
- package/strategies/examples/volume_breakout.yaml +42 -0
- package/templates/_macros.j2 +90 -0
- package/templates/market_review/full.md.j2 +103 -0
- package/templates/stock_analysis/brief.md.j2 +28 -0
- package/templates/stock_analysis/full.md.j2 +108 -0
- package/tools/__init__.py +0 -0
- package/tools/alert_rules.py +243 -0
- package/tools/anomaly_detect.py +575 -0
- package/tools/backtest.py +764 -0
- package/tools/capabilities.py +108 -0
- package/tools/diagnostics.py +146 -0
- package/tools/gather.py +157 -0
- package/tools/import_parser.py +279 -0
- package/tools/market_regime.py +252 -0
- package/tools/market_review.py +155 -0
- package/tools/name_resolver.py +136 -0
- package/tools/pattern.py +515 -0
- package/tools/position_context.py +161 -0
- package/tools/report_renderer.py +116 -0
- package/tools/requirements.txt +15 -0
- package/tools/risk_screening.py +323 -0
- package/tools/screener.py +219 -0
- package/tools/search_intel.py +504 -0
- package/tools/stock_data.py +1721 -0
- package/tools/technical.py +642 -0
- package/tools/trading_calendar.py +226 -0
- package/tools/volume_analysis.py +178 -0
- package/tools/watchlist.py +66 -0
- package/tools/watchlist_context.py +185 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Search intelligence & social sentiment — multi-engine web search + Reddit/X/A-share sentiment aggregation."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
import sys
|
|
9
|
+
import time as _time
|
|
10
|
+
|
|
11
|
+
# --------------- TTL Cache ---------------
|
|
12
|
+
|
|
13
|
+
_CACHE = {}
|
|
14
|
+
_CACHE_TTL = 600 # 10 minutes
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _cache_get(key: str):
|
|
18
|
+
entry = _CACHE.get(key)
|
|
19
|
+
if entry and (_time.time() - entry["ts"]) < _CACHE_TTL:
|
|
20
|
+
return entry["data"]
|
|
21
|
+
return None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _cache_set(key: str, data):
|
|
25
|
+
_CACHE[key] = {"data": data, "ts": _time.time()}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# --------------- Web Search ---------------
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _tavily_search(query: str, max_results: int = 5) -> list[dict]:
|
|
32
|
+
api_key = os.environ.get("TAVILY_API_KEY")
|
|
33
|
+
if not api_key:
|
|
34
|
+
return []
|
|
35
|
+
try:
|
|
36
|
+
import requests
|
|
37
|
+
|
|
38
|
+
resp = requests.post(
|
|
39
|
+
"https://api.tavily.com/search",
|
|
40
|
+
json={
|
|
41
|
+
"api_key": api_key,
|
|
42
|
+
"query": query,
|
|
43
|
+
"max_results": max_results,
|
|
44
|
+
"search_depth": "basic",
|
|
45
|
+
},
|
|
46
|
+
timeout=15,
|
|
47
|
+
)
|
|
48
|
+
data = resp.json()
|
|
49
|
+
return [
|
|
50
|
+
{
|
|
51
|
+
"title": r.get("title", ""),
|
|
52
|
+
"url": r.get("url", ""),
|
|
53
|
+
"snippet": r.get("content", "")[:200],
|
|
54
|
+
"source": "tavily",
|
|
55
|
+
}
|
|
56
|
+
for r in data.get("results", [])
|
|
57
|
+
]
|
|
58
|
+
except Exception:
|
|
59
|
+
return []
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _brave_search(query: str, count: int = 5) -> list[dict]:
|
|
63
|
+
api_key = os.environ.get("BRAVE_API_KEY")
|
|
64
|
+
if not api_key:
|
|
65
|
+
return []
|
|
66
|
+
try:
|
|
67
|
+
import requests
|
|
68
|
+
|
|
69
|
+
resp = requests.get(
|
|
70
|
+
"https://api.search.brave.com/res/v1/web/search",
|
|
71
|
+
headers={"X-Subscription-Token": api_key, "Accept": "application/json"},
|
|
72
|
+
params={"q": query, "count": count},
|
|
73
|
+
timeout=15,
|
|
74
|
+
)
|
|
75
|
+
data = resp.json()
|
|
76
|
+
return [
|
|
77
|
+
{
|
|
78
|
+
"title": r.get("title", ""),
|
|
79
|
+
"url": r.get("url", ""),
|
|
80
|
+
"snippet": r.get("description", "")[:200],
|
|
81
|
+
"source": "brave",
|
|
82
|
+
}
|
|
83
|
+
for r in data.get("web", {}).get("results", [])
|
|
84
|
+
]
|
|
85
|
+
except Exception:
|
|
86
|
+
return []
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _serpapi_search(query: str, num: int = 5) -> list[dict]:
|
|
90
|
+
api_key = os.environ.get("SERPAPI_KEY")
|
|
91
|
+
if not api_key:
|
|
92
|
+
return []
|
|
93
|
+
try:
|
|
94
|
+
import requests
|
|
95
|
+
|
|
96
|
+
resp = requests.get(
|
|
97
|
+
"https://serpapi.com/search",
|
|
98
|
+
params={
|
|
99
|
+
"api_key": api_key,
|
|
100
|
+
"q": query,
|
|
101
|
+
"num": num,
|
|
102
|
+
"engine": "google",
|
|
103
|
+
},
|
|
104
|
+
timeout=15,
|
|
105
|
+
)
|
|
106
|
+
data = resp.json()
|
|
107
|
+
return [
|
|
108
|
+
{
|
|
109
|
+
"title": r.get("title", ""),
|
|
110
|
+
"url": r.get("link", ""),
|
|
111
|
+
"snippet": r.get("snippet", "")[:200],
|
|
112
|
+
"source": "serpapi",
|
|
113
|
+
}
|
|
114
|
+
for r in data.get("organic_results", [])
|
|
115
|
+
]
|
|
116
|
+
except Exception:
|
|
117
|
+
return []
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _bocha_search(query: str, max_results: int = 5) -> list[dict]:
|
|
121
|
+
api_key = os.environ.get("BOCHA_API_KEY")
|
|
122
|
+
if not api_key:
|
|
123
|
+
return []
|
|
124
|
+
try:
|
|
125
|
+
import requests
|
|
126
|
+
|
|
127
|
+
resp = requests.post(
|
|
128
|
+
"https://api.bochaai.com/v1/web-search",
|
|
129
|
+
headers={"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"},
|
|
130
|
+
json={"query": query, "count": max_results},
|
|
131
|
+
timeout=15,
|
|
132
|
+
)
|
|
133
|
+
data = resp.json()
|
|
134
|
+
results = []
|
|
135
|
+
for item in data.get("web", {}).get("results", []):
|
|
136
|
+
results.append(
|
|
137
|
+
{
|
|
138
|
+
"title": item.get("name", ""),
|
|
139
|
+
"url": item.get("url", ""),
|
|
140
|
+
"snippet": item.get("snippet", "")[:200],
|
|
141
|
+
"source": "bocha",
|
|
142
|
+
}
|
|
143
|
+
)
|
|
144
|
+
return results
|
|
145
|
+
except Exception:
|
|
146
|
+
return []
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _searxng_search(query: str, max_results: int = 5) -> list[dict]:
|
|
150
|
+
"""SearXNG self-hosted metasearch — no API key, quota-free fallback.
|
|
151
|
+
|
|
152
|
+
Set SEARXNG_BASE_URLS to a comma-separated list of instance URLs,
|
|
153
|
+
e.g. "http://127.0.0.1:8080,https://searx.example.com".
|
|
154
|
+
"""
|
|
155
|
+
base_urls = [u.strip().rstrip("/") for u in os.environ.get("SEARXNG_BASE_URLS", "").split(",") if u.strip()]
|
|
156
|
+
if not base_urls:
|
|
157
|
+
return []
|
|
158
|
+
try:
|
|
159
|
+
import requests
|
|
160
|
+
except ImportError:
|
|
161
|
+
return []
|
|
162
|
+
for base in base_urls:
|
|
163
|
+
try:
|
|
164
|
+
resp = requests.get(
|
|
165
|
+
f"{base}/search",
|
|
166
|
+
params={"q": query, "format": "json"},
|
|
167
|
+
headers={"Accept": "application/json"},
|
|
168
|
+
timeout=15,
|
|
169
|
+
)
|
|
170
|
+
if not resp.ok:
|
|
171
|
+
continue
|
|
172
|
+
data = resp.json()
|
|
173
|
+
results = [
|
|
174
|
+
{
|
|
175
|
+
"title": r.get("title", ""),
|
|
176
|
+
"url": r.get("url", ""),
|
|
177
|
+
"snippet": (r.get("content") or "")[:200],
|
|
178
|
+
"source": "searxng",
|
|
179
|
+
}
|
|
180
|
+
for r in data.get("results", [])
|
|
181
|
+
]
|
|
182
|
+
if results:
|
|
183
|
+
return results
|
|
184
|
+
except Exception:
|
|
185
|
+
continue
|
|
186
|
+
return []
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def search_news(query: str, max_results: int = 10) -> dict:
|
|
190
|
+
results = []
|
|
191
|
+
engines_tried = []
|
|
192
|
+
engines_available = []
|
|
193
|
+
|
|
194
|
+
for name, fn in [
|
|
195
|
+
("tavily", _tavily_search),
|
|
196
|
+
("brave", _brave_search),
|
|
197
|
+
("bocha", _bocha_search),
|
|
198
|
+
("serpapi", _serpapi_search),
|
|
199
|
+
("searxng", _searxng_search),
|
|
200
|
+
]:
|
|
201
|
+
engines_tried.append(name)
|
|
202
|
+
items = fn(query, max_results)
|
|
203
|
+
if items:
|
|
204
|
+
engines_available.append(name)
|
|
205
|
+
results.extend(items)
|
|
206
|
+
if len(results) >= max_results:
|
|
207
|
+
break
|
|
208
|
+
|
|
209
|
+
if not results:
|
|
210
|
+
return {
|
|
211
|
+
"query": query,
|
|
212
|
+
"results": [],
|
|
213
|
+
"engines_tried": engines_tried,
|
|
214
|
+
"note": (
|
|
215
|
+
"No search engines configured. Set TAVILY_API_KEY, BRAVE_API_KEY, BOCHA_API_KEY, "
|
|
216
|
+
"SERPAPI_KEY, or SEARXNG_BASE_URLS in environment."
|
|
217
|
+
),
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
seen_urls = set()
|
|
221
|
+
deduped = []
|
|
222
|
+
for r in results:
|
|
223
|
+
if r["url"] not in seen_urls:
|
|
224
|
+
seen_urls.add(r["url"])
|
|
225
|
+
deduped.append(r)
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
"query": query,
|
|
229
|
+
"results": deduped[:max_results],
|
|
230
|
+
"engines_used": engines_available,
|
|
231
|
+
"total": len(deduped),
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def search_comprehensive(symbol: str, name: str = None) -> dict:
|
|
236
|
+
label = name or symbol
|
|
237
|
+
dimensions = {
|
|
238
|
+
"latest_news": f"{label} 最新消息 新闻",
|
|
239
|
+
"announcements": f"{label} 公告 公司公告",
|
|
240
|
+
"market_analysis": f"{label} 行情分析 走势分析",
|
|
241
|
+
"risk_check": f"{label} 风险 预警 违规",
|
|
242
|
+
"earnings": f"{label} 业绩 财报 营收",
|
|
243
|
+
"industry": f"{label} 行业 赛道 趋势",
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
results = {}
|
|
247
|
+
for dim, query in dimensions.items():
|
|
248
|
+
data = search_news(query, max_results=3)
|
|
249
|
+
results[dim] = data.get("results", [])
|
|
250
|
+
|
|
251
|
+
return {"symbol": symbol, "name": label, "dimensions": results}
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
# --------------- A-share Sentiment Sources ---------------
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def _eastmoney_guba_heat(symbol: str) -> dict:
|
|
258
|
+
"""东方财富股吧热度 (public API, no auth)."""
|
|
259
|
+
try:
|
|
260
|
+
import requests
|
|
261
|
+
|
|
262
|
+
resp = requests.get(
|
|
263
|
+
"https://guba.eastmoney.com/interface/GetData.aspx",
|
|
264
|
+
params={"path": "newtopic/api", "type": "gethot", "code": symbol},
|
|
265
|
+
headers={"Referer": "https://guba.eastmoney.com/"},
|
|
266
|
+
timeout=10,
|
|
267
|
+
)
|
|
268
|
+
if not resp.ok:
|
|
269
|
+
return {}
|
|
270
|
+
data = resp.json()
|
|
271
|
+
re_data = data.get("re") or data.get("data") or {}
|
|
272
|
+
if not re_data:
|
|
273
|
+
return {}
|
|
274
|
+
return {
|
|
275
|
+
"source": "eastmoney_guba",
|
|
276
|
+
"symbol": symbol,
|
|
277
|
+
"hot_rank": re_data.get("hot_rank"),
|
|
278
|
+
"post_count_today": re_data.get("post_count"),
|
|
279
|
+
"view_count_today": re_data.get("view_count"),
|
|
280
|
+
"sentiment_ratio": re_data.get("sentiment"),
|
|
281
|
+
}
|
|
282
|
+
except Exception:
|
|
283
|
+
return {}
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def _xueqiu_heat(symbol: str) -> dict:
|
|
287
|
+
"""雪球讨论热度 (public API)."""
|
|
288
|
+
try:
|
|
289
|
+
import requests
|
|
290
|
+
|
|
291
|
+
xq_symbol = f"SH{symbol}" if symbol.startswith(("6", "9", "5")) else f"SZ{symbol}"
|
|
292
|
+
|
|
293
|
+
resp = requests.get(
|
|
294
|
+
"https://stock.xueqiu.com/v5/stock/hot_stock/info.json",
|
|
295
|
+
params={"symbol": xq_symbol},
|
|
296
|
+
headers={"User-Agent": "Mozilla/5.0", "Cookie": "xq_a_token=placeholder"},
|
|
297
|
+
timeout=10,
|
|
298
|
+
)
|
|
299
|
+
if not resp.ok:
|
|
300
|
+
return {}
|
|
301
|
+
data = resp.json().get("data") or {}
|
|
302
|
+
if not data:
|
|
303
|
+
return {}
|
|
304
|
+
return {
|
|
305
|
+
"source": "xueqiu",
|
|
306
|
+
"symbol": symbol,
|
|
307
|
+
"followers": data.get("followers"),
|
|
308
|
+
"discussion_count": data.get("discussion_count"),
|
|
309
|
+
"hot_value": data.get("value"),
|
|
310
|
+
"rank": data.get("rank"),
|
|
311
|
+
}
|
|
312
|
+
except Exception:
|
|
313
|
+
return {}
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
# --------------- Social Sentiment (market-aware) ---------------
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def get_social_sentiment(symbol: str) -> dict:
|
|
320
|
+
"""Market-aware social sentiment. A-shares: eastmoney + xueqiu. US/HK: Reddit/X/Polymarket."""
|
|
321
|
+
result = {"symbol": symbol, "sources": {}}
|
|
322
|
+
|
|
323
|
+
if re.match(r"^\d{6}$", symbol):
|
|
324
|
+
market = "A"
|
|
325
|
+
elif symbol.upper().endswith(".HK"):
|
|
326
|
+
market = "HK"
|
|
327
|
+
else:
|
|
328
|
+
market = "US"
|
|
329
|
+
|
|
330
|
+
if market == "A":
|
|
331
|
+
guba = _eastmoney_guba_heat(symbol)
|
|
332
|
+
if guba:
|
|
333
|
+
result["sources"]["eastmoney_guba"] = guba
|
|
334
|
+
|
|
335
|
+
xueqiu = _xueqiu_heat(symbol)
|
|
336
|
+
if xueqiu:
|
|
337
|
+
result["sources"]["xueqiu"] = xueqiu
|
|
338
|
+
else:
|
|
339
|
+
api_url = os.environ.get("SENTIMENT_API_URL", "https://api.adanos.org")
|
|
340
|
+
api_key = os.environ.get("SENTIMENT_API_KEY")
|
|
341
|
+
|
|
342
|
+
try:
|
|
343
|
+
import requests
|
|
344
|
+
|
|
345
|
+
headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
|
|
346
|
+
|
|
347
|
+
for source in ["reddit", "twitter", "polymarket"]:
|
|
348
|
+
try:
|
|
349
|
+
resp = requests.get(f"{api_url}/sentiment/{source}/{symbol}", headers=headers, timeout=10)
|
|
350
|
+
if resp.ok:
|
|
351
|
+
result["sources"][source] = resp.json()
|
|
352
|
+
except Exception:
|
|
353
|
+
pass
|
|
354
|
+
except ImportError:
|
|
355
|
+
result["error"] = "requests library not available"
|
|
356
|
+
|
|
357
|
+
if not result["sources"]:
|
|
358
|
+
if market == "A":
|
|
359
|
+
result["note"] = "No A-share sentiment data available. 东方财富/雪球 APIs may be temporarily unavailable."
|
|
360
|
+
else:
|
|
361
|
+
result["note"] = (
|
|
362
|
+
"No sentiment data available. Set SENTIMENT_API_URL and SENTIMENT_API_KEY, "
|
|
363
|
+
"or ensure the sentiment API is accessible."
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
result["additional_context"] = {
|
|
367
|
+
"wisburg_mcp": "Use 'list-feed' and 'list-market-daily' MCP tools for institutional research context.",
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return result
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
# --------------- Trending Aggregation ---------------
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def get_trending_sentiment() -> dict:
|
|
377
|
+
"""Fetch trending sentiment from Reddit/X/Polymarket. Results cached for 10 min."""
|
|
378
|
+
cache_key = "trending_sentiment"
|
|
379
|
+
cached = _cache_get(cache_key)
|
|
380
|
+
if cached:
|
|
381
|
+
return cached
|
|
382
|
+
|
|
383
|
+
api_url = os.environ.get("SENTIMENT_API_URL", "https://api.adanos.org")
|
|
384
|
+
api_key = os.environ.get("SENTIMENT_API_KEY")
|
|
385
|
+
|
|
386
|
+
result = {"trending": {}, "fetched_at": None}
|
|
387
|
+
|
|
388
|
+
try:
|
|
389
|
+
import requests
|
|
390
|
+
|
|
391
|
+
headers = {"Authorization": f"Bearer {api_key}"} if api_key else {}
|
|
392
|
+
|
|
393
|
+
for source in ["reddit", "twitter", "polymarket"]:
|
|
394
|
+
try:
|
|
395
|
+
resp = requests.get(f"{api_url}/trending/{source}", headers=headers, timeout=10)
|
|
396
|
+
if resp.ok:
|
|
397
|
+
result["trending"][source] = resp.json()
|
|
398
|
+
except Exception:
|
|
399
|
+
pass
|
|
400
|
+
except ImportError:
|
|
401
|
+
result["error"] = "requests library not available"
|
|
402
|
+
|
|
403
|
+
if not result["trending"]:
|
|
404
|
+
result["note"] = "No trending data available. Set SENTIMENT_API_URL and SENTIMENT_API_KEY."
|
|
405
|
+
|
|
406
|
+
result["fetched_at"] = _time.strftime("%Y-%m-%d %H:%M:%S")
|
|
407
|
+
result["additional_context"] = {
|
|
408
|
+
"wisburg_mcp": "Use 'list-feed' for latest research feed and 'list-market-daily' for market daily digest.",
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
_cache_set(cache_key, result)
|
|
412
|
+
return result
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
# --------------- Article Extraction ---------------
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def extract_article(url: str) -> dict:
|
|
419
|
+
try:
|
|
420
|
+
from newspaper import Article
|
|
421
|
+
|
|
422
|
+
a = Article(url, language="zh")
|
|
423
|
+
a.download()
|
|
424
|
+
a.parse()
|
|
425
|
+
return {
|
|
426
|
+
"url": url,
|
|
427
|
+
"title": a.title or "",
|
|
428
|
+
"text": (a.text or "")[:3000],
|
|
429
|
+
"authors": a.authors or [],
|
|
430
|
+
"publish_date": str(a.publish_date) if a.publish_date else None,
|
|
431
|
+
"top_image": a.top_image or None,
|
|
432
|
+
}
|
|
433
|
+
except ImportError:
|
|
434
|
+
return {"url": url, "error": "newspaper4k not installed. pip install newspaper4k"}
|
|
435
|
+
except Exception as e:
|
|
436
|
+
return {"url": url, "error": str(e)}
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
# --------------- CLI Commands ---------------
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def cmd_search(args):
|
|
443
|
+
return search_news(args.query, args.count)
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
def cmd_comprehensive(args):
|
|
447
|
+
return search_comprehensive(args.symbol, args.name)
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def cmd_sentiment(args):
|
|
451
|
+
return get_social_sentiment(args.symbol)
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def cmd_trending(args):
|
|
455
|
+
return get_trending_sentiment()
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def cmd_extract(args):
|
|
459
|
+
return extract_article(args.url)
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def main():
|
|
463
|
+
parser = argparse.ArgumentParser(description="Search intelligence & social sentiment")
|
|
464
|
+
sub = parser.add_subparsers(dest="command")
|
|
465
|
+
|
|
466
|
+
p_search = sub.add_parser("search")
|
|
467
|
+
p_search.add_argument("query", help="Search query")
|
|
468
|
+
p_search.add_argument("--count", type=int, default=10)
|
|
469
|
+
|
|
470
|
+
p_comp = sub.add_parser("comprehensive")
|
|
471
|
+
p_comp.add_argument("symbol", help="Stock symbol")
|
|
472
|
+
p_comp.add_argument("--name", default=None, help="Stock name for better search")
|
|
473
|
+
|
|
474
|
+
p_sent = sub.add_parser("sentiment")
|
|
475
|
+
p_sent.add_argument("symbol", help="Stock ticker (e.g. AAPL, 600519, 00700.HK)")
|
|
476
|
+
|
|
477
|
+
sub.add_parser("trending")
|
|
478
|
+
|
|
479
|
+
p_ext = sub.add_parser("extract")
|
|
480
|
+
p_ext.add_argument("url", help="Article URL to extract")
|
|
481
|
+
|
|
482
|
+
args = parser.parse_args()
|
|
483
|
+
if not args.command:
|
|
484
|
+
parser.print_help()
|
|
485
|
+
sys.exit(1)
|
|
486
|
+
|
|
487
|
+
dispatch = {
|
|
488
|
+
"search": cmd_search,
|
|
489
|
+
"comprehensive": cmd_comprehensive,
|
|
490
|
+
"sentiment": cmd_sentiment,
|
|
491
|
+
"trending": cmd_trending,
|
|
492
|
+
"extract": cmd_extract,
|
|
493
|
+
}
|
|
494
|
+
result = dispatch[args.command](args)
|
|
495
|
+
print(json.dumps(result, ensure_ascii=False, default=str))
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
if __name__ == "__main__":
|
|
499
|
+
# Windows defaults stdio to a legacy code page (cp1252) that cannot encode the
|
|
500
|
+
# Chinese text these tools emit — force UTF-8 so stdout never crashes there.
|
|
501
|
+
for _s in (sys.stdout, sys.stderr):
|
|
502
|
+
if hasattr(_s, "reconfigure"):
|
|
503
|
+
_s.reconfigure(encoding="utf-8")
|
|
504
|
+
main()
|