Peach: Local-first video metadata scraper and media manager.
fix(follow): 归档站原始文件补 /data 前缀并按站选主机
longmeidao8dbe24b7ec4a
diff --git a/docs/reference-snapshots/kemono-archive-media-host.md b/docs/reference-snapshots/kemono-archive-media-host.mdindex 3142d2682..aa1195725 100644--- a/docs/reference-snapshots/kemono-archive-media-host.md+++ b/docs/reference-snapshots/kemono-archive-media-host.md@@ -28,20 +28,40 @@`follow_sources.py` 里原来那条注释记的是 2026-08-27 主域回 200 `image/jpeg`——站点行为在这三天里变了。**这类"当前哪个主机能取到"的结论必须带日期,并且会过期。**-## 未取得+## 原始文件(非缩略图)-pawchive 的**原始媒体**(`media_url`,非缩略图)正确形式未取得。已测且全部 404:+缩略图和原始文件走**不同的主机与路径**,而且三站规则并不一致。真实 URL 不是猜出来的:+pawchive 的帖子页是服务端渲染的,`<source src>` 里就写着;kemono 是 SPA 外壳,+但它的主域会用 302 告诉你当前节点。**别再逐个试子域了,问站点自己。**- pawchive.pw/<path>- pawchive.pw/data/<path>- img.pawchive.pw/<path>- img.pawchive.pw/data/<path>+| 请求 | 结果 |+| --- | --- |+| `pawchive.pw/<path>`(旧代码拼的) | 404 |+| `pawchive.pw/data/<path>` | 404,主域对 /data 也不重定向 |+| `file.pawchive.pw/data/<path>` | **206** video/mp4,支持 Range |+| `kemono.cr/data/<path>` | 302 → `n1.kemono.cr/data/<path>` |+| `coomer.st/data/<path>` | 302 → `n4.coomer.st/data/<path>` |-只有 `img.pawchive.pw/thumbnail/data/<path>` 可用,那是缩略图路径。原始文件可能需要-从帖子页另取链接,或者需要会话。**没有猜测代入**:详情里的媒体在拿到确证之前仍会失败,-不用缩略图冒充原图。+两处结论:-## 复核方式+1. **路径要带 `/data` 前缀。**旧代码拼的是 `https://<主域><path>`,少了这一段,+ 三站的原始文件都取不到——不只是 pawchive。+2. **主机分两种处理。**kemono/coomer 走主域让站点自己 302(`nX` 的编号会变,+ 写死会过期);pawchive 主域对 `/data` 直接 404,必须点名 `file.` 子域。++`follow_stream._allowed` 按后缀匹配原站域名,`file.` / `n1.` 这些子域天然在白名单内,+安全边界没有放宽。++## 连通性:kemono 的文件节点未取得++`n1.kemono.cr` 在本机网络下连不上(curl 返回 000),而同站的 `img.kemono.cr` 正常+200。所以这不是 URL 规则问题,是那个节点在本网络不可达。++`HttpxTransport` 用的是默认 `httpx.Client()`:`trust_env=True` 会读 `HTTPS_PROXY`,+`follow_redirects=True` 会跟随 302。**要走代理不必改代码**,给服务进程设环境变量即可。+是否要为归档站配代理是网络决策,留给用户。++## 复核方式## 复核方式```bashcurl -s -o /dev/null -w "%{http_code} %{content_type} %{size_download}\n" \diff --git a/src/peach/follow_sources.py b/src/peach/follow_sources.pyindex 1afb0cd44..d421d3b81 100644--- a/src/peach/follow_sources.py+++ b/src/peach/follow_sources.py@@ -490,6 +490,14 @@ class KemonoConnector(_BaseConnector):semantics = "work"HOSTS = {"kemono": "kemono.cr", "coomer": "coomer.st", "pawchive": "pawchive.pw"}+ #: 原始文件的主机。2026-08-30 实测(取证见+ #: `docs/reference-snapshots/kemono-archive-media-host.md`)三站行为并不一致:+ #: kemono/coomer 的主域对 `/data/<path>` 回 302,分别指向 `n1.` 和 `n4.` 节点——+ #: 编号会变,所以走主域让站点自己路由,不写死;pawchive 主域对 `/data` 直接 404,+ #: 必须点名 `file.` 子域。+ #: 路径也要带 `/data` 前缀:以前拼的是 `https://<host><path>`,少了这一段,+ #: 三站都取不到原始文件。+ FILE_HOSTS = {"pawchive": "file.pawchive.pw"}_SERVICE_RE = re.compile(r"^[a-z0-9_\-]{1,32}$")_USER_RE = re.compile(r"^[A-Za-z0-9_\-.]{1,64}$")@@ -663,7 +671,8 @@ class KemonoConnector(_BaseConnector):external_id=post_id or stable_id(title, str(post.get("published"))),title=title,url=page,- media_url=f"https://{self.host}{media}" if media else None,+ media_url=(f"https://{self.FILE_HOSTS.get(self.provider, self.host)}"+ f"/data{media}") if media else None,thumb_url=self._thumb_url(preview),published_at=_iso_from_text(post.get("published")),author=None,@@ -677,6 +686,31 @@ class KemonoConnector(_BaseConnector):)++def archive_file_url(provider: str, url: str) -> str:+ """把归档站的**原始文件** URL 修成能取到的形式。++ 存量行是按旧规则拼的 `https://<主域><path>`——既少了 `/data` 前缀,主机也不对,+ 所以详情里的视频一直取不到。2026-08-30 实测:++ pawchive.pw/<path> 404+ pawchive.pw/data/<path> 404 ← 主域对 /data 也不重定向+ file.pawchive.pw/data/<path> 206 video/mp4,支持 Range+ kemono.cr/data/<path> 302 → n1.kemono.cr+ coomer.st/data/<path> 302 → n4.coomer.st++ kemono/coomer 走主域让站点自己路由(nX 的编号会变,写死会过期);+ pawchive 必须点名 `file.` 子域。缩略图仍走 `img.`,见 `_archive_media_host`。+ """+ host = KemonoConnector.HOSTS.get(provider)+ if not host or not url.startswith(f"https://{host}/"):+ return url+ path = url[len(f"https://{host}") :]+ if not path.startswith("/data/"):+ path = "/data" + path+ return f"https://{KemonoConnector.FILE_HOSTS.get(provider, host)}{path}"++class Rule34VideoConnector(_BaseConnector):"""rule34video.com 的创作者页(KVS 引擎,无公开 API,只能读 HTML)。diff --git a/src/peach/follow_stream.py b/src/peach/follow_stream.pyindex 9bb501623..3c6a657b8 100644--- a/src/peach/follow_stream.py+++ b/src/peach/follow_stream.py@@ -14,7 +14,7 @@ from dataclasses import dataclassfrom typing import Callable, Mappingfrom . import follow_providers-from .follow_sources import USER_AGENT, f95_attachment_media_items+from .follow_sources import USER_AGENT, archive_file_url, f95_attachment_media_itemsfrom .follow_secrets import Credentialfrom .follow_store import FollowItemRowfrom .http import HttpRequest, HttpTransport@@ -101,9 +101,13 @@ class FollowMediaResolver:return ResolvedFollowMedia(url, item.url)raise FollowMediaUnavailable("媒体来源不受支持")if item.provider != "rule34video":- if not item.media_url or not _allowed(item.provider, item.media_url):+ # 归档站的存量行按旧规则拼过 URL(少 /data 前缀、主机也不对),+ # 在这里修回可取的形式;`_allowed` 按后缀匹配,file./n1. 这些子域+ # 仍在原站的白名单内,安全边界没有放宽。+ media_url = archive_file_url(item.provider, str(item.media_url or ""))+ if not media_url or not _allowed(item.provider, media_url):raise FollowMediaUnavailable("来源媒体地址不可用")- return ResolvedFollowMedia(item.media_url, item.url)+ return ResolvedFollowMedia(media_url, item.url)with self._lock:cached = self._cache.get(item.id)diff --git a/src/peach/web_follow.py b/src/peach/web_follow.pyindex 46dc5d798..b9ff77b25 100644--- a/src/peach/web_follow.py+++ b/src/peach/web_follow.py@@ -177,6 +177,8 @@ def _archive_media_host(provider: str, url: str) -> str:return url.replace(f"https://{host}/", f"https://img.{host}/", 1)++def _thumb_url(item) -> str | None:# rule34.xxx 的历史行存的是 250px preview。官方 dapi 的 sample_url 与它使用# 同一 bucket/hash;2026-08-28 对生产历史行实测推导后为 1920x1080。diff --git a/tests/test_follow_sources.py b/tests/test_follow_sources.pyindex 6f6df9a59..c43a63aa7 100644--- a/tests/test_follow_sources.py+++ b/tests/test_follow_sources.py@@ -393,7 +393,8 @@ class KemonoConnectorTests(unittest.TestCase):# 交付文件取非图片的那个:夹具里 file 是 a.png、附件是 a.zip,压缩包才是正片。# 按 file.path 优先会把整条判成图片——pawchive 上作者用 gif 当预览、mp4 放附件,# 正是这条路径让两个 1080p 正片消失(2026-08-30 取证)。- self.assertEqual(first.media_url, "https://kemono.cr/1c/fa/1cfae7.zip")+ # /data 前缀是必需的:少了它三站的原始文件都是 404(2026-08-30 取证)。+ self.assertEqual(first.media_url, "https://kemono.cr/data/1c/fa/1cfae7.zip")self.assertEqual(first.extra["attachment_count"], 1)# post id 就是原平台的 post id,和别的站点从 source 归一出的键同一个命名空间。self.assertEqual(first.group_hint, "fanbox:11406814")@@ -422,7 +423,7 @@ class KemonoConnectorTests(unittest.TestCase):result = KemonoConnector(transport=_transport(body=json.dumps(posts).encode())).fetch("fanbox/1")candidate = result.candidates[0]- self.assertEqual(candidate.media_url, "https://kemono.cr/video/release.mp4")+ self.assertEqual(candidate.media_url, "https://kemono.cr/data/video/release.mp4")self.assertEqual(candidate.thumb_url,"https://img.kemono.cr/thumbnail/data/cover/release.webp")diff --git a/tests/test_follow_web.py b/tests/test_follow_web.pyindex e69bbcd20..2669184ca 100644--- a/tests/test_follow_web.py+++ b/tests/test_follow_web.py@@ -455,6 +455,43 @@ class FollowContractTests(unittest.TestCase):'"public, max-age=31536000, immutable"', ''),"只有 /vendor/ 那条可以 immutable")+ def test_archive_file_urls_get_the_data_prefix_and_the_right_host(self):+ """原始文件和缩略图走不同主机与路径,三站规则还不一样。++ 2026-08-30 实测(取证见 docs/reference-snapshots/kemono-archive-media-host.md):+ 旧代码拼的是 `https://<主域><path>`,少了 `/data` 前缀——三站的原始文件都取不到,+ 不只是 pawchive。pawchive 主域对 /data 也直接 404,必须点名 file. 子域;+ kemono/coomer 走主域让站点自己 302 到当前的 nX 节点(编号会变,不写死)。+ """+ from peach.follow_sources import archive_file_url++ self.assertEqual(+ archive_file_url("pawchive", "https://pawchive.pw/b7/d5/x.mp4"),+ "https://file.pawchive.pw/data/b7/d5/x.mp4")+ self.assertEqual(+ archive_file_url("kemono", "https://kemono.cr/7e/6b/y.jpg"),+ "https://kemono.cr/data/7e/6b/y.jpg")+ self.assertEqual(+ archive_file_url("coomer", "https://coomer.st/aa/bb/z.jpg"),+ "https://coomer.st/data/aa/bb/z.jpg")+ # 已经带 /data 的不再叠加+ self.assertEqual(+ archive_file_url("pawchive", "https://pawchive.pw/data/b7/d5/x.mp4"),+ "https://file.pawchive.pw/data/b7/d5/x.mp4")+ # 别的站点原样返回+ self.assertEqual(+ archive_file_url("rule34video", "https://rule34video.com/a.mp4"),+ "https://rule34video.com/a.mp4")++ def test_archive_file_subdomains_stay_inside_the_host_allowlist(self):+ """改写后的主机仍要过播放代理的白名单,安全边界不许因此放宽。"""+ from peach.follow_stream import _allowed++ self.assertTrue(_allowed("pawchive", "https://file.pawchive.pw/data/a.mp4"))+ self.assertTrue(_allowed("kemono", "https://n1.kemono.cr/data/a.jpg"))+ self.assertFalse(_allowed("pawchive", "https://evil.test/data/a.mp4"))+ self.assertFalse(_allowed("pawchive", "http://file.pawchive.pw/data/a.mp4"))+def test_archive_thumbnails_use_the_img_subdomain(self):"""归档站静态资源走 img. 子域,主域取不到。