Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(metadata): 封面不收动图,也不收文件名写着别的作品号的图

longmeidao6b98b1519713
diff --git a/docs/SOURCING.md b/docs/SOURCING.md
index c4ffedd6e..11bf5e146 100644
--- a/docs/SOURCING.md
+++ b/docs/SOURCING.md
@@ -160,7 +160,10 @@ r18.dev 对 85 个 FC2 番号全空,AVBase 与 JavBus 对本地这批一律「
`image` 排后。转存者的命名没有统一——`4137487pl.jpg` 沿用 FC2 自己的 `pl`/`ps`,`FC2PPV-4030617.jpg`
与 `FC2PPV835964-2.jpg` 是他自己起的——按名字认的话 4 部里 3 部一张都取不到。`_s.jpg` 结尾的排除,
那是正文里标着 `Preview` 的多帧长条拼图。`FC2-PPV-4137487` 在 fc2cmadb 是 404、这里有,封面比官方
- 原图差一档,所以排在最后。`robots.txt` 全站放行。
+ 原图差一档,所以排在最后。`robots.txt` 全站放行。文件名不用来认,只用来否:名字里五到七位的
+ 独立数字段都不是本番号时,那是别的作品的图(`FC2-PPV-1512205` 与 `1931440` 都取到过 `2184960PL.gif`,
+ 本机共 10 张),`is_cross_product_cover` 按这条拦。那里存的 GIF 多是预览动画,`picture()` 一律
+ 不收,卡片封面只要静态图。
- 同一个商品站上常有好几条,不同转存者各发一次,文章号、番号写法、标题和图都各不相同,先后没有
质量含义。搜索命中的每一条都取:第一条那张未必还在(2026-09-22 实测 `1436028` 的 `/641159-` 是
404,`/800025-` 上有 1280×720)。走到这一档时前两处已经都说没有,多那一两页换的是这个番号有没有
diff --git a/src/peach/community_catalog.py b/src/peach/community_catalog.py
index 12748a9c1..c3fd9d97e 100644
--- a/src/peach/community_catalog.py
+++ b/src/peach/community_catalog.py
@@ -305,7 +305,11 @@ def fingerprint(image: Image.Image) -> int:
def picture(candidate: Candidate, data: bytes) -> Picture:
+ """下载到的一张图。动图不算封面:JavArchive 的图床给 FC2 存的常是 GIF 预览动画,
+ 落进 `.jpg` 之后卡片就一直在动。"""
with Image.open(io.BytesIO(data)) as image:
+ if getattr(image, "is_animated", False):
+ raise ValueError(f"{candidate.url} 是动图")
image.load()
return Picture(candidate, image.size, data, fingerprint(image))
@@ -316,26 +320,39 @@ def same_picture(one: Picture, other: Picture) -> bool:
and bin(one.fingerprint ^ other.fingerprint).count("1") <= HASH_DISTANCE)
-def _pictures(transport, urls: dict[str, Candidate], reference, *, deadline: float | None) -> list[Picture]:
- """官方小图(有的话)加上社区来源里下载得到、宽度够小图门槛的图。"""
+def _pictures(transport, urls: dict[str, Candidate], reference, *,
+ deadline: float | None) -> tuple[list[Picture], int]:
+ """官方小图(有的话)加上社区来源里下载得到、宽度够小图门槛的静态图;再报下载到了几张。
+
+ 下载到了却不能用(动图、太窄)和没下载到是两回事:前者再问一遍还是这几张。
+ """
pool = [picture(reference[0], reference[2])] if reference is not None else []
+ fetched = 0
for url, candidate in urls.items():
if reference is not None and url == reference[0].url:
continue
try:
- found = picture(candidate, _fetch(transport, url, referer=candidate.referer,
- limit=IMAGE_LIMIT, deadline=deadline))
- except (NotFound, Unavailable, SourcePaused, httpx.TransportError, OSError, ValueError,
- Image.DecompressionBombError):
+ data = _fetch(transport, url, referer=candidate.referer, limit=IMAGE_LIMIT, deadline=deadline)
+ except (NotFound, Unavailable, SourcePaused, httpx.TransportError, OSError):
+ continue
+ fetched += 1
+ try:
+ found = picture(candidate, data)
+ except (OSError, ValueError, Image.DecompressionBombError):
continue
if found.size[0] >= SMALL_MIN_WIDTH:
pool.append(found)
- return pool
+ return pool, fetched
+
+def _unverified(pool: list[Picture], usable: int,
+ fetched: int) -> tuple[Candidate, tuple[int, int], bytes, tuple[str, ...]]:
+ """没有两个图源对得上时的退路:图全出自一个图源就取最大那张,印证图源留空。
-def _unverified(pool: list[Picture], downloaded: bool) -> tuple[Candidate, tuple[int, int], bytes, tuple[str, ...]]:
- """没有两个图源对得上时的退路:图全出自一个图源就取最大那张,印证图源留空。"""
- if not downloaded:
+ `usable` 是池子里社区来源那几张,不算官方小图。"""
+ if not usable:
+ if fetched:
+ raise NotFound("社区来源给的封面都是动图或太小")
raise Unavailable("社区来源的封面下载失败")
origins = sorted({one.origin for one in pool})
if len(origins) > 1:
@@ -352,15 +369,16 @@ def verified_cover(transport, code: str, works: list[tuple[str, dict]], *,
`reference` 是官方渠道取到的小图,它也算一个图源:javdb 的大图和 DMM 的小图是同一张
时,大图就有了官方印证。没有第二个图源可比时按 `_unverified` 取图,末尾为空。
"""
- urls = {url: _candidate(url) for _source, payload in works for url in payload.get("cover_urls") or []}
+ urls = {url: _candidate(url) for _source, payload in works for url in payload.get("cover_urls") or []
+ if not is_cross_product_cover(code, url)}
if not urls:
raise NotFound("社区来源没有这部片的封面")
- pool = _pictures(transport, urls, reference, deadline=deadline)
+ pool, fetched = _pictures(transport, urls, reference, deadline=deadline)
best = None
for one in pool:
origins = {other.origin for other in pool if same_picture(one, other)}
if len(origins) >= 2 and (best is None or one.pixels > best[0].pixels):
best = (one, tuple(sorted(origins)))
if best is None:
- return _unverified(pool, downloaded=len(pool) > (reference is not None))
+ return _unverified(pool, len(pool) - (reference is not None), fetched)
return best[0].candidate, best[0].size, best[0].data, best[1]
diff --git a/src/peach/jav_cover_fetch.py b/src/peach/jav_cover_fetch.py
index 14187255c..33fb03136 100644
--- a/src/peach/jav_cover_fetch.py
+++ b/src/peach/jav_cover_fetch.py
@@ -450,6 +450,22 @@ _DMM_CID = re.compile(
#: 番号与 content_id 各自末尾那段数字。content_id 可能带 DVD 版尾缀(`49ha102r`)。
_CODE_NUMBER = re.compile(r"(\d+)\D*$")
_CID_NUMBER = re.compile(r"(\d+)[a-z]*$")
+#: 图床文件名里的作品号:五到七位的独立数字段。`pl1654160016.07k.gif` 那种十位的是
+#: 上传时间戳,不算作品号。
+_FILE_NUMBER = re.compile(r"(?<!\d)\d{5,7}(?!\d)")
+
+
+def _fc2_names_another_work(code: str, url: str) -> bool:
+ """FC2 番号的图,文件名写着别的作品号。
+
+ JavArchive 的图床 img.javstore.net 按作品号起文件名(`FC2PPV-2543627.gif`、
+ `1083921pl.jpg`)。页面上挂着相关作品时,取回的会是另一部的图:`FC2-PPV-1512205`
+ 与 `FC2-PPV-1931440` 都拿到了 `2184960PL.gif`,本机同样对不上的共 10 张。
+ """
+ key = normalise_code_key(code)
+ wanted = _CODE_NUMBER.search(key) if key.startswith("FC2") else None
+ numbers = {int(one) for one in _FILE_NUMBER.findall(urlparse(url or "").path.rsplit("/", 1)[-1])}
+ return bool(wanted and numbers) and int(wanted.group(1)) not in numbers
def is_cross_product_cover(code: str, url: str) -> bool:
@@ -464,6 +480,8 @@ def is_cross_product_cover(code: str, url: str) -> bool:
再也没有机会被重探。判据落在 URL 上而不是抓取路径上,正是因为下一个引入
错图的来源不会用同一个函数。
"""
+ if _fc2_names_another_work(code, url):
+ return True
stem = code_letter_stem(code)
match = _DMM_CID.search(urlparse(url or "").path)
if not stem or match is None:
diff --git a/tests/test_community_catalog.py b/tests/test_community_catalog.py
index f694963b2..d77ad3f37 100644
--- a/tests/test_community_catalog.py
+++ b/tests/test_community_catalog.py
@@ -229,6 +229,23 @@ class VerifiedCoverTests(unittest.TestCase):
with self.assertRaises(NotFound):
verified_cover(serve({}), "ORETD-615", [("javdb", {"cover_urls": []})])
+ def test_an_animated_picture_or_another_works_picture_is_never_the_cover(self):
+ """JavArchive 的图床给 FC2 存 GIF 预览动画,页面上还挂着别的作品的图。"""
+ own_gif = "https://img.javstore.net/images/2022/01/03/FC2PPV-2543627.gif"
+ other = "https://img.javstore.net/images/2024/01/15/2184960PL.jpg"
+ frames = [Image.open(io.BytesIO(gradient(500, 282, rising))) for rising in (True, False)]
+ buffer = io.BytesIO()
+ frames[0].save(buffer, format="GIF", save_all=True, append_images=frames[1:])
+ works = [("javarchive", {"cover_urls": [own_gif, other]})]
+ pages = {own_gif: buffer.getvalue(), other: gradient(800, 450)}
+ with self.assertRaisesRegex(NotFound, "动图或太小"):
+ verified_cover(serve(pages), "FC2-PPV-2543627", works)
+ still = "https://img.javstore.net/images/2022/01/03/fc2ppv-2543627.jpg"
+ pages[still] = gradient(800, 450)
+ works = [("javarchive", {"cover_urls": [own_gif, other, still]})]
+ candidate, _size, _data, _origins = verified_cover(serve(pages), "FC2-PPV-2543627", works)
+ self.assertEqual(candidate.url, still)
+
class SourceChoiceTests(unittest.TestCase):
def test_an_fc2_product_number_only_goes_to_javdb(self):
diff --git a/tests/test_jav_covers.py b/tests/test_jav_covers.py
index caec5423e..cbc516097 100644
--- a/tests/test_jav_covers.py
+++ b/tests/test_jav_covers.py
@@ -433,6 +433,19 @@ class CrossProductCoverTests(unittest.TestCase):
self.assertFalse(covers.is_cross_product_cover(
code, f"https://pics.dmm.co.jp/mono/movie/{cid}/{cid}pl.jpg"))
+ def test_an_fc2_image_named_after_another_work_is_cross_product(self):
+ # JavArchive 图床的文件名就是作品号;十位的 `1654160016` 是上传时间戳,说明不了归属。
+ for url in ("https://img.javstore.net/images/2024/01/15/2184960PL.gif",
+ "http://img2.javstore.net/images/2026/04/27/4536228pl.jpg"):
+ with self.subTest(url=url):
+ self.assertTrue(covers.is_cross_product_cover("FC2-PPV-1512205", url))
+ for code, url in (
+ ("FC2-PPV-2653914", "https://img.javstore.net/images/2022/02/14/FC2PPV-2653914-1-1.gif"),
+ ("FC2-PPV-1083921", "https://img.javstore.net/images/2023/12/24/1083921pl.jpg"),
+ ("FC2-PPV-2935152", "https://img.javstore.net/images/2022/07/31/pl1654160016.07k.gif")):
+ with self.subTest(url=url):
+ self.assertFalse(covers.is_cross_product_cover(code, url))
+
def test_a_carried_wrong_cover_loses_to_the_right_one(self):
wrong = "https://pics.dmm.co.jp/mono/movie/adult/118sng021/118sng021pl.jpg"
right = "https://image.mgstage.com/images/luxutv/259luxu/1475/pb_e_259luxu-1475.jpg"