Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

feat(follow): fanbox 正文媒体补收封面图

longmeidao14ee054daf4a
diff --git a/src/peach/fanbox.py b/src/peach/fanbox.py
index 44e4b4747..674bf9463 100644
--- a/src/peach/fanbox.py
+++ b/src/peach/fanbox.py
@@ -100,6 +100,28 @@ def _embed_url(node: Mapping[str, object]) -> str | None:
return None
+def _add_cover(post: Mapping[str, object], media: list[dict],
+ media_urls: set[str]) -> None:
+ """封面追加到媒体清单末尾。
+
+ 封面也是这篇的图,收进媒体清单;排在正文之后,卡片缩略图仍然落在正文首图上。
+ 站点给的是 1200x630 的展示裁切版,不是原画质。
+ """
+ cover = _https_url(post.get("coverImageUrl"))
+ if not cover or cover in media_urls:
+ return
+ media_urls.add(cover)
+ media.append({
+ "id": "cover",
+ "name": "封面",
+ "url": cover,
+ "thumb_url": cover,
+ "media_kind": "image",
+ "size": None,
+ "resource_provider": "fanbox",
+ })
+
+
def normalize_fanbox_post(post: Mapping[str, object]) -> FanboxContent:
"""Return ordered text, links and playable media from one public post."""
post_type = str(post.get("type") or "image").casefold()
@@ -222,6 +244,8 @@ def normalize_fanbox_post(post: Mapping[str, object]) -> FanboxContent:
add_link(node.get("url"))
add_media(node, item_id=f"file-{index}", fallback_name=f"文件 {file_count}")
+ _add_cover(post, media, media_urls)
+
video = body.get("video")
add_link(video)
if isinstance(video, Mapping):
diff --git a/src/peach/follow_stream.py b/src/peach/follow_stream.py
index 6cbbcbe3e..551905873 100644
--- a/src/peach/follow_stream.py
+++ b/src/peach/follow_stream.py
@@ -175,10 +175,12 @@ class FollowMediaResolver:
url, item.url, {"Authorization": f"Bearer {token}"},
allowed_hosts=("gofile.io",))
if resource_provider == "fanbox":
- if not _allowed_resource(url, ("fanbox.cc",)):
+ # 正文图在 downloads.fanbox.cc;封面在 pixiv.pximg.net——pixiv 官方
+ # CDN,fanbox 封面都从它出,与 fanbox.cc 同属一个运营方。
+ if not _allowed_resource(url, ("fanbox.cc", "pixiv.pximg.net")):
raise FollowMediaUnavailable("FANBOX 返回了不受信任的图片地址")
return ResolvedFollowMedia(url, item.url,
- allowed_hosts=("fanbox.cc",))
+ allowed_hosts=("fanbox.cc", "pixiv.pximg.net"))
if resource_provider == "f95zone":
if not _allowed_resource(url, ("attachments.f95zone.to",)):
raise FollowMediaUnavailable("F95 返回了不受信任的图片地址")
diff --git a/tests/test_follow_fanbox.py b/tests/test_follow_fanbox.py
index b93af7c36..43cc23fcd 100644
--- a/tests/test_follow_fanbox.py
+++ b/tests/test_follow_fanbox.py
@@ -56,6 +56,39 @@ class FanboxNormalizerTests(unittest.TestCase):
"https://downloads.fanbox.cc/source.zip",
])
+ def test_cover_image_joins_media_after_the_body(self):
+ cover = ("https://pixiv.pximg.net/c/1200x630_90_a2_g5/fanbox/public/images/"
+ "post/12565427/cover/1iVQFUYcuQvxD6oVZRBcy1Cm.jpeg")
+ content = normalize_fanbox_post({
+ "type": "image", "coverImageUrl": cover,
+ "body": {"images": [
+ {"id": "one", "originalUrl": "https://downloads.fanbox.cc/one.jpg",
+ "thumbnailUrl": "https://downloads.fanbox.cc/one-thumb.jpg"},
+ ]},
+ })
+
+ self.assertEqual([item["id"] for item in content.media_items], ["one", "cover"])
+ tail = content.media_items[-1]
+ self.assertEqual(tail["name"], "封面")
+ self.assertEqual(tail["url"], cover)
+ self.assertEqual(tail["thumb_url"], cover)
+ self.assertEqual(tail["media_kind"], "image")
+ self.assertEqual(content.image_count, 2)
+
+ def test_cover_image_is_skipped_when_absent_or_already_present(self):
+ cover = "https://pixiv.pximg.net/fanbox/public/images/post/1/cover/abc.jpeg"
+ again = normalize_fanbox_post({
+ "type": "image",
+ "body": {"images": [{"id": "one", "originalUrl": cover}]},
+ })
+ self.assertEqual([item["id"] for item in again.media_items], ["one"])
+ none = normalize_fanbox_post({
+ "type": "image",
+ "coverImageUrl": cover,
+ "body": {"images": [{"id": "one", "originalUrl": cover}]},
+ })
+ self.assertEqual([item["id"] for item in none.media_items], ["one"])
+
def test_image_post_exposes_a_switchable_gallery(self):
content = normalize_fanbox_post({"type": "image", "body": {"images": [
{"id": "one", "originalUrl": "https://downloads.fanbox.cc/one.jpg",