argo-search 2.16.0 → 2.17.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/AGENTS.md +3 -1
- package/README.es.md +9 -4
- package/README.ja.md +9 -4
- package/README.ko.md +9 -4
- package/README.md +9 -4
- package/README.zh.md +9 -4
- package/SKILL.md +1 -1
- package/assets/readme/counts.json +1 -1
- package/assets/readme/hero.svg +3 -3
- package/assets/readme/hero.template.svg +121 -0
- package/assets/readme/proof-routes.svg +17 -16
- package/backends/domain_profiles.json +115 -18
- package/backends/engine_registry.yaml +11 -0
- package/backends/env_passthrough.txt +2 -0
- package/backends/quota_profiles.json +10 -0
- package/docs/CHANGELOG.md +17 -1
- package/docs/COMMIT_STANDARD.md +10 -0
- package/docs/ENGINE_CATALOG.md +32 -30
- package/docs/RELEASE_NOTES_v2.17.0.md +64 -0
- package/docs/RELEASING.md +1 -1
- package/engines/specs/crossref.yaml +2 -0
- package/engines/specs/hackernews.yaml +2 -0
- package/engines/specs/helve.yaml +49 -0
- package/engines/specs/hf_papers.yaml +2 -0
- package/engines/specs/openalex.yaml +9 -2
- package/package.json +2 -1
- package/packages/dsh-plugin/README.md +5 -2
- package/packages/dsh-plugin/cordis.patch.yml +4 -1
- package/packages/dsh-plugin/dsh/client.js +57 -63
- package/packages/dsh-plugin/dsh/diagnostics.js +25 -4
- package/packages/dsh-plugin/dsh/index.js +109 -21
- package/packages/dsh-plugin/dsh/route-guard.js +95 -0
- package/packages/dsh-plugin/dsh/runner.js +49 -6
- package/packages/dsh-plugin/dsh/wide-research.js +20 -18
- package/packages/dsh-plugin/dsh/workbench-client.js +119 -20
- package/packages/dsh-plugin/dsh/workbench-io.js +163 -0
- package/packages/dsh-plugin/dsh/workbench-search.js +385 -0
- package/packages/dsh-plugin/dsh/workbench-suggestions.js +387 -75
- package/packages/dsh-plugin/dsh/workbench.js +234 -497
- package/packages/dsh-plugin/package.json +4 -3
- package/packages/dsh-plugin/scripts/sync-local.mjs +4 -2
- package/packages/dsh-plugin/scripts/sync-local.sh +4 -2
- package/packages/dsh-plugin/test/client.test.js +26 -0
- package/packages/dsh-plugin/test/diagnostics.test.js +5 -2
- package/packages/dsh-plugin/test/index.test.js +150 -1
- package/packages/dsh-plugin/test/workbench-concurrency.test.js +98 -0
- package/packages/dsh-plugin/test/workbench.test.js +337 -9
- package/scripts/adaptive.py +13 -11
- package/scripts/browser_auth.py +178 -4
- package/scripts/cache.py +68 -69
- package/scripts/check_commit_report.py +46 -0
- package/scripts/chrome_cdp.py +71 -0
- package/scripts/circuit_breaker.py +77 -12
- package/scripts/config_cache.py +4 -3
- package/scripts/daemon.py +9 -2
- package/scripts/engine_add.py +45 -0
- package/scripts/engine_dispatch.py +27 -47
- package/scripts/engine_failure.py +33 -0
- package/scripts/engine_fingerprint.py +114 -0
- package/scripts/engine_health.py +130 -0
- package/scripts/engine_states.py +50 -0
- package/scripts/engines_base.py +7 -16
- package/scripts/engines_builders_search.py +19 -8
- package/scripts/engines_builders_tech.py +16 -16
- package/scripts/engines_fields.py +23 -0
- package/scripts/evidence_grounding.py +182 -0
- package/scripts/fetch_browser.py +25 -10
- package/scripts/gate_manifest_check.py +75 -0
- package/scripts/gen_readme_art.py +264 -0
- package/scripts/mcp_transport.py +24 -5
- package/scripts/numeric_verify.py +9 -0
- package/scripts/pre-commit +57 -8
- package/scripts/query_operators.py +229 -1
- package/scripts/quota.py +19 -37
- package/scripts/release.py +16 -1
- package/scripts/research_gates.py +85 -1
- package/scripts/route_combo.py +13 -1
- package/scripts/source_discovery.py +611 -0
- package/scripts/sync_backends.py +45 -0
- package/sub-skills/ego-search/README.md +9 -0
- package/sub-skills/ego-search/scripts/ego_search.py +182 -15
- package/sub-skills/ego-search/scripts/pacing.py +291 -0
- package/sub-skills/ego-search/scripts/quality.py +262 -6
- package/sub-skills/ego-search/scripts/runtime.py +7 -1
- package/sub-skills/ego-search/scripts/site_profiles.py +157 -0
- package/sub-skills/ego-search/scripts/webbridge_adapter.py +7 -1
|
@@ -7,8 +7,37 @@
|
|
|
7
7
|
from __future__ import annotations
|
|
8
8
|
|
|
9
9
|
import re
|
|
10
|
+
import sys
|
|
11
|
+
from pathlib import Path
|
|
10
12
|
from typing import Any
|
|
11
13
|
|
|
14
|
+
# 优先复用主仓 scripts/engine_states.py 单真源;子技能独立运行时回落同值常量
|
|
15
|
+
_ARGO_SCRIPTS = Path(__file__).resolve().parents[3] / "scripts"
|
|
16
|
+
if _ARGO_SCRIPTS.is_dir() and str(_ARGO_SCRIPTS) not in sys.path:
|
|
17
|
+
sys.path.insert(0, str(_ARGO_SCRIPTS))
|
|
18
|
+
try:
|
|
19
|
+
from engine_states import (
|
|
20
|
+
EGO_AUTH_COOLDOWN_STATES,
|
|
21
|
+
EGO_AUTH_FAILED,
|
|
22
|
+
EGO_AUTH_HEALTHY,
|
|
23
|
+
EGO_AUTH_OK,
|
|
24
|
+
EGO_AUTH_RESTRICTED,
|
|
25
|
+
EGO_AUTH_TO_PAGE_AUTH,
|
|
26
|
+
EGO_AUTH_UNKNOWN,
|
|
27
|
+
)
|
|
28
|
+
except ImportError: # pragma: no cover - 独立部署兜底
|
|
29
|
+
EGO_AUTH_OK, EGO_AUTH_HEALTHY = "ok", "healthy"
|
|
30
|
+
EGO_AUTH_FAILED, EGO_AUTH_RESTRICTED, EGO_AUTH_UNKNOWN = (
|
|
31
|
+
"auth_failed", "account_restricted", "unknown",
|
|
32
|
+
)
|
|
33
|
+
EGO_AUTH_COOLDOWN_STATES = frozenset({EGO_AUTH_FAILED, EGO_AUTH_RESTRICTED})
|
|
34
|
+
EGO_AUTH_TO_PAGE_AUTH = {
|
|
35
|
+
EGO_AUTH_OK: "ready",
|
|
36
|
+
EGO_AUTH_HEALTHY: "ready",
|
|
37
|
+
EGO_AUTH_FAILED: "signed_out",
|
|
38
|
+
EGO_AUTH_RESTRICTED: "search_restricted",
|
|
39
|
+
}
|
|
40
|
+
|
|
12
41
|
# 中英常见登录墙 / 验证码信号(低噪声关键词)
|
|
13
42
|
_AUTH_WALL_RE = re.compile(
|
|
14
43
|
r"(请先登录|立即登录|登录后查看|sign\s*in\s*to\s*continue|log\s*in\s*to\s*continue|"
|
|
@@ -20,30 +49,257 @@ _EMPTY_MIN_CHARS = 80
|
|
|
20
49
|
|
|
21
50
|
|
|
22
51
|
def assess_body(payload: dict[str, Any]) -> dict[str, Any]:
|
|
23
|
-
"""对 fetch/detail 类 payload 附加 quality
|
|
52
|
+
"""对 fetch/detail/api 类 payload 附加 quality 与 Cookie 鉴权归因字段。"""
|
|
24
53
|
if not isinstance(payload, dict):
|
|
25
54
|
return payload
|
|
26
|
-
|
|
27
|
-
|
|
55
|
+
cookie_names = payload.get("cookie_names")
|
|
56
|
+
strict_cookie = bool(payload.get("cookie_names_complete", False))
|
|
57
|
+
if payload.get("content") is None and isinstance(payload.get("detail"), dict):
|
|
28
58
|
# act 结构:评估 detail
|
|
29
59
|
d = payload["detail"]
|
|
30
60
|
if isinstance(d, dict):
|
|
31
61
|
d = dict(d)
|
|
32
|
-
|
|
62
|
+
d_url = str(d.get("url") or payload.get("url") or "")
|
|
63
|
+
d_cookies = d.get("cookie_names") if d.get("cookie_names") is not None else cookie_names
|
|
64
|
+
d["quality"] = assess_auth_with_cookies(
|
|
65
|
+
_score_text(d.get("content") or "", d.get("title") or ""),
|
|
66
|
+
url=d_url,
|
|
67
|
+
present_cookie_names=d_cookies,
|
|
68
|
+
strict_cookie_check=strict_cookie,
|
|
69
|
+
)
|
|
33
70
|
payload = dict(payload)
|
|
34
71
|
payload["detail"] = d
|
|
35
72
|
# 顶层也挂一份摘要
|
|
36
73
|
payload["quality"] = d["quality"]
|
|
74
|
+
if d["quality"].get("handoff_required"):
|
|
75
|
+
payload["handoff_required"] = True
|
|
76
|
+
payload["login_url"] = d["quality"].get("login_url") or ""
|
|
37
77
|
return payload
|
|
38
78
|
|
|
39
|
-
|
|
79
|
+
if payload.get("content") is None and payload.get("api_url"):
|
|
80
|
+
# api 结构:评估同源 API 返回 + Cookie 存在性 + 静默限权探针
|
|
81
|
+
api_err = payload.get("data_type") == "error" or payload.get("data") in (None, "", {}, [])
|
|
82
|
+
raw_preview = str(payload.get("data") or "")[:600]
|
|
83
|
+
base = {
|
|
84
|
+
"content_chars": len(raw_preview),
|
|
85
|
+
"empty_or_thin": api_err,
|
|
86
|
+
"auth_wall_suspected": bool(_AUTH_WALL_RE.search(raw_preview)),
|
|
87
|
+
"login_likely_ok": not api_err and not bool(_AUTH_WALL_RE.search(raw_preview)),
|
|
88
|
+
"hint": "api_error_or_empty" if api_err else "api_ok",
|
|
89
|
+
}
|
|
90
|
+
target_url = str(payload.get("api_url") or payload.get("page_url") or "")
|
|
91
|
+
q = assess_auth_with_cookies(
|
|
92
|
+
base,
|
|
93
|
+
url=target_url,
|
|
94
|
+
present_cookie_names=cookie_names,
|
|
95
|
+
strict_cookie_check=strict_cookie,
|
|
96
|
+
)
|
|
97
|
+
# 若携带懒触发的 health_probe 响应或接口本身内联返回限权码,优先判定 account_restricted
|
|
98
|
+
probe_res = assess_health_probe(
|
|
99
|
+
payload.get("health_probe") if payload.get("health_probe") is not None else payload.get("data"),
|
|
100
|
+
url=target_url,
|
|
101
|
+
)
|
|
102
|
+
if probe_res.get("auth_state") in EGO_AUTH_COOLDOWN_STATES:
|
|
103
|
+
q.update(probe_res)
|
|
104
|
+
out = dict(payload)
|
|
105
|
+
out["quality"] = q
|
|
106
|
+
if q.get("handoff_required"):
|
|
107
|
+
out["handoff_required"] = True
|
|
108
|
+
out["login_url"] = q.get("login_url") or ""
|
|
109
|
+
out["handoff"] = build_handoff(q, url=target_url)
|
|
110
|
+
return out
|
|
111
|
+
|
|
112
|
+
text = payload.get("content") if isinstance(payload.get("content"), str) else ""
|
|
40
113
|
title = payload.get("title") or ""
|
|
41
|
-
|
|
114
|
+
target_url = str(payload.get("url") or "")
|
|
115
|
+
q = assess_auth_with_cookies(
|
|
116
|
+
_score_text(text, str(title)),
|
|
117
|
+
url=target_url,
|
|
118
|
+
present_cookie_names=cookie_names,
|
|
119
|
+
strict_cookie_check=strict_cookie,
|
|
120
|
+
)
|
|
42
121
|
out = dict(payload)
|
|
43
122
|
out["quality"] = q
|
|
123
|
+
if q.get("handoff_required"):
|
|
124
|
+
out["handoff_required"] = True
|
|
125
|
+
out["login_url"] = q.get("login_url") or ""
|
|
126
|
+
out["handoff"] = build_handoff(q, url=target_url)
|
|
44
127
|
return out
|
|
45
128
|
|
|
46
129
|
|
|
130
|
+
def assess_health_probe(probe_data: Any, *, url: str = "", site: str = "") -> dict[str, Any]:
|
|
131
|
+
"""判定静默限权(Shadow Restriction)探针或内联接口响应。
|
|
132
|
+
|
|
133
|
+
核心区分:
|
|
134
|
+
- account_restricted:Cookie 仍在但账号被平台暗中风控限权(如小红书 code=300011),
|
|
135
|
+
重登同账号无效,需换活跃小号或退回 L0 公开检索。
|
|
136
|
+
- auth_failed:凭证过期或未登录(如 HTTP 401 / isLogin=false / code=-101)。
|
|
137
|
+
- healthy:探针正常。
|
|
138
|
+
"""
|
|
139
|
+
import site_profiles
|
|
140
|
+
|
|
141
|
+
profile = site_profiles.get_profile(site) if site else site_profiles.match_site(url)
|
|
142
|
+
if profile is None or probe_data is None:
|
|
143
|
+
return {"auth_state": EGO_AUTH_UNKNOWN}
|
|
144
|
+
|
|
145
|
+
blob = ""
|
|
146
|
+
code: int | None = None
|
|
147
|
+
if isinstance(probe_data, dict):
|
|
148
|
+
raw_code = probe_data.get("code")
|
|
149
|
+
if isinstance(raw_code, int):
|
|
150
|
+
code = raw_code
|
|
151
|
+
elif isinstance(raw_code, str) and raw_code.lstrip("-").isdigit():
|
|
152
|
+
code = int(raw_code)
|
|
153
|
+
# B 站 nav 接口 data.isLogin=False 明确表未登录
|
|
154
|
+
data_obj = probe_data.get("data")
|
|
155
|
+
if isinstance(data_obj, dict) and data_obj.get("isLogin") is False:
|
|
156
|
+
return {
|
|
157
|
+
"site": profile.name,
|
|
158
|
+
"login_url": profile.login_url,
|
|
159
|
+
"auth_state": EGO_AUTH_FAILED,
|
|
160
|
+
"login_likely_ok": False,
|
|
161
|
+
"handoff_required": True,
|
|
162
|
+
"hint": f"{profile.name} 身份探针返回未登录(isLogin=false),请在 {profile.login_url} 登录后重试",
|
|
163
|
+
}
|
|
164
|
+
blob = f"{probe_data.get('msg', '')} {probe_data.get('message', '')} {probe_data.get('error', '')}"
|
|
165
|
+
else:
|
|
166
|
+
blob = str(probe_data)[:500]
|
|
167
|
+
|
|
168
|
+
hit_code = code is not None and code in profile.restricted_codes
|
|
169
|
+
hit_kw = any(kw.lower() in blob.lower() for kw in profile.restricted_keywords)
|
|
170
|
+
if hit_code or hit_kw:
|
|
171
|
+
# B 站 -101 属于未登录,其余限权码归 account_restricted
|
|
172
|
+
is_unauth_only = (profile.name == "bilibili" and code == -101)
|
|
173
|
+
state = EGO_AUTH_FAILED if is_unauth_only else EGO_AUTH_RESTRICTED
|
|
174
|
+
hint = (
|
|
175
|
+
f"{profile.name} 未登录(code={code}),请在 {profile.login_url} 登录后重试"
|
|
176
|
+
if is_unauth_only
|
|
177
|
+
else (
|
|
178
|
+
f"检测到 {profile.name} 账号被平台静默限权(code={code}),重登同账号通常无效;"
|
|
179
|
+
"建议切换正常活跃小号,或退回 L0 公开检索(argo search)。"
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
return {
|
|
183
|
+
"site": profile.name,
|
|
184
|
+
"login_url": profile.login_url,
|
|
185
|
+
"auth_state": state,
|
|
186
|
+
"restricted_code": code,
|
|
187
|
+
"login_likely_ok": False,
|
|
188
|
+
"handoff_required": True,
|
|
189
|
+
"hint": hint,
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
"site": profile.name,
|
|
193
|
+
"auth_state": EGO_AUTH_HEALTHY,
|
|
194
|
+
"login_likely_ok": True,
|
|
195
|
+
"handoff_required": False,
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def build_handoff(quality_info: dict[str, Any], *, url: str = "") -> dict[str, Any]:
|
|
200
|
+
"""构造统一的 L3 人工接管契约载荷(含 L0 公开降级建议,绝不绕过反爬/验证码)。"""
|
|
201
|
+
import site_profiles
|
|
202
|
+
|
|
203
|
+
site_name = str(quality_info.get("site") or "")
|
|
204
|
+
profile = site_profiles.get_profile(site_name) if site_name else site_profiles.match_site(url)
|
|
205
|
+
auth_state = str(quality_info.get("auth_state") or EGO_AUTH_FAILED)
|
|
206
|
+
login_url = str(quality_info.get("login_url") or (profile.login_url if profile else ""))
|
|
207
|
+
scopes = list(profile.public_web_scopes) if profile else []
|
|
208
|
+
scope_expr = " OR ".join(f"site:{s}" for s in scopes) if scopes else ""
|
|
209
|
+
return {
|
|
210
|
+
"handoff_required": True,
|
|
211
|
+
"auth_state": auth_state,
|
|
212
|
+
"site": profile.name if profile else (site_name or "unknown"),
|
|
213
|
+
"login_url": login_url,
|
|
214
|
+
"clear_cooldown_cmd": (
|
|
215
|
+
f"argo ego-search status --clear-cooldown {profile.domains[0]}"
|
|
216
|
+
if profile and profile.domains
|
|
217
|
+
else "argo ego-search status --clear-cooldown"
|
|
218
|
+
),
|
|
219
|
+
"public_fallback": {
|
|
220
|
+
"tier": "L0",
|
|
221
|
+
"available": bool(scopes),
|
|
222
|
+
"site_scopes": scopes,
|
|
223
|
+
"suggested_query_filter": f"({scope_expr})" if scope_expr else "",
|
|
224
|
+
},
|
|
225
|
+
"action_required": (
|
|
226
|
+
"切换活跃小号或在浏览器人工完成登录/验证后,再清除冷却重试;"
|
|
227
|
+
"不尝试绕过验证码或风控。"
|
|
228
|
+
),
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def assess_auth_with_cookies(
|
|
233
|
+
base_quality: dict[str, Any],
|
|
234
|
+
*,
|
|
235
|
+
url: str = "",
|
|
236
|
+
present_cookie_names: list[str] | set[str] | None = None,
|
|
237
|
+
strict_cookie_check: bool = False,
|
|
238
|
+
) -> dict[str, Any]:
|
|
239
|
+
"""结合站点声明的登录 Cookie 名,精准区分登录失效与普通内容异常。
|
|
240
|
+
|
|
241
|
+
规则(只查键名存在性,绝不接触 Cookie 值):
|
|
242
|
+
- 未匹配到站点画像:只保留文本启发式嫌疑,不升级 auth_failed
|
|
243
|
+
(无 login_url 的 handoff 与冷却都是死路)。
|
|
244
|
+
- 匹配到画像但未采集到 Cookie 列表:正文/API 异常(auth_wall_suspected
|
|
245
|
+
或 empty_or_thin)即升级 auth_failed + handoff_required——画像自带
|
|
246
|
+
login_url,交接可执行。
|
|
247
|
+
- strict_cookie_check=True(如 CDP 全量含 HttpOnly 键名):关键 Cookie 全部缺失时
|
|
248
|
+
即使页面返回 200 壳子也判定为 auth_failed + handoff_required。
|
|
249
|
+
- strict_cookie_check=False(如页面 document.cookie,看不到 HttpOnly):仅在
|
|
250
|
+
正文/API 已出现异常(auth_wall_suspected 或 empty_or_thin)且关键 Cookie 未命中时,
|
|
251
|
+
才升级为 auth_failed + handoff_required,避免误杀 HttpOnly 登录态。
|
|
252
|
+
"""
|
|
253
|
+
import site_profiles
|
|
254
|
+
|
|
255
|
+
q = dict(base_quality)
|
|
256
|
+
profile = site_profiles.match_site(url)
|
|
257
|
+
if profile is None:
|
|
258
|
+
# 无画像站点只保留文本启发式嫌疑(auth_wall_suspected + hint),不升级
|
|
259
|
+
# auth_failed / handoff_required:没有站点身份就没有 login_url,升级后
|
|
260
|
+
# 触发的 1h 站点冷却是条死路,而正文含 cloudflare/captcha 等词并不代表
|
|
261
|
+
# 真登录墙(2026-11-21 探针 5 实证:误封类第三分支)。
|
|
262
|
+
return q
|
|
263
|
+
|
|
264
|
+
q["site"] = profile.name
|
|
265
|
+
q["login_url"] = profile.login_url
|
|
266
|
+
|
|
267
|
+
if present_cookie_names is None:
|
|
268
|
+
if q.get("auth_wall_suspected"):
|
|
269
|
+
q["auth_state"] = EGO_AUTH_FAILED
|
|
270
|
+
q["handoff_required"] = True
|
|
271
|
+
return q
|
|
272
|
+
|
|
273
|
+
present = {str(n).strip() for n in present_cookie_names if str(n).strip()}
|
|
274
|
+
expected = list(profile.cookie_names)
|
|
275
|
+
matched = [c for c in expected if c in present]
|
|
276
|
+
missing = [c for c in expected if c not in present]
|
|
277
|
+
|
|
278
|
+
q["cookie_probe"] = {
|
|
279
|
+
"site": profile.name,
|
|
280
|
+
"matched_count": len(matched),
|
|
281
|
+
"missing_cookies": missing,
|
|
282
|
+
}
|
|
283
|
+
all_missing = bool(expected) and not matched
|
|
284
|
+
body_suspect = bool(q.get("auth_wall_suspected") or q.get("empty_or_thin"))
|
|
285
|
+
|
|
286
|
+
# 判据与 docstring 对齐:Cookie 命中即登录态证据,正文嫌疑只作辅助——
|
|
287
|
+
# 旧第三子句让 matched 非空也被误判 auth_failed 并自动记 1h 站点冷却
|
|
288
|
+
# (2026-11-21 审计:误封锁站)。
|
|
289
|
+
if (strict_cookie_check and all_missing) or (all_missing and body_suspect):
|
|
290
|
+
q["auth_state"] = EGO_AUTH_FAILED
|
|
291
|
+
q["login_likely_ok"] = False
|
|
292
|
+
q["handoff_required"] = True
|
|
293
|
+
q["hint"] = (
|
|
294
|
+
f"检测到 {profile.name} 登录态失效(缺少关键 Cookie: {', '.join(missing) or 'auth'}),"
|
|
295
|
+
f"请在浏览器打开 {profile.login_url} 人工登录后重试"
|
|
296
|
+
)
|
|
297
|
+
else:
|
|
298
|
+
q["auth_state"] = EGO_AUTH_OK
|
|
299
|
+
q["handoff_required"] = False
|
|
300
|
+
return q
|
|
301
|
+
|
|
302
|
+
|
|
47
303
|
def _score_text(text: str, title: str = "") -> dict[str, Any]:
|
|
48
304
|
blob = f"{title}\n{text}"
|
|
49
305
|
n = len(text.strip())
|
|
@@ -15,6 +15,12 @@ import urllib.error
|
|
|
15
15
|
import urllib.request
|
|
16
16
|
from typing import Any, Literal
|
|
17
17
|
|
|
18
|
+
try:
|
|
19
|
+
from net_proxy import open_url as _open_url
|
|
20
|
+
except ImportError:
|
|
21
|
+
# scripts/ 不在路径(独立打包形态)时回退裸 urlopen,行为与改动前一致
|
|
22
|
+
_open_url = urllib.request.urlopen # type: ignore[assignment]
|
|
23
|
+
|
|
18
24
|
RuntimeName = Literal["ego", "webbridge", "none"]
|
|
19
25
|
|
|
20
26
|
EGO_BIN = os.environ.get("EGO_SEARCH_EGO_BIN", "ego-browser")
|
|
@@ -77,7 +83,7 @@ def webbridge_available(*, try_start: bool = False) -> dict[str, Any]:
|
|
|
77
83
|
headers={"Content-Type": "application/json"},
|
|
78
84
|
method="POST",
|
|
79
85
|
)
|
|
80
|
-
with
|
|
86
|
+
with _open_url(req, timeout=PROBE_TIMEOUT) as resp:
|
|
81
87
|
raw = resp.read().decode("utf-8", errors="replace")
|
|
82
88
|
data = json.loads(raw) if raw else {}
|
|
83
89
|
# 协议:{ok, data:{success, tabs}} 或 data 直接 success
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""围墙花园站点画像:只读登录 Cookie 探针 + 声明式同源搜索 API 预设。
|
|
4
|
+
|
|
5
|
+
设计原则(唯一真源):
|
|
6
|
+
1. auth_probe 只声明「登录态标识 Cookie 名称」,绝不读取、打印或持久化 Cookie 值;
|
|
7
|
+
在页面返回 200 推荐壳子时,依靠关键 Cookie 缺失将模糊异常精准翻转为 auth_failed
|
|
8
|
+
+ handoff_required。
|
|
9
|
+
2. inpage_api 预设让高频围墙站通过 `ego-search api --preset <site> --query <q>`
|
|
10
|
+
在真实浏览器上下文直取结构化 JSON,替代脆弱的 DOM 解析与人工拼 URL。
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import urllib.parse
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass(frozen=True)
|
|
20
|
+
class SiteProfile:
|
|
21
|
+
name: str
|
|
22
|
+
domains: tuple[str, ...]
|
|
23
|
+
login_url: str
|
|
24
|
+
cookie_names: tuple[str, ...]
|
|
25
|
+
api_origin: str = ""
|
|
26
|
+
api_url_template: str = ""
|
|
27
|
+
api_default_n: int = 10
|
|
28
|
+
# L0:免登录公开检索范围(供未开 pro-mode 或触发限权/冷却时降级提示)
|
|
29
|
+
public_web_scopes: tuple[str, ...] = ()
|
|
30
|
+
# L1:静默限权(Shadow Restriction)二次懒探针端点与特征码
|
|
31
|
+
health_probe_url: str = ""
|
|
32
|
+
restricted_codes: tuple[int, ...] = ()
|
|
33
|
+
restricted_keywords: tuple[str, ...] = ()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
SITE_PROFILES: dict[str, SiteProfile] = {
|
|
37
|
+
"zhihu": SiteProfile(
|
|
38
|
+
name="zhihu",
|
|
39
|
+
domains=("zhihu.com",),
|
|
40
|
+
login_url="https://www.zhihu.com/signin",
|
|
41
|
+
cookie_names=("z_c0",),
|
|
42
|
+
api_origin="https://www.zhihu.com",
|
|
43
|
+
api_url_template="https://www.zhihu.com/api/v4/search_v3?t=general&q={q}&offset=0&limit={n}",
|
|
44
|
+
public_web_scopes=("zhihu.com/question", "zhuanlan.zhihu.com/p"),
|
|
45
|
+
health_probe_url="https://www.zhihu.com/api/v4/me",
|
|
46
|
+
restricted_codes=(40352, 40362),
|
|
47
|
+
restricted_keywords=("账号异常", "安全验证", "请完成验证"),
|
|
48
|
+
),
|
|
49
|
+
"xiaohongshu": SiteProfile(
|
|
50
|
+
name="xiaohongshu",
|
|
51
|
+
domains=("xiaohongshu.com", "xhslink.com"),
|
|
52
|
+
login_url="https://www.xiaohongshu.com/explore",
|
|
53
|
+
cookie_names=("web_session", "a1"),
|
|
54
|
+
api_origin="https://www.xiaohongshu.com",
|
|
55
|
+
api_url_template="https://www.xiaohongshu.com/search_result?keyword={q}&source=web_search_result_notes",
|
|
56
|
+
public_web_scopes=("xiaohongshu.com/explore", "xiaohongshu.com/discovery/item"),
|
|
57
|
+
health_probe_url="https://edith.xiaohongshu.com/api/sns/web/v2/user/me",
|
|
58
|
+
restricted_codes=(300011, 300012, -100),
|
|
59
|
+
restricted_keywords=("当前账号存在异常", "请切换账号", "账号异常", "安全限制"),
|
|
60
|
+
),
|
|
61
|
+
"x": SiteProfile(
|
|
62
|
+
name="x",
|
|
63
|
+
domains=("x.com", "twitter.com"),
|
|
64
|
+
login_url="https://x.com/i/flow/login",
|
|
65
|
+
cookie_names=("auth_token", "ct0"),
|
|
66
|
+
public_web_scopes=("x.com", "twitter.com"),
|
|
67
|
+
restricted_codes=(326, 64),
|
|
68
|
+
restricted_keywords=("account is temporarily locked", "suspended", "unusual activity"),
|
|
69
|
+
),
|
|
70
|
+
"xueqiu": SiteProfile(
|
|
71
|
+
name="xueqiu",
|
|
72
|
+
domains=("xueqiu.com",),
|
|
73
|
+
login_url="https://xueqiu.com/",
|
|
74
|
+
cookie_names=("xq_a_token", "u"),
|
|
75
|
+
api_origin="https://xueqiu.com",
|
|
76
|
+
api_url_template="https://xueqiu.com/query/v1/symbol/search/status.json?q={q}&count={n}&page=1",
|
|
77
|
+
public_web_scopes=("xueqiu.com",),
|
|
78
|
+
health_probe_url="https://xueqiu.com/account/status.json",
|
|
79
|
+
restricted_codes=(400016, 400018),
|
|
80
|
+
restricted_keywords=("访问过于频繁", "账号异常"),
|
|
81
|
+
),
|
|
82
|
+
"weibo": SiteProfile(
|
|
83
|
+
name="weibo",
|
|
84
|
+
domains=("weibo.com", "s.weibo.com"),
|
|
85
|
+
login_url="https://weibo.com/login.php",
|
|
86
|
+
cookie_names=("SUB", "SUBP"),
|
|
87
|
+
api_origin="https://weibo.com",
|
|
88
|
+
api_url_template="https://weibo.com/ajax/side/search?q={q}",
|
|
89
|
+
public_web_scopes=("weibo.com", "s.weibo.com"),
|
|
90
|
+
health_probe_url="https://weibo.com/ajax/profile/info",
|
|
91
|
+
restricted_codes=(100003, 20003),
|
|
92
|
+
restricted_keywords=("账号异常", "访问频次过高"),
|
|
93
|
+
),
|
|
94
|
+
"bilibili": SiteProfile(
|
|
95
|
+
name="bilibili",
|
|
96
|
+
domains=("bilibili.com",),
|
|
97
|
+
login_url="https://passport.bilibili.com/login",
|
|
98
|
+
cookie_names=("SESSDATA", "bili_jct"),
|
|
99
|
+
api_origin="https://www.bilibili.com",
|
|
100
|
+
api_url_template="https://api.bilibili.com/x/web-interface/search/all/v2?keyword={q}&page_size={n}",
|
|
101
|
+
public_web_scopes=("bilibili.com/video", "bilibili.com/read"),
|
|
102
|
+
health_probe_url="https://api.bilibili.com/x/web-interface/nav",
|
|
103
|
+
restricted_codes=(-101, -352, -412),
|
|
104
|
+
restricted_keywords=("风控", "请求被拦截", "未登录"),
|
|
105
|
+
),
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
_ALIASES: dict[str, str] = {
|
|
109
|
+
"twitter": "x",
|
|
110
|
+
"xhs": "xiaohongshu",
|
|
111
|
+
"bili": "bilibili",
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def get_profile(name: str) -> SiteProfile | None:
|
|
116
|
+
key = (name or "").strip().lower()
|
|
117
|
+
key = _ALIASES.get(key, key)
|
|
118
|
+
return SITE_PROFILES.get(key)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def match_site(url_or_host: str) -> SiteProfile | None:
|
|
122
|
+
"""根据完整 URL 或主机名匹配站点画像。"""
|
|
123
|
+
raw = (url_or_host or "").strip().lower()
|
|
124
|
+
if not raw:
|
|
125
|
+
return None
|
|
126
|
+
host = urllib.parse.urlparse(raw).hostname if "://" in raw else raw.split("/", 1)[0]
|
|
127
|
+
host = (host or "").strip(".").lower()
|
|
128
|
+
if not host:
|
|
129
|
+
return None
|
|
130
|
+
for profile in SITE_PROFILES.values():
|
|
131
|
+
for dom in profile.domains:
|
|
132
|
+
if host == dom or host.endswith("." + dom):
|
|
133
|
+
return profile
|
|
134
|
+
return None
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def resolve_preset_api(preset: str, query: str, *, n: int = 10) -> dict[str, Any]:
|
|
138
|
+
"""把站点预设 + 查询词展开为同源 API URL 与 origin。"""
|
|
139
|
+
profile = get_profile(preset)
|
|
140
|
+
if profile is None:
|
|
141
|
+
valid = ", ".join(sorted(k for k, v in SITE_PROFILES.items() if v.api_url_template))
|
|
142
|
+
raise ValueError(f"未知站点预设 {preset!r};支持:{valid}")
|
|
143
|
+
if not profile.api_url_template or not profile.api_origin:
|
|
144
|
+
raise ValueError(f"站点 {profile.name} 暂无声明式同源 API 模板,请直接传入 api_url")
|
|
145
|
+
q_enc = urllib.parse.quote(query.strip())
|
|
146
|
+
limit = max(1, min(int(n), 50))
|
|
147
|
+
return {
|
|
148
|
+
"site": profile.name,
|
|
149
|
+
"origin": profile.api_origin,
|
|
150
|
+
"api_url": profile.api_url_template.format(q=q_enc, n=limit),
|
|
151
|
+
"login_url": profile.login_url,
|
|
152
|
+
"cookie_names": list(profile.cookie_names),
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def list_api_presets() -> list[str]:
|
|
157
|
+
return sorted(k for k, v in SITE_PROFILES.items() if v.api_url_template)
|
|
@@ -18,6 +18,12 @@ from datetime import datetime, timedelta
|
|
|
18
18
|
from pathlib import Path
|
|
19
19
|
from typing import Any
|
|
20
20
|
|
|
21
|
+
try:
|
|
22
|
+
from net_proxy import open_url as _open_url
|
|
23
|
+
except ImportError:
|
|
24
|
+
# scripts/ 不在路径(独立打包形态)时回退裸 urlopen,行为与改动前一致
|
|
25
|
+
_open_url = urllib.request.urlopen # type: ignore[assignment]
|
|
26
|
+
|
|
21
27
|
# 同目录 safety
|
|
22
28
|
_SCRIPT_DIR = Path(__file__).resolve().parent
|
|
23
29
|
# argo 核心 scripts(全文存档等公共能力);与本目录其他模块同一解析方式
|
|
@@ -135,7 +141,7 @@ def _command(action: str, args: dict | None, session: str, timeout: int = DEFAUL
|
|
|
135
141
|
method="POST",
|
|
136
142
|
)
|
|
137
143
|
try:
|
|
138
|
-
with
|
|
144
|
+
with _open_url(req, timeout=timeout) as resp:
|
|
139
145
|
raw = resp.read().decode("utf-8", errors="replace")
|
|
140
146
|
except (urllib.error.URLError, TimeoutError, OSError) as e:
|
|
141
147
|
return {"ok": False, "error": f"webbridge_unreachable: {e}"}
|