Skip to content

armgit / peach

Public Imported from GitHub
0

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

feat(follow): fanbox 卡面封面优先、Poll 帖剔首位图表、媒体加载失败给说明

longmeidaoc39be626892e
diff --git a/src/peach/web_follow.py b/src/peach/web_follow.py
index e1682cff3..f08c49da3 100644
--- a/src/peach/web_follow.py
+++ b/src/peach/web_follow.py
@@ -662,6 +662,18 @@ def _raw_media_items(item) -> list:
return raw
+#: 个别作者把非作品图固定贴在正文首位,按「作者 + 标题」直接不投影:
+#: `lazyprocrast` 标题带 Poll 的帖子,第一张正文图必是投票结果图表。
+_FANBOX_POLL_CHART_AUTHOR = "lazyprocrast"
+_FANBOX_POLL_CHART_TITLE_RE = re.compile(r"\bpoll\b", re.IGNORECASE)
+
+
+def _skips_poll_chart(item) -> bool:
+ return (item.provider == "fanbox"
+ and str(item.ref or "") == _FANBOX_POLL_CHART_AUTHOR
+ and _FANBOX_POLL_CHART_TITLE_RE.search(str(item.title or "")) is not None)
+
+
def _media_projection(item) -> tuple[list[dict], list[dict]]:
"""把 media_items 投影成浏览器字段,按用户隐藏的键拆成(可见,已隐藏)两份。
@@ -670,6 +682,7 @@ def _media_projection(item) -> tuple[list[dict], list[dict]]:
"""
hidden = frozenset(item.hidden_media or ())
visible, concealed = [], []
+ skip_poll_chart = _skips_poll_chart(item)
for index, media in enumerate(_raw_media_items(item)):
if not isinstance(media, dict):
continue
@@ -678,6 +691,8 @@ def _media_projection(item) -> tuple[list[dict], list[dict]]:
kind = str(media.get("media_kind") or "")
if kind not in {"video", "image"}:
continue
+ if skip_poll_chart and index == 0 and kind == "image":
+ continue
thumb = str(media.get("thumb_url") or "")
projected = {
"index": index,
@@ -710,7 +725,7 @@ def _thumb_url(item) -> str | None:
if item.provider == "f95zone" and f95_discussion_image(item.thumb_url):
return next((media["thumb_url"] for media in _media_items(item) if media["thumb_url"]), None)
if item.provider == "fanbox":
- thumb = display_thumb_url(item)
+ thumb = _fanbox_card_thumb(item) or display_thumb_url(item)
media_items = item.metadata.get("media_items") or []
videos = [media for media in media_items if isinstance(media, dict)
and media.get("media_kind") == "video"
@@ -718,6 +733,8 @@ def _thumb_url(item) -> str | None:
if videos and (not thumb or thumb in {media.get("url") for media in videos}
or urllib.parse.urlsplit(thumb).path.lower().endswith((".mp4", ".webm", ".mov", ".m4v"))):
return f"/follow-cover?id={item.id}"
+ if thumb:
+ return thumb
if item.provider == "rule34paheal" and item.media_url:
kind = _media_kind(item)
if kind == "image":
@@ -727,6 +744,30 @@ def _thumb_url(item) -> str | None:
return _unhide_thumb(item, display_thumb_url(item))
+def _fanbox_card_thumb(item) -> str | None:
+ """fanbox 的卡面:作者挑的封面优先,其次正文里第一张没被隐藏的图。
+
+ 封面是作者给这篇选的展示图;它收在媒体清单里(`id=cover`),从那里认,
+ 未补齐正文的行没有清单,退回存的缩略图(列表阶段就是封面)。
+ """
+ hidden = frozenset(item.hidden_media or ())
+ fallback = None
+ for index, media in enumerate(_raw_media_items(item)):
+ if not isinstance(media, dict) or media.get("media_kind") != "image":
+ continue
+ if _media_key(media) in hidden:
+ continue
+ if index == 0 and _skips_poll_chart(item):
+ continue
+ thumb = str(media.get("thumb_url") or "")
+ if not thumb.startswith("https://"):
+ continue
+ if media.get("id") == "cover":
+ return thumb
+ fallback = fallback or thumb
+ return fallback
+
+
def _unhide_thumb(item, thumb: str | None) -> str | None:
"""缩略图落在被隐藏的媒体上时,改用第一张可见媒体的缩略图。
diff --git a/tests/test_follow_web.py b/tests/test_follow_web.py
index daa2d9104..a9b7d92bd 100644
--- a/tests/test_follow_web.py
+++ b/tests/test_follow_web.py
@@ -620,11 +620,79 @@ class FollowContractTests(unittest.TestCase):
self.assertEqual(items[key]["thumb_url"], f"/follow-cover?id={items[key]['id']}")
self.assertEqual(items["3"]["thumb_url"], image)
+ def test_fanbox_card_thumb_prefers_the_author_cover(self):
+ chart = "https://downloads.fanbox.cc/images/post/1/w/1200/chart.jpeg"
+ art = "https://downloads.fanbox.cc/images/post/1/w/1200/art.jpeg"
+ cover = ("https://pixiv.pximg.net/c/1200x630_90_a2_g5/fanbox/public/"
+ "images/post/1/cover/abc.jpeg")
+ self._seed(candidates=(FollowCandidate(
+ provider="fanbox", external_id="1", title="Post",
+ url="https://lazyprocrast.fanbox.cc/posts/1", thumb_url=chart,
+ extra={"media_items": [
+ {"id": "chart", "media_kind": "image", "resource_provider": "fanbox",
+ "url": "https://downloads.fanbox.cc/images/post/1/chart.png",
+ "thumb_url": chart},
+ {"id": "art", "media_kind": "image", "resource_provider": "fanbox",
+ "url": "https://downloads.fanbox.cc/images/post/1/art.png",
+ "thumb_url": art},
+ {"id": "cover", "media_kind": "image", "resource_provider": "fanbox",
+ "url": cover, "thumb_url": cover},
+ ]}),), provider="fanbox", ref="lazyprocrast")
+ item = self._get()["groups"][0]["primary"]
+ self.assertEqual(item["thumb_url"], cover)
+
+ self._post("/api/follow/media/hide", {"item": item["id"], "media": 2, "hidden": True})
+ item = self._get()["groups"][0]["primary"]
+ # 封面被隐藏后卡面退回正文首图。
+ self.assertEqual(item["thumb_url"], chart)
+
+ def test_poll_post_drops_the_leading_chart_for_its_author(self):
+ chart = "https://downloads.fanbox.cc/images/post/1/w/1200/chart.jpeg"
+ art = "https://downloads.fanbox.cc/images/post/1/w/1200/art.jpeg"
+ cover = ("https://pixiv.pximg.net/c/1200x630_90_a2_g5/fanbox/public/"
+ "images/post/1/cover/abc.jpeg")
+ media = [
+ {"id": "chart", "media_kind": "image", "resource_provider": "fanbox",
+ "url": "https://downloads.fanbox.cc/images/post/1/chart.png",
+ "thumb_url": chart},
+ {"id": "art", "media_kind": "image", "resource_provider": "fanbox",
+ "url": "https://downloads.fanbox.cc/images/post/1/art.png",
+ "thumb_url": art},
+ {"id": "cover", "media_kind": "image", "resource_provider": "fanbox",
+ "url": cover, "thumb_url": cover},
+ ]
+ self._seed(candidates=(FollowCandidate(
+ provider="fanbox", external_id="1", title="Poll Results + Scheduling Talk",
+ url="https://lazyprocrast.fanbox.cc/posts/1", thumb_url=chart,
+ extra={"media_items": media}),), provider="fanbox", ref="lazyprocrast")
+ item = self._get()["groups"][0]["primary"]
+ # 首位图表不投影;序号保持原始下标,卡面用作者封面。
+ self.assertEqual([media_["index"] for media_ in item["media_items"]], [1, 2])
+ self.assertEqual(item["thumb_url"], cover)
+
+ self._post("/api/follow/media/hide", {"item": item["id"], "media": 2, "hidden": True})
+ item = self._get()["groups"][0]["primary"]
+ # 封面被隐藏后卡面跳过首位的图表,落在正文第二张。
+ self.assertEqual(item["thumb_url"], art)
+
+ def test_poll_leading_chart_stays_for_other_creators(self):
+ chart = "https://downloads.fanbox.cc/images/post/1/w/1200/chart.jpeg"
+ self._seed(candidates=(FollowCandidate(
+ provider="fanbox", external_id="2", title="Poll Results",
+ url="https://someoneelse.fanbox.cc/posts/2", thumb_url=chart,
+ extra={"media_items": [
+ {"id": "chart", "media_kind": "image", "resource_provider": "fanbox",
+ "url": "https://downloads.fanbox.cc/images/post/1/chart.png",
+ "thumb_url": chart},
+ ]}),), provider="fanbox", ref="someoneelse")
+ item = self._get()["groups"][0]["primary"]
+ self.assertEqual([media_["index"] for media_ in item["media_items"]], [0])
+
def test_hidden_media_leaves_the_feed_and_the_card_thumb(self):
chart = "https://downloads.fanbox.cc/images/post/1/w/1200/chart.jpeg"
art = "https://downloads.fanbox.cc/images/post/1/w/1200/art.jpeg"
self._seed(candidates=(FollowCandidate(
- provider="fanbox", external_id="12565427", title="Poll Results",
+ provider="fanbox", external_id="12565427", title="Public Release Notes",
url="https://lazyprocrast.fanbox.cc/posts/12565427", thumb_url=chart,
extra={"media_items": [
{"id": "chart", "media_kind": "image", "resource_provider": "fanbox",
@@ -656,7 +724,7 @@ class FollowContractTests(unittest.TestCase):
def test_media_hidden_state_survives_a_refetch(self):
candidate = FollowCandidate(
- provider="fanbox", external_id="12565427", title="Poll Results",
+ provider="fanbox", external_id="12565427", title="Public Release Notes",
url="https://lazyprocrast.fanbox.cc/posts/12565427",
extra={"media_items": [
{"id": "chart", "media_kind": "image", "resource_provider": "fanbox",
diff --git a/web/app.js b/web/app.js
index e0d922ee0..254039149 100644
--- a/web/app.js
+++ b/web/app.js
@@ -5752,6 +5752,7 @@ async function openFollowDetail(id,push=true,mediaIndex=null,preserveReturn=fals
<div class="smeta mono"><span>${followWhen(item)}</span>${realDuration(item.duration)?`<span>${fmtDur(item.duration)}</span>`:''}${badges?`<span class="fbadges">${badges}</span>`:''}</div>
${item.summary?`<p class="followdetailsummary">${esc(item.summary)}</p>`:''}
${mediaIssue?`<p class="fnote followmediaissue">${esc(mediaIssue)}</p>`:''}
+ <p class="fnote followmediaissue" data-media-load-issue hidden>媒体没有取回来:上游这一次没给出内容,多半是站点在限流——过一阵再打开。</p>
${followResourceLinks(item)}
<div class="fb followdetailactions">
<button class="later" data-follow-detail-save aria-label="${item.status==='saved'?'已保存':'保存到账本'}" title="${item.status==='saved'?'已保存':'保存到账本'}"${item.status==='saved'?' disabled':''}>${item.status==='saved'?icon('check'):icon('bookmark-plus')}</button>
@@ -5877,6 +5878,11 @@ async function openFollowDetail(id,push=true,mediaIndex=null,preserveReturn=fals
write(button,'/api/follow/media/hide',{item:item.id,media:index,hidden:false},()=>{
reopenAfterMediaChange(index)},{message:'已恢复显示'});
});
+ // 上游没给出媒体时,代理只会回一个不含缘由的失败;界面上把「这次没取到、
+ // 多半是限流」说在侧栏,别让人对着一块空画布猜。
+ const loadIssue=$('#stage').querySelector('[data-media-load-issue]');
+ $('#stage').querySelectorAll('.followdetailmedia img,.followdetailmedia video').forEach(el=>
+ el.addEventListener('error',()=>{if(loadIssue)loadIssue.hidden=false}));
alignFollowImageControls();
// 滚到舞台本身,不是页面头部——就近展开的意义就在于视线不被拽走。
// 复用首页那套 sticky 偏移,标题不会被吸顶的筛选条盖住。