argo-search 2.9.8 → 2.10.0
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.en.md +48 -35
- package/README.es.md +48 -35
- package/README.ja.md +47 -34
- package/README.ko.md +47 -34
- package/README.md +50 -35
- package/SKILL.md +5 -5
- package/backends/domain_profiles.json +76 -0
- package/backends/engine_registry.yaml +161 -5
- package/backends/quota_profiles.json +122 -2
- package/bin/argo +26 -1
- package/bin/argo.js +3 -3
- package/config.yaml +180 -26
- package/docs/ARGO_INTRO.md +7 -5
- package/docs/ENGINE_CATALOG.md +64 -44
- package/docs/MCP_SETUP.md +1 -1
- package/docs/RELEASE_NOTES_v2.10.0.md +85 -0
- package/docs/RELEASE_NOTES_v2.9.8.md +1 -1
- package/docs/RELEASE_NOTES_v2.9.9.md +78 -0
- package/docs//344/270/272/344/273/200/344/271/210/351/200/211/346/213/251argo.md +9 -9
- package/engines/specs/clawhub.yaml +16 -15
- package/engines/specs/deepseek1024.yaml +46 -0
- package/engines/specs/dsh_hub.yaml +44 -0
- package/engines/specs/dshopen.yaml +32 -0
- package/engines/specs/explainx.yaml +32 -0
- package/engines/specs/gbfs_nyc.yaml +6 -1
- package/engines/specs/metaso_doc.yaml +49 -0
- package/engines/specs/metaso_image.yaml +65 -0
- package/engines/specs/metaso_podcast.yaml +60 -0
- package/engines/specs/metaso_scholar.yaml +57 -0
- package/engines/specs/metaso_video.yaml +62 -0
- package/engines/specs/ncpssd.yaml +41 -0
- package/engines/specs/sciverse.yaml +72 -0
- package/engines/specs/skills_sh.yaml +32 -0
- package/engines/specs/skillsmp.yaml +18 -16
- package/package.json +4 -2
- package/packages/dsh-plugin/README.md +4 -2
- package/packages/dsh-plugin/dsh/client.js +98 -32
- package/packages/dsh-plugin/dsh/diagnostics.js +43 -0
- package/packages/dsh-plugin/dsh/index.js +23 -1
- package/packages/dsh-plugin/package.json +2 -2
- package/packages/dsh-plugin/test/client.test.js +2 -1
- package/packages/dsh-plugin/test/size-gate.test.js +113 -0
- package/scripts/adaptive.py +135 -48
- package/scripts/article.py +6 -1
- package/scripts/article_meta.py +151 -2
- package/scripts/bench_general_pool.py +145 -0
- package/scripts/bench_tail_ab.py +281 -0
- package/scripts/bounded_run.py +91 -49
- package/scripts/candidate_envelope.py +45 -4
- package/scripts/charset_decode.py +127 -0
- package/scripts/clock_bridge.py +22 -0
- package/scripts/content_security.py +119 -4
- package/scripts/crawl.py +240 -74
- package/scripts/crawl_frontier.py +217 -0
- package/scripts/crawl_policy.py +174 -0
- package/scripts/dispatch_result.py +9 -2
- package/scripts/engine_add.py +602 -0
- package/scripts/engine_dispatch.py +31 -15
- package/scripts/engine_failure.py +15 -0
- package/scripts/engine_validate.py +8 -0
- package/scripts/engines.py +10 -0
- package/scripts/engines_base.py +12 -0
- package/scripts/engines_builders.py +14 -0
- package/scripts/engines_builders_dsh.py +330 -0
- package/scripts/engines_builders_skill.py +314 -0
- package/scripts/evidence_loop.py +4 -0
- package/scripts/fetch_quality.py +13 -0
- package/scripts/fetch_v3.py +39 -7
- package/scripts/gen_engine_catalog.py +218 -2
- package/scripts/general_pool.py +162 -0
- package/scripts/http_client.py +62 -80
- package/scripts/image_ops.py +4 -0
- package/scripts/image_vision/vision_probe +0 -0
- package/scripts/install.ps1 +47 -19
- package/scripts/install.sh +45 -20
- package/scripts/matrix_search_eval.py +128 -2
- package/scripts/mcp_transport.py +1 -1
- package/scripts/ncpssd_bridge.py +123 -0
- package/scripts/net_proxy.py +27 -0
- package/scripts/paid_policy.py +109 -13
- package/scripts/pre-commit +19 -4
- package/scripts/query_signals.py +156 -3
- package/scripts/recovery.py +7 -3
- package/scripts/redskill/redskill_engine.py +29 -2
- package/scripts/release_check.py +209 -0
- package/scripts/retry_policy.py +22 -1
- package/scripts/robots_guard.py +26 -0
- package/scripts/route.py +11 -8
- package/scripts/route_cache.py +6 -0
- package/scripts/route_combo.py +56 -64
- package/scripts/route_domains.py +142 -13
- package/scripts/route_policy.py +6 -0
- package/scripts/safe_conn.py +52 -0
- package/scripts/search.py +9 -1
- package/scripts/search_cli.py +27 -0
- package/scripts/search_entry.py +10 -0
- package/scripts/search_output.py +29 -0
- package/scripts/search_pipeline.py +59 -3
- package/scripts/search_rank.py +7 -0
- package/scripts/serp_guard.py +23 -0
- package/scripts/social_engines/bilibili_engine.py +163 -26
- package/scripts/social_engines/reddit_engine.py +52 -51
- package/scripts/source_selection.py +302 -0
- package/scripts/tail_completion.py +212 -0
- package/scripts/url_safety.py +126 -3
- package/scripts/usage_log.py +45 -0
- package/scripts/video_fetch.py +303 -0
- package/scripts/video_meta.py +225 -0
- package/scripts/video_route.py +65 -0
- package/scripts/watch.py +123 -0
|
@@ -2,21 +2,38 @@
|
|
|
2
2
|
"""B站搜索引擎
|
|
3
3
|
|
|
4
4
|
使用 B站公开搜索 API(无需登录可搜索视频)。
|
|
5
|
+
|
|
6
|
+
2026-10-27 起补三件事(此前搜到视频但读不出「这是视频、多长、谁发的」):
|
|
7
|
+
- 时长筛选:把 --min-duration/--max-duration 映射成 B 站 API 的 duration
|
|
8
|
+
档位(1=<10min、2=10-30、3=30-60、4=>60min),档位表达不了的范围
|
|
9
|
+
再按解析出的秒数后过滤;
|
|
10
|
+
- 元数据完整化:发布时间转 ISO、收藏/投币/分享一并带出(envelope 只映射
|
|
11
|
+
它认识的键,但引擎层不该先丢);
|
|
12
|
+
- snippet 前置「UP主 · 时长 · 播放量」:否则五维排序和下游模型只看到标题,
|
|
13
|
+
视频结果无法按「长视频/高播放」判断。
|
|
5
14
|
"""
|
|
6
15
|
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import html
|
|
7
19
|
import json
|
|
20
|
+
import os
|
|
21
|
+
import re
|
|
22
|
+
import sys
|
|
8
23
|
import time
|
|
9
24
|
import urllib.error
|
|
10
25
|
import urllib.parse
|
|
11
26
|
import urllib.request
|
|
27
|
+
from datetime import datetime, timedelta, timezone
|
|
12
28
|
|
|
13
29
|
# 出口调度唯一入口(issue #13 同类修复):urlopen 原生只认标准 HTTP(S)_PROXY
|
|
14
30
|
# 环境变量,**不认** config.yaml 的 network.proxy —— 裸用会在「必须经代理才能
|
|
15
31
|
# 出网」的环境里整源连不上。本文件在 scripts/ 子目录,父目录不在 sys.path 上。
|
|
16
|
-
import os
|
|
17
|
-
import sys
|
|
18
32
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
19
33
|
from net_proxy import open_url # noqa: E402
|
|
34
|
+
from video_meta import parse_duration_seconds # noqa: E402
|
|
35
|
+
|
|
36
|
+
_TZ_CN = timezone(timedelta(hours=8))
|
|
20
37
|
|
|
21
38
|
|
|
22
39
|
def _http_get_with_retry(url: str, headers: dict, timeout: int = 10, max_retries: int = 2):
|
|
@@ -44,55 +61,169 @@ def _http_get_with_retry(url: str, headers: dict, timeout: int = 10, max_retries
|
|
|
44
61
|
return b"", 0
|
|
45
62
|
|
|
46
63
|
|
|
47
|
-
def
|
|
48
|
-
"""
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
64
|
+
def _duration_bucket(min_seconds, max_seconds):
|
|
65
|
+
"""把秒级范围映射成 B 站搜索的 duration 档位;跨档返回 None(只后过滤)。
|
|
66
|
+
|
|
67
|
+
档位语义来自 B 站 search/all/v2:1=<10min、2=10-30min、3=30-60min、
|
|
68
|
+
4=>60min。档位只做**粗召回**,精确范围仍由 _parse_bilibili_response
|
|
69
|
+
里的后过滤保证;两者都做,才不会为了精确而丢掉「档位内但不在区间」的
|
|
70
|
+
大量候选。
|
|
71
|
+
"""
|
|
72
|
+
if min_seconds is None and max_seconds is None:
|
|
73
|
+
return None
|
|
74
|
+
if min_seconds is None:
|
|
75
|
+
# 只有上限时唯一安全的档位是「≤10 分钟」(档位 1 正好覆盖全集)。
|
|
76
|
+
# 上限 30/60 分钟不能用档位 2/3:那会把更短的视频一起排除,而用户
|
|
77
|
+
# 只要求「不超过」,没有要求「至少 10/30 分钟」。
|
|
78
|
+
return 1 if max_seconds <= 600 else None
|
|
79
|
+
if max_seconds is None:
|
|
80
|
+
# 只有下限时同理:档位 4(>60 分钟)是下限 ≥60 分钟的安全超集;
|
|
81
|
+
# 下限 10/30 分钟不能用档位 2/3,否则会丢掉更长(>30/>60)的视频。
|
|
82
|
+
return 4 if min_seconds >= 3600 else None
|
|
83
|
+
if min_seconds >= 3600:
|
|
84
|
+
return 4
|
|
85
|
+
if 600 <= min_seconds and max_seconds <= 1800:
|
|
86
|
+
return 2
|
|
87
|
+
if 1800 <= min_seconds and max_seconds <= 3600:
|
|
88
|
+
return 3
|
|
89
|
+
if max_seconds <= 600:
|
|
90
|
+
return 1
|
|
91
|
+
return None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _format_play(count) -> str:
|
|
95
|
+
try:
|
|
96
|
+
n = int(count)
|
|
97
|
+
except (TypeError, ValueError):
|
|
98
|
+
return ""
|
|
99
|
+
if n >= 10000:
|
|
100
|
+
text = f"{n / 10000:.1f}万"
|
|
101
|
+
return text.replace(".0万", "万")
|
|
102
|
+
return str(n)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _published_iso(ts) -> str:
|
|
106
|
+
try:
|
|
107
|
+
return datetime.fromtimestamp(int(ts), tz=_TZ_CN).isoformat()
|
|
108
|
+
except (TypeError, ValueError, OSError):
|
|
109
|
+
return ""
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _clean_title(raw: str) -> str:
|
|
113
|
+
"""B 站搜索标题带 <em class="keyword"> 高亮和 HTML 实体。"""
|
|
114
|
+
text = re.sub(r"<[^>]+>", "", raw or "")
|
|
115
|
+
return html.unescape(text).strip()
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _snippet_of(item: dict, title: str) -> str:
|
|
119
|
+
"""把视频元数据前置到摘要:排序器与模型都需要「多长、谁发的、多少人看」。"""
|
|
120
|
+
meta = []
|
|
121
|
+
author = (item.get("author") or "").strip()
|
|
122
|
+
duration = (item.get("duration") or "").strip()
|
|
123
|
+
play = _format_play(item.get("play"))
|
|
124
|
+
if author:
|
|
125
|
+
meta.append(f"UP主 {author}")
|
|
126
|
+
if duration:
|
|
127
|
+
meta.append(f"时长 {duration}")
|
|
128
|
+
if play:
|
|
129
|
+
meta.append(f"播放 {play}")
|
|
130
|
+
desc = (item.get("description") or "").strip()
|
|
131
|
+
parts = meta + ([desc] if desc else [])
|
|
132
|
+
return " · ".join(parts)[:300] or title[:300]
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def search(query: str, n: int = 5, min_duration=None, max_duration=None) -> list[dict]:
|
|
136
|
+
"""通过 B站搜索 API 搜索。
|
|
137
|
+
|
|
138
|
+
min_duration/max_duration 单位是秒;None 表示不限制。带时长条件时多取
|
|
139
|
+
一些候选(档位粗召回后还要后过滤),避免筛完不足 n 条。
|
|
140
|
+
|
|
141
|
+
B 站 search API 单页上限实测 20 条(pagesize=50 仍只回 20),所以
|
|
142
|
+
「档位表达不了、只能后过滤」时按页取 3 页再按 bvid 去重,而不是把
|
|
143
|
+
pagesize 调大(调大无效)。
|
|
144
|
+
"""
|
|
145
|
+
bucket = _duration_bucket(min_duration, max_duration)
|
|
146
|
+
filtered = min_duration is not None or max_duration is not None
|
|
147
|
+
pages = 3 if filtered and bucket is None else 1
|
|
148
|
+
# 翻页场景取满单页(20 条),否则 3 页只有 3n 条、长视频更容易筛空。
|
|
149
|
+
page_size = 20 if pages > 1 else min(20, max(n, 1))
|
|
54
150
|
headers = {
|
|
55
151
|
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
|
|
56
152
|
"Referer": "https://www.bilibili.com"
|
|
57
153
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
154
|
+
results: list[dict] = []
|
|
155
|
+
seen: set[str] = set()
|
|
156
|
+
last_error: Exception | None = None
|
|
157
|
+
for page in range(1, pages + 1):
|
|
158
|
+
params = {"keyword": query, "page": page, "pagesize": page_size}
|
|
159
|
+
if bucket is not None:
|
|
160
|
+
params["duration"] = bucket
|
|
161
|
+
url = ("https://api.bilibili.com/x/web-interface/search/all/v2?"
|
|
162
|
+
+ urllib.parse.urlencode(params))
|
|
163
|
+
try:
|
|
164
|
+
body, _ = _http_get_with_retry(url, headers, timeout=10, max_retries=2)
|
|
165
|
+
data = json.loads(body.decode("utf-8"))
|
|
166
|
+
except Exception as e:
|
|
167
|
+
last_error = e
|
|
168
|
+
break
|
|
169
|
+
for item in _parse_bilibili_response(
|
|
170
|
+
data, n, min_duration=min_duration, max_duration=max_duration):
|
|
171
|
+
url_key = item.get("url") or ""
|
|
172
|
+
if url_key in seen:
|
|
173
|
+
continue
|
|
174
|
+
seen.add(url_key)
|
|
175
|
+
results.append(item)
|
|
176
|
+
if len(results) >= n:
|
|
177
|
+
break
|
|
178
|
+
if results:
|
|
179
|
+
return results[:n]
|
|
180
|
+
if last_error is not None:
|
|
63
181
|
# 吞异常返空会让 MCP 社交搜索把网络故障静默报成「平台成功、0 结果」
|
|
64
182
|
# (mcp_handlers 的 err=None 设计依赖引擎抛异常或返 error 占位,
|
|
65
183
|
# 见 tests/test_regression_p0p1.py「零结果应以 errors 提示」契约)。
|
|
66
184
|
# 对齐 zhihu_engine 范式:失败必须带着 error 字段浮上来。
|
|
67
|
-
return [{"error": f"bilibili {type(
|
|
185
|
+
return [{"error": f"bilibili {type(last_error).__name__}: {last_error}",
|
|
186
|
+
"source": "bilibili"}]
|
|
187
|
+
return []
|
|
68
188
|
|
|
69
189
|
|
|
70
|
-
def _parse_bilibili_response(data: dict, n: int
|
|
71
|
-
|
|
190
|
+
def _parse_bilibili_response(data: dict, n: int,
|
|
191
|
+
min_duration=None, max_duration=None) -> list[dict]:
|
|
192
|
+
"""解析 B站 API 响应;时长条件在这里做精确后过滤。"""
|
|
72
193
|
results = []
|
|
73
194
|
data_data = data.get("data", {})
|
|
74
195
|
for result_type in data_data.get("result", []):
|
|
75
196
|
if result_type.get("result_type") != "video":
|
|
76
197
|
continue
|
|
77
|
-
for item in result_type.get("data", [])
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
198
|
+
for item in result_type.get("data", []):
|
|
199
|
+
duration_text = item.get("duration", "")
|
|
200
|
+
seconds = parse_duration_seconds(duration_text)
|
|
201
|
+
if seconds is not None:
|
|
202
|
+
if min_duration is not None and seconds < min_duration:
|
|
203
|
+
continue
|
|
204
|
+
if max_duration is not None and seconds > max_duration:
|
|
205
|
+
continue
|
|
206
|
+
title = _clean_title(item.get("title", ""))
|
|
207
|
+
play = item.get("play", 0)
|
|
82
208
|
results.append({
|
|
83
209
|
"title": title[:100] + ("..." if len(title) > 100 else ""),
|
|
84
210
|
"url": f"https://www.bilibili.com/video/{item.get('bvid', '')}",
|
|
85
|
-
"snippet": (item
|
|
211
|
+
"snippet": _snippet_of(item, title),
|
|
86
212
|
"source": "bilibili",
|
|
87
213
|
"score": max(1.0 - len(results) * 0.1, 0.1),
|
|
88
214
|
"social_meta": {
|
|
89
215
|
"platform": "bilibili",
|
|
90
216
|
"content_type": "video",
|
|
91
217
|
"author": item.get("author", ""),
|
|
92
|
-
"duration":
|
|
93
|
-
"
|
|
218
|
+
"duration": duration_text,
|
|
219
|
+
"duration_seconds": seconds,
|
|
220
|
+
"play_count": play,
|
|
94
221
|
"danmu_count": item.get("video_review", 0),
|
|
95
222
|
"like_count": item.get("like", 0),
|
|
223
|
+
"favorite_count": item.get("favorites", 0),
|
|
224
|
+
"coin_count": item.get("coin", 0),
|
|
225
|
+
"share_count": item.get("share", 0),
|
|
226
|
+
"published_at": _published_iso(item.get("pubdate")),
|
|
96
227
|
"bvid": item.get("bvid", ""),
|
|
97
228
|
}
|
|
98
229
|
})
|
|
@@ -109,6 +240,10 @@ def main():
|
|
|
109
240
|
parser.add_argument("action", nargs="?", default="search")
|
|
110
241
|
parser.add_argument("query", nargs="?")
|
|
111
242
|
parser.add_argument("-n", type=int, default=5)
|
|
243
|
+
parser.add_argument("--min-duration", type=int, default=None,
|
|
244
|
+
help="最小时长(秒)")
|
|
245
|
+
parser.add_argument("--max-duration", type=int, default=None,
|
|
246
|
+
help="最大时长(秒)")
|
|
112
247
|
parser.add_argument("--json", action="store_true")
|
|
113
248
|
args = parser.parse_args()
|
|
114
249
|
|
|
@@ -116,7 +251,9 @@ def main():
|
|
|
116
251
|
print("[]")
|
|
117
252
|
return
|
|
118
253
|
|
|
119
|
-
results = search(args.query, args.n
|
|
254
|
+
results = search(args.query, args.n,
|
|
255
|
+
min_duration=args.min_duration,
|
|
256
|
+
max_duration=args.max_duration)
|
|
120
257
|
if args.json:
|
|
121
258
|
print(json.dumps(results, ensure_ascii=False))
|
|
122
259
|
else:
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
2
|
"""Reddit 搜索引擎
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
通过 Reddit RSS(Atom)搜索公开内容,无需认证。也可通过 rdt CLI 扩展。
|
|
5
|
+
|
|
6
|
+
端点史:无 key 的 search.json 自 2026-10 起对直连请求返回 Cloudflare HTML
|
|
7
|
+
挑战页(bot 与浏览器 UA 无差别),RSS 是仅存的 keyless 通道;RSS 不带
|
|
8
|
+
互动数(upvotes/comments),social_meta 相应字段恒 0。
|
|
6
9
|
"""
|
|
7
10
|
|
|
11
|
+
import html as _html
|
|
8
12
|
import json
|
|
13
|
+
import re
|
|
9
14
|
import subprocess
|
|
10
|
-
import time
|
|
11
|
-
import urllib.error
|
|
12
15
|
import urllib.parse
|
|
13
16
|
import urllib.request
|
|
17
|
+
import xml.etree.ElementTree as ET
|
|
14
18
|
|
|
15
19
|
# 出口调度唯一入口(issue #13 同类修复):urlopen 原生只认标准 HTTP(S)_PROXY
|
|
16
20
|
# 环境变量,**不认** config.yaml 的 network.proxy —— 裸用会在「必须经代理才能
|
|
@@ -21,40 +25,31 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
21
25
|
from net_proxy import open_url # noqa: E402
|
|
22
26
|
|
|
23
27
|
|
|
24
|
-
def
|
|
25
|
-
"""
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
raise
|
|
41
|
-
except (urllib.error.URLError, OSError):
|
|
42
|
-
if attempt < max_retries:
|
|
43
|
-
time.sleep(2 ** attempt + 0.5)
|
|
44
|
-
continue
|
|
45
|
-
raise
|
|
46
|
-
return b"", 0
|
|
28
|
+
def _http_get(url: str, headers: dict, timeout: int = 10) -> bytes:
|
|
29
|
+
"""单次 HTTP GET(stdlib)。429 不重试:无 key 端点按 IP「一次一发」,
|
|
30
|
+
2026-10 实测首次 200 后即连续 429、Retry-After 达分钟级,短间隔重试
|
|
31
|
+
必然再 429,还会叠乘 config reddit 段的引擎级 8s timeout。失败直接
|
|
32
|
+
上浮,由 search_reddit_api 转成 error 条目。"""
|
|
33
|
+
req = urllib.request.Request(url, headers=headers)
|
|
34
|
+
with open_url(req, timeout=timeout) as resp:
|
|
35
|
+
return resp.read()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
_ATOM_NS = {"atom": "http://www.w3.org/2005/Atom"}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _strip_html(raw: str) -> str:
|
|
42
|
+
text = re.sub(r"<[^>]+>", " ", raw or "")
|
|
43
|
+
return _html.unescape(re.sub(r"\s+", " ", text)).strip()
|
|
47
44
|
|
|
48
45
|
|
|
49
46
|
def search_reddit_api(query: str, n: int = 5) -> list[dict]:
|
|
50
|
-
"""通过 Reddit
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
headers = {"User-Agent": "argo-search/1.0 (by taxueseek)"}
|
|
47
|
+
"""通过 Reddit RSS(Atom)搜索"""
|
|
48
|
+
url = (f"https://www.reddit.com/search.rss?q={urllib.parse.quote(query)}"
|
|
49
|
+
f"&sort=relevance&limit={n}")
|
|
54
50
|
try:
|
|
55
|
-
body
|
|
56
|
-
|
|
57
|
-
return _parse_reddit_response(data, n)
|
|
51
|
+
body = _http_get(url, {"User-Agent": "argo-search/1.0 (by taxueseek)"}, timeout=10)
|
|
52
|
+
return _parse_reddit_rss(body, n)
|
|
58
53
|
except Exception as e:
|
|
59
54
|
# 吞异常返空会让 MCP 社交搜索把网络故障静默报成「平台成功、0 结果」
|
|
60
55
|
# (mcp_handlers 的 err=None 设计依赖引擎抛异常或返 error 占位,
|
|
@@ -63,29 +58,35 @@ def search_reddit_api(query: str, n: int = 5) -> list[dict]:
|
|
|
63
58
|
return [{"error": f"reddit {type(e).__name__}: {e}", "source": "reddit"}]
|
|
64
59
|
|
|
65
60
|
|
|
66
|
-
def
|
|
67
|
-
"""解析
|
|
61
|
+
def _parse_reddit_rss(body: bytes, n: int) -> list[dict]:
|
|
62
|
+
"""解析 search.rss 的 Atom 响应。link 命中 /comments/ 的是帖子,
|
|
63
|
+
其余(如 /r/<sub>/ 子版块页)按 listing 收录。"""
|
|
64
|
+
root = ET.fromstring(body.decode("utf-8", errors="replace"))
|
|
68
65
|
results = []
|
|
69
|
-
for
|
|
70
|
-
|
|
71
|
-
if not
|
|
72
|
-
|
|
73
|
-
title =
|
|
74
|
-
|
|
66
|
+
for entry in root.findall("atom:entry", _ATOM_NS)[:n]:
|
|
67
|
+
link_el = entry.find("atom:link", _ATOM_NS)
|
|
68
|
+
url = link_el.get("href", "") if link_el is not None else ""
|
|
69
|
+
m = re.match(r"https://(?:www\.)?reddit\.com/r/([^/]+)/", url or "")
|
|
70
|
+
title = (entry.findtext("atom:title", "", _ATOM_NS) or "").strip()
|
|
71
|
+
author = entry.findtext("atom:author/atom:name", "", _ATOM_NS) or ""
|
|
72
|
+
if author.startswith("/u/"):
|
|
73
|
+
author = author[3:]
|
|
74
|
+
snippet = _strip_html(entry.findtext("atom:content", "", _ATOM_NS))
|
|
75
75
|
results.append({
|
|
76
76
|
"title": title[:100] + ("..." if len(title) > 100 else ""),
|
|
77
|
-
"url":
|
|
78
|
-
"snippet":
|
|
77
|
+
"url": url,
|
|
78
|
+
"snippet": snippet[:300] or title,
|
|
79
79
|
"source": "reddit",
|
|
80
80
|
"score": max(1.0 - len(results) * 0.1, 0.1),
|
|
81
81
|
"social_meta": {
|
|
82
82
|
"platform": "reddit",
|
|
83
|
-
"content_type": "post",
|
|
84
|
-
"subreddit":
|
|
85
|
-
"author":
|
|
86
|
-
|
|
87
|
-
"
|
|
88
|
-
"
|
|
83
|
+
"content_type": "post" if "/comments/" in url else "listing",
|
|
84
|
+
"subreddit": m.group(1) if m else "",
|
|
85
|
+
"author": author,
|
|
86
|
+
# RSS 不带互动数,字段保留供 aggregate_social_sentiment 消费
|
|
87
|
+
"upvotes": 0,
|
|
88
|
+
"comments": 0,
|
|
89
|
+
"awards": 0,
|
|
89
90
|
}
|
|
90
91
|
})
|
|
91
92
|
return results
|
|
@@ -105,7 +106,7 @@ def search(query: str, n: int = 5) -> list[dict]:
|
|
|
105
106
|
except (FileNotFoundError, subprocess.TimeoutExpired):
|
|
106
107
|
pass
|
|
107
108
|
|
|
108
|
-
# fallback: Reddit
|
|
109
|
+
# fallback: Reddit RSS(search.json 已被 Cloudflare 挑战页取代)
|
|
109
110
|
return search_reddit_api(query, n)
|
|
110
111
|
|
|
111
112
|
|