Skip to content

armgit / peach

Public Imported from GitHub
0

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

feat(follow): fanbox 媒体带固有宽高,存量行重探补齐

longmeidao82f7dcdd9893
diff --git a/src/peach/fanbox.py b/src/peach/fanbox.py
index 674bf9463..f3786c4d7 100644
--- a/src/peach/fanbox.py
+++ b/src/peach/fanbox.py
@@ -33,6 +33,8 @@ class FanboxContent:
_SUPPORTED_TYPES = frozenset({"image", "text", "file", "article", "video", "entry"})
_URL_RE = re.compile(r"https?://[^\s\"'<>)\]]+", re.IGNORECASE)
_IMAGE_SUFFIXES = frozenset({".avif", ".bmp", ".gif", ".jpeg", ".jpg", ".png", ".webp"})
+#: 封面地址里的裁切尺寸:`.../c/1200x630_90_a2_g5/...`。
+_COVER_DIMS_RE = re.compile(r"/c/(\d+)x(\d+)")
_VIDEO_SUFFIXES = frozenset({".m4v", ".mkv", ".mov", ".mp4", ".webm"})
@@ -105,12 +107,14 @@ def _add_cover(post: Mapping[str, object], media: list[dict],
"""封面追加到媒体清单末尾。
封面也是这篇的图,收进媒体清单;排在正文之后,卡片缩略图仍然落在正文首图上。
- 站点给的是 1200x630 的展示裁切版,不是原画质。
+ 站点给的是 1200x630 的展示裁切版,不是原画质;裁切尺寸写在地址里,解析出来
+ 供界面占位。
"""
cover = _https_url(post.get("coverImageUrl"))
if not cover or cover in media_urls:
return
media_urls.add(cover)
+ matched = _COVER_DIMS_RE.search(cover)
media.append({
"id": "cover",
"name": "封面",
@@ -118,6 +122,8 @@ def _add_cover(post: Mapping[str, object], media: list[dict],
"thumb_url": cover,
"media_kind": "image",
"size": None,
+ "width": int(matched.group(1)) if matched else None,
+ "height": int(matched.group(2)) if matched else None,
"resource_provider": "fanbox",
})
@@ -168,6 +174,9 @@ def normalize_fanbox_post(post: Mapping[str, object]) -> FanboxContent:
"thumb_url": (thumb or url) if kind == "image" else thumb,
"media_kind": kind,
"size": node.get("size"),
+ # imageMap 自带的固有宽高跟着走:界面靠它在图片落地前占出正确的比例。
+ "width": node.get("width"),
+ "height": node.get("height"),
"resource_provider": "fanbox",
})
diff --git a/src/peach/follow_sources.py b/src/peach/follow_sources.py
index d43fff38f..6dec11e63 100644
--- a/src/peach/follow_sources.py
+++ b/src/peach/follow_sources.py
@@ -2139,10 +2139,10 @@ class FanboxConnector(_BaseConnector):
provider = "fanbox"
#: 2026-08-27 实测公开接口单页 10 条,整页都能补上。
DEFAULT_ENRICH_BUDGET = 10
- #: 详情补齐一轮的标记:补全阶段在 extra 里写 `cover_harvested`。拿新键当判据,
- #: 按 `post_type` 判成已补齐的旧行缺这一个键,下一轮检查会各重探一次详情——
- #: 封面是这次才收进媒体清单的,旧行只有重探才能拿到。
- ENRICHED_MARK = "cover_harvested"
+ #: 详情补齐一轮的标记:补全阶段在 extra 里写 `media_dims`。拿新键当判据,
+ #: 缺键的行下一轮检查会各重探一次详情。媒体清单现在是带封面和固有宽高的
+ #: 完整形状,按旧键判成已补齐的行只有重探才能拿到这个形状。
+ ENRICHED_MARK = "media_dims"
_CREATOR_RE = re.compile(r"^[A-Za-z0-9_-]{1,64}$")
@classmethod
@@ -2298,7 +2298,7 @@ class FanboxConnector(_BaseConnector):
summary=detail["summary"] or candidate.summary,
extra={**candidate.extra, "links": links, "media_items": media_items,
"media_error": None,
- "cover_harvested": True,
+ "media_dims": True,
"post_type": detail.get("post_type"),
"image_count": detail.get("image_count", 0),
"video_count": detail.get("video_count", 0),
diff --git a/src/peach/follow_store.py b/src/peach/follow_store.py
index a128722a9..c9d808aaa 100644
--- a/src/peach/follow_store.py
+++ b/src/peach/follow_store.py
@@ -145,7 +145,7 @@ _ENRICHED_PREDICATES = {
"published_at": "published_at IS NOT NULL AND published_at<>''",
"tag_types": "json_extract(metadata_json,'$.tag_types') IS NOT NULL",
"post_type": "json_extract(metadata_json,'$.post_type') IS NOT NULL",
- "cover_harvested": "json_extract(metadata_json,'$.cover_harvested') IS NOT NULL",
+ "media_dims": "json_extract(metadata_json,'$.media_dims') IS NOT NULL",
}
diff --git a/tests/test_follow_fanbox.py b/tests/test_follow_fanbox.py
index 43cc23fcd..203820e98 100644
--- a/tests/test_follow_fanbox.py
+++ b/tests/test_follow_fanbox.py
@@ -73,8 +73,21 @@ class FanboxNormalizerTests(unittest.TestCase):
self.assertEqual(tail["url"], cover)
self.assertEqual(tail["thumb_url"], cover)
self.assertEqual(tail["media_kind"], "image")
+ # 裁切尺寸写在封面地址里,解析出来给界面占位。
+ self.assertEqual((tail["width"], tail["height"]), (1200, 630))
self.assertEqual(content.image_count, 2)
+ def test_body_images_carry_their_intrinsic_dimensions(self):
+ content = normalize_fanbox_post({"type": "image", "body": {"images": [
+ {"id": "one", "originalUrl": "https://downloads.fanbox.cc/one.png",
+ "thumbnailUrl": "https://downloads.fanbox.cc/w/1200/one.jpeg",
+ "width": 1920, "height": 1080},
+ {"id": "two", "originalUrl": "https://downloads.fanbox.cc/two.png"},
+ ]}})
+ self.assertEqual((content.media_items[0]["width"],
+ content.media_items[0]["height"]), (1920, 1080))
+ self.assertIsNone(content.media_items[1]["width"])
+
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({
diff --git a/tests/test_follow_sources.py b/tests/test_follow_sources.py
index 563f65ee0..d68d0fd8f 100644
--- a/tests/test_follow_sources.py
+++ b/tests/test_follow_sources.py
@@ -179,7 +179,8 @@ FANBOX_DETAIL_JSON = json.dumps({"body": {"post": {"type": "article", "body": {
],
"imageMap": {
"one": {"originalUrl": "https://downloads.fanbox.cc/one.jpg",
- "thumbnailUrl": "https://downloads.fanbox.cc/one-thumb.jpg"},
+ "thumbnailUrl": "https://downloads.fanbox.cc/one-thumb.jpg",
+ "width": 1920, "height": 1080},
"two": {"originalUrl": "https://downloads.fanbox.cc/two.jpg",
"thumbnailUrl": "https://downloads.fanbox.cc/two-thumb.jpg"},
},
@@ -308,10 +309,12 @@ class OfficialConnectorTests(unittest.TestCase):
self.assertEqual(result.candidates[0].group_hint, "fanbox:12489354")
self.assertEqual(result.candidates[0].published_at, "2026-08-26T14:34:51Z")
self.assertEqual(len(result.candidates[0].extra["media_items"]), 2)
+ self.assertEqual(result.candidates[0].extra["media_items"][0]["width"], 1920)
+ self.assertEqual(result.candidates[0].extra["media_items"][1]["width"], None)
self.assertEqual(result.candidates[0].extra["links"],
["https://gofile.io/d/OS2Qz9"])
self.assertEqual(result.candidates[0].extra["post_type"], "article")
- self.assertTrue(result.candidates[0].extra["cover_harvested"])
+ self.assertTrue(result.candidates[0].extra["media_dims"])
self.assertEqual(result.candidates[0].extra["image_count"], 2)
self.assertEqual(result.candidates[0].extra["video_count"], 0)
self.assertEqual(result.candidates[0].extra["file_count"], 0)