Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(follow): rule34xxx 发布时间取详情页的上传时间

longmeidao8564cd5fe618
diff --git a/scripts/backfill_rule34_tag_types.py b/scripts/backfill_rule34_tag_types.py
index 9cd93f361..d657234f3 100644
--- a/scripts/backfill_rule34_tag_types.py
+++ b/scripts/backfill_rule34_tag_types.py
@@ -13,7 +13,7 @@ copyright/metadata)只在帖子页的 `#tag-sidebar` 上。连接器早就
- 详情标签全是中性色。按类型着色和按 rule34 分类排序都要有类型才成立。
常规「检查更新」只看第一页,补不到历史条目,所以要这一趟。抓取逻辑不重写,
-直接复用连接器的 `_detail_tag_types`:判据只能有一处。
+直接复用连接器的 `_detail`:判据只能有一处。
默认 dry-run。`--apply` 必须同时给 `--backup`,与本仓库其它真实写入脚本一致。
可反复运行:已经有类型的条目直接跳过,中断后接着跑就是。
@@ -43,7 +43,7 @@ FIELDS = ("item_id", "external_id", "url", "result", "tag_types", "note")
#: 同一主机的最小请求间隔。rule34.xxx 自己公布的口径是每 60 秒 60 次,也就是
#: 每次至少 1 秒;留一点余量。首轮按 0.35 秒跑过一次:400 条里 372 条「帖子页
-#: 没有解析出类型」——不是这些帖子没有类型,是被限流挡了,`_detail_tag_types`
+#: 没有解析出类型」——不是这些帖子没有类型,是被限流挡了,`_detail`
#: 把非 200 吞成了空字典。速度换来的全是白跑的请求。
DEFAULT_DELAY = 1.1
#: 连续这么多条取不到就认定是被限流,而不是这一段恰好都没类型。
@@ -140,7 +140,7 @@ def run(args: argparse.Namespace) -> int:
"result": "跳过", "tag_types": "", "note": "external_id 不是帖子号"})
continue
try:
- tag_types = connector._detail_tag_types(post_id)
+ tag_types = connector._detail(post_id).get("tag_types") or {}
except (FollowSourceError, OSError) as error:
log.append({"item_id": row["id"], "external_id": post_id, "url": row["url"],
"result": "未取得", "tag_types": "",
diff --git a/src/peach/follow_sources.py b/src/peach/follow_sources.py
index 4ab6b7ddb..c74329fc9 100644
--- a/src/peach/follow_sources.py
+++ b/src/peach/follow_sources.py
@@ -1402,12 +1402,14 @@ class Rule34XxxConnector(_BaseConnector):
raw_body=response.body, **common)
def _enrich_one(self, candidate: FollowCandidate) -> FollowCandidate:
- tag_types = self._detail_tag_types(str(candidate.external_id))
+ detail = self._detail(str(candidate.external_id))
+ tag_types = detail.get("tag_types")
if not tag_types:
# 详情页这次没给出分类。保持 partial:上一轮取到的分类还在库里,
# 覆盖成空会让「这条没有分类」和「这次没问到」在数据里长得一样。
return candidate
return replace(candidate, partial=False,
+ published_at=detail.get("published_at") or candidate.published_at,
extra={**candidate.extra, "tag_types": tag_types})
#: 自动补全项的形状:`ria-neearts (248)`,括号里是该标签下的帖子数。
@@ -1447,7 +1449,7 @@ class Rule34XxxConnector(_BaseConnector):
return tuple(rows)
#: dapi 的 tag 接口用数字表示分类。名字取站方自己在详情页 `#tag-sidebar` 上用的
- #: 那套(`_detail_tag_types` 读的就是它),两条路认出来的类型才是同一个词。
+ #: 那套(`_detail` 读的就是它),两条路认出来的类型才是同一个词。
_TAG_TYPE_NAMES = {0: "general", 1: "artist", 3: "copyright",
4: "character", 5: "metadata"}
#: dapi 的 tag 接口只回 XML,`json=1` 实测被忽略。
@@ -1492,14 +1494,16 @@ class Rule34XxxConnector(_BaseConnector):
})
#: 详情页取不到时的退避节奏,与外网退避规则同一套。
_DETAIL_RETRY_DELAYS = (1.5, 4.0, 9.0)
+ #: 详情页 `#stats` 里的上传时间:`Posted: 2026-09-11 00:48:20 by mhzw666`。
+ _POSTED_RE = re.compile(r"Posted:\s*(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})")
- def _detail_tag_types(self, post_id: str) -> dict[str, str]:
- """Read rule34.xxx's own tag taxonomy from the public post page.
+ def _detail(self, post_id: str) -> dict[str, object]:
+ """帖子详情页上才有的两样:标签分类和上传时间。取不到就是空字典。
- The posts DAPI only returns one flat tag string. The post page is the
- authoritative surface that marks each tag as general, artist,
- copyright, character or metadata. If that optional enrichment is
- unavailable we keep the post, but we do not guess types from words.
+ dapi 只给一串扁平标签,分类要读详情页 `#tag-sidebar`,取不到就不给,不按词形
+ 猜。dapi 也不给上传时间:它的 `change` 是最后一次修改,实测 17361475 在
+ 2026-05-01 上传、09-14 被改过标签,`change` 就是 09-14。详情页 `#stats` 的
+ Posted 才是上传时间;没被改过的帖子两者逐秒相同,所以它和 `change` 一样是 UTC。
"""
url = ("https://rule34.xxx/index.php?page=post&s=view&id="
f"{urllib.parse.quote(post_id)}")
@@ -1538,7 +1542,10 @@ class Rule34XxxConnector(_BaseConnector):
name = html.unescape(str((query.get("tags") or [""])[0])).strip()
if name:
result[name] = tag_type
- return result
+ stats = soup.select_one("#stats")
+ posted = self._POSTED_RE.search(stats.get_text(" ") if stats is not None else "")
+ return {"tag_types": result,
+ "published_at": _iso_from_text(posted.group(1)) if posted else None}
def _candidate(self, post: dict, tag: str) -> FollowCandidate:
post_id = str(post.get("id") or "")
@@ -1580,6 +1587,7 @@ class Rule34XxxConnector(_BaseConnector):
# 对图片则是站点选定的样图。gallery-dl 的 booru 抽取器也把这三层
# 作为可配置回退链,不能把最小 preview 固定成 Peach 封面。
thumb_url=str(post.get("sample_url") or post.get("preview_url") or "") or None,
+ # `change` 是最后修改时间,只在详情页取不到时占位;上传时间由 `_detail` 给。
published_at=_iso_from_epoch(post.get("change")),
group_hint=hint,
title_is_name=title_is_name,
diff --git a/tests/test_follow_sources.py b/tests/test_follow_sources.py
index d86ea2512..b6e3d4b37 100644
--- a/tests/test_follow_sources.py
+++ b/tests/test_follow_sources.py
@@ -1375,6 +1375,26 @@ class Rule34XxxConnectorTests(unittest.TestCase):
self.assertEqual(types["lazyprocrastinator"], "artist")
self.assertEqual(types["reverse_cowgirl_position"], "general")
self.assertNotIn("api_key", seen[1].url)
+ # 详情页没有 `#stats` 时,dapi 的 `change` 留作占位。
+ self.assertEqual(result.candidates[0].published_at, "2026-08-23T00:36:13Z")
+
+ def test_upload_time_comes_from_the_post_page_not_the_last_edit(self):
+ """dapi 的 `change` 是最后修改时间:17361475 五月上传、九月改过标签就显示成九月。"""
+ detail = RULE34XXX_DETAIL_HTML.replace(b"</ul>", (
+ b'</ul><div id="stats"><ul><li>Id: 18534395</li>'
+ b"<li>Posted: 2026-05-01 16:32:21<br> by <a>rekin3d</a></li></ul></div>"))
+
+ def transport(request, timeout, max_bytes):
+ body = RULE34XXX_JSON if request.url.startswith("https://api.rule34.xxx/") \
+ else detail
+ return HttpResponse(200, {}, body)
+
+ result = Rule34XxxConnector(
+ transport=transport, max_items=1,
+ credential=Credential("rule34xxx", {"user_id": "42", "api_key": "sekret"}),
+ ).fetch("lazyprocrastinator")
+ self.assertEqual(result.candidates[0].published_at, "2026-05-01T16:32:21Z")
+ self.assertFalse(result.candidates[0].partial)
def test_a_throttled_detail_page_is_retried_not_read_as_no_types(self):
"""站方公布的是每 60 秒 60 次,而列表页一页 24 条、每条都要单独打一次详情页。
diff --git a/tests/test_scripts.py b/tests/test_scripts.py
index 55dcc4446..52cda34e7 100644
--- a/tests/test_scripts.py
+++ b/tests/test_scripts.py
@@ -262,7 +262,7 @@ class OperationalScriptTests(unittest.TestCase):
"""rule34xxx 的标签类型只在帖子页上,2152 条里当时只有 40 条带着它。
常规检查只看第一页,补不到历史条目。抓取判据不重写,直接复用连接器的
- `_detail_tag_types`;备份先做、分批提交,中断一次不至于白跑五十分钟。
+ `_detail`;备份先做、分批提交,中断一次不至于白跑五十分钟。
"""
backfill = load_script("backfill_rule34_tag_types")
# 连接与备份走共享的 `open_for_write`/`open_readonly`,WAL 正确性由