@jayyuen66/dsh-scrapling 0.1.2 → 0.1.4
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/CHANGELOG.md +83 -0
- package/README.md +141 -19
- package/README.zh-CN.md +141 -19
- package/THIRD_PARTY_NOTICES.md +20 -2
- package/bin/dsh-scrapling.mjs +13 -0
- package/client.js +11 -3
- package/cordis.patch.yml +1 -1
- package/host.js +40 -36
- package/package.json +26 -9
- package/py/adapters.py +763 -267
- package/py/bridge.py +27 -4
- package/py/crawl.py +640 -90
- package/py/extraction.py +400 -0
- package/py/guard.py +120 -27
- package/py/media.py +474 -0
- package/py/redact.py +118 -0
- package/py/redis_storage.py +129 -0
- package/py/runtime-requirements.txt +49 -0
- package/py/search.py +404 -0
- package/py/session_page.py +240 -0
- package/runtime-manifest.json +16 -0
package/py/bridge.py
CHANGED
|
@@ -50,10 +50,12 @@ from typing import Any, Callable
|
|
|
50
50
|
# 会让整帧写不出去,替换成转义文本至少留下可读的痕迹。
|
|
51
51
|
# 用 getattr 取 reconfigure:被重定向成 io.StringIO 的流没有这个方法(测试与嵌宿主都可能
|
|
52
52
|
# 这么做),缺了它不该让整个 sidecar 起不来。
|
|
53
|
+
# newline="\n":协议帧以 LF 分帧,而 Windows 的文本模式会把 "\n" 翻成 "\r\n";宿主侧虽然
|
|
54
|
+
# 容忍行尾的 \r,但那是容忍,不该由协议来依赖。
|
|
53
55
|
for _stream in (sys.stdout, sys.stderr):
|
|
54
56
|
_reconfigure = getattr(_stream, "reconfigure", None)
|
|
55
57
|
if callable(_reconfigure):
|
|
56
|
-
_reconfigure(encoding="utf-8", errors="backslashreplace")
|
|
58
|
+
_reconfigure(encoding="utf-8", errors="backslashreplace", newline="\n")
|
|
57
59
|
|
|
58
60
|
MIN_PYTHON = (3, 14)
|
|
59
61
|
|
|
@@ -135,15 +137,19 @@ def ok(request_id: Any, result: Any) -> dict[str, Any]:
|
|
|
135
137
|
return {"id": request_id, "ok": True, "result": result}
|
|
136
138
|
|
|
137
139
|
|
|
138
|
-
def fail(request_id: Any, code: str, message: str) -> dict[str, Any]:
|
|
140
|
+
def fail(request_id: Any, code: str, message: str, details: dict[str, Any] | None = None) -> dict[str, Any]:
|
|
139
141
|
"""构造失败响应帧。
|
|
140
142
|
|
|
141
143
|
:param request_id - 原样回传请求的 id
|
|
142
144
|
:param code - 机器可读错误码
|
|
143
145
|
:param message - 人读信息
|
|
146
|
+
:param details - 可选的机器可读细节(例如动作失败的下标)
|
|
144
147
|
:returns: 响应帧
|
|
145
148
|
"""
|
|
146
|
-
|
|
149
|
+
error: dict[str, Any] = {"code": code, "message": message}
|
|
150
|
+
if details:
|
|
151
|
+
error["details"] = details
|
|
152
|
+
return {"id": request_id, "ok": False, "error": error}
|
|
147
153
|
|
|
148
154
|
|
|
149
155
|
def handle(frame: dict[str, Any]) -> dict[str, Any]:
|
|
@@ -172,7 +178,7 @@ def handle(frame: dict[str, Any]) -> dict[str, Any]:
|
|
|
172
178
|
try:
|
|
173
179
|
return ok(request_id, handler(params))
|
|
174
180
|
except adapters.AdapterError as error:
|
|
175
|
-
return fail(request_id, error.code, str(error))
|
|
181
|
+
return fail(request_id, error.code, str(error), error.details)
|
|
176
182
|
except ModuleNotFoundError as error:
|
|
177
183
|
# 缺的是可选依赖,不是抓取失败。落到下面的 FETCH_FAILED 会把它说成「请求出错」,
|
|
178
184
|
# 而模型与用户对这两种情况的正确反应完全不同:前者该退避重试,后者该去装包。
|
|
@@ -255,6 +261,21 @@ def _ensure_worker(count: int) -> None:
|
|
|
255
261
|
worker.start()
|
|
256
262
|
|
|
257
263
|
|
|
264
|
+
def _shutdown_crawls() -> None:
|
|
265
|
+
"""退出前叫停全部爬虫,并给它们的线程一点收尾时间。
|
|
266
|
+
|
|
267
|
+
蜘蛛跑在自己的线程里:进程退出它们才退,中间还能往 stdout 推事件帧 —— 那时宿主已经
|
|
268
|
+
不读这一路了,帧落进管道就是没人认领的字节。所以爬虫要在会话之前关:蜘蛛自己也要用
|
|
269
|
+
浏览器,先把会话抽走只会让它在下一次请求里抛。
|
|
270
|
+
"""
|
|
271
|
+
try:
|
|
272
|
+
import crawl
|
|
273
|
+
|
|
274
|
+
crawl.close_all_crawls()
|
|
275
|
+
except Exception as error: # noqa: BLE001 —— 关不掉也不能拖住关机
|
|
276
|
+
log(f"failed to stop crawls: {type(error).__name__}: {error}")
|
|
277
|
+
|
|
278
|
+
|
|
258
279
|
def _shutdown_sessions() -> None:
|
|
259
280
|
"""退出前关掉全部会话。
|
|
260
281
|
|
|
@@ -340,6 +361,7 @@ def read_loop() -> int:
|
|
|
340
361
|
_drain_before_exit()
|
|
341
362
|
# 与 EOF 路径同一套收尾:浏览器会话是独立进程,不显式关掉就会活过 sidecar
|
|
342
363
|
# 本身。宿主的优雅关闭走的就是这一帧(见 lib/sidecar.ts 的 dispose)。
|
|
364
|
+
_shutdown_crawls()
|
|
343
365
|
_shutdown_sessions()
|
|
344
366
|
return 0
|
|
345
367
|
# 顺序要紧:先入队再起 worker。反过来的话,新起的线程可能立刻醒来、
|
|
@@ -364,6 +386,7 @@ def read_loop() -> int:
|
|
|
364
386
|
_ensure_worker(min(depth, MAX_INFLIGHT))
|
|
365
387
|
log("stdin closed")
|
|
366
388
|
_drain_before_exit()
|
|
389
|
+
_shutdown_crawls()
|
|
367
390
|
_shutdown_sessions()
|
|
368
391
|
return 0
|
|
369
392
|
|