Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(follow): 卡片标签改留玩法而不是题材;缓存分两档

longmeidaoc8ad864b931e
diff --git a/src/peach/api.py b/src/peach/api.py
index 30463c83b..6dec81b9d 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -128,11 +128,12 @@ def _offline_response(exc: MediaOffline) -> JSONResponse:
return response
-#: 生成物与外部头像的缓存时长。这些端点按 asset / follow_item id 取图,内容换了
-#: id 也就换了(封面重生成会写新文件、头像换了会解析到新 URL),所以一天太短——
-#: 用户实测「基本不可能变动」。不加 immutable:那会让浏览器连刷新都不再回源,
-#: 万一真要换图就只能等过期。
-MEDIA_CACHE_SECONDS = 30 * 24 * 3600
+#: 生成物的缓存时长。这些端点按 asset / follow_item id 取图:内容换了 id 也就换了
+#: (封面重生成写的是新文件),所以一年也不嫌长。
+#: 不加 immutable——那会让浏览器连刷新都不再回源,真要换图就只能干等过期。
+MEDIA_CACHE_SECONDS = 365 * 24 * 3600
+#: 头像单独短一档:id 不变但人会换头像,作者换得还挺勤。
+AVATAR_CACHE_SECONDS = 30 * 24 * 3600
def create_app(
@@ -669,7 +670,7 @@ def create_app(
except PreviewUnavailable:
return JSONResponse({"error": "unavailable"}, status_code=404)
response = FileResponse(path, media_type="image/jpeg")
- response.headers["Cache-Control"] = f"public, max-age={MEDIA_CACHE_SECONDS}"
+ response.headers["Cache-Control"] = f"public, max-age={AVATAR_CACHE_SECONDS}"
return response
@app.api_route("/follow-avatar", methods=["GET", "HEAD"])
@@ -680,7 +681,7 @@ def create_app(
except (OSError, FollowSourceError):
return JSONResponse({"error": "unavailable"}, status_code=404)
response = RedirectResponse(target, status_code=307)
- response.headers["Cache-Control"] = f"private, max-age={MEDIA_CACHE_SECONDS}"
+ response.headers["Cache-Control"] = f"private, max-age={AVATAR_CACHE_SECONDS}"
return response
@app.api_route("/follow-stream", methods=["GET", "HEAD"])
diff --git a/tests/test_follow_web.py b/tests/test_follow_web.py
index e5dc6202e..e69bbcd20 100644
--- a/tests/test_follow_web.py
+++ b/tests/test_follow_web.py
@@ -426,13 +426,15 @@ class FollowContractTests(unittest.TestCase):
找东西。筛选条里仍然保留——主动去筛和被动堆一排不是一回事。
"""
page = (ROOT / "web" / "app.js").read_text(encoding="utf-8")
- self.assertIn(
- "const followCardTags=item=>(item.tags||[])"
- ".filter(tag=>followTagType(item,tag)!=='general');", page)
+ # 只留玩法:题材(copyright/artist)整类不上卡片
+ self.assertIn("if(followTagType(item,tag)!=='general')return false;", page)
+ # 站点把三种东西都塞进 general,所以还要按两条判据再筛
+ self.assertIn("FOLLOW_TAG_NOISE", page, "技术标记与计数要滤掉")
+ self.assertIn("FOLLOW_TAG_TOPICAL", page, "booru 的「角色 (作品)」写法要滤掉")
self.assertIn("const tags=followCardTags(item).slice(0,3)", page,
"卡片必须走过滤后的标签")
self.assertIn("data-follow-tag=\"${esc(key)}\"", page,
- "筛选条仍要能按标签筛,包括 general")
+ "筛选条仍要能按标签筛,包括被卡片滤掉的那些")
def test_generated_media_is_cached_for_a_month_not_a_day(self):
"""按 id 取的生成物内容不会变,一天太短。
@@ -442,7 +444,10 @@ class FollowContractTests(unittest.TestCase):
"""
from peach import api
- self.assertEqual(api.MEDIA_CACHE_SECONDS, 30 * 24 * 3600)
+ self.assertEqual(api.MEDIA_CACHE_SECONDS, 365 * 24 * 3600)
+ # 头像单独短一档:id 不变但人会换头像。
+ self.assertEqual(api.AVATAR_CACHE_SECONDS, 30 * 24 * 3600)
+ self.assertLess(api.AVATAR_CACHE_SECONDS, api.MEDIA_CACHE_SECONDS)
source = (ROOT / "src" / "peach" / "api.py").read_text(encoding="utf-8")
self.assertNotIn('max-age=86400', source,
"媒体端点不该再写死一天")
diff --git a/web/app.js b/web/app.js
index 0fd1bc0ed..e0e674b36 100644
--- a/web/app.js
+++ b/web/app.js
@@ -2293,11 +2293,37 @@ function followWhen(item){
}
const followTagType=(item,tag)=>item.tag_types&&item.tag_types[tag]||'general';
-/* 卡片上只留有身份的标签:作者、角色、作品、来源属性。
- general 是内容描述(footjob、standing sex、from behind 这类),三个一排占满卡片
- 底部,却几乎不用来找东西——按用户要求不在卡片上展示。筛选条里仍然保留:
- 那里是主动去筛,和卡片被动堆一排不是一回事。 */
-const followCardTags=item=>(item.tags||[]).filter(tag=>followTagType(item,tag)!=='general');
+/* 卡片上只留「玩法」这一类:体位、动作、视角。
+
+ 题材不留——`copyright`(Final Fantasy、Genshin Impact)和 `artist` 整类都是,
+ 看卡片时已经知道自己在看谁的什么作品了。
+
+ 麻烦在于站点把三种东西都塞进了 `general`:真正的玩法(straight、male pov)、
+ 技术标记(animated、loop、blender、sound)、以及又一份题材(Final_Fantasy_VII、
+ InitialA)。所以只按标签类型过滤不够,还要按两条判据再筛一遍。 */
+//: booru 的角色消歧写法 `角色 (作品)`。这类标签的 tag_types 常常也是 general,
+//: 靠类型分不出来,但括号后缀是站点约定,认得出。
+const FOLLOW_TAG_TOPICAL=/\([^)]+\)\s*$/;
+//: 计数与技术标记:说的是「这条素材怎么做的」,不是「里面发生了什么」。
+const FOLLOW_TAG_NOISE=/^(?:\d+\s*(?:boy|girl|futa)s?|solo|animated|loop|no\s*sound|sound|blender|3d|2d|cgi|hd|highres|absurdres|video|webm|mp4|vertical\s*video|horizontal\s*video)$/i;
+//: 把 `Final_Fantasy_(series)` 与 copyright 里的 `Final Fantasy` 归一到同一个键,
+//: 好把混进 general 的那份题材也认出来。
+const followTagKey=tag=>String(tag).toLowerCase()
+ .replace(/\([^)]*\)/g,'').replace(/[_\s-]+/g,'').trim();
+const followCardTags=item=>{
+ const tags=item.tags||[];
+ // 题材集合:非 general 的那些标签本身,用来反查混进 general 的同名项。
+ const topical=new Set(tags.filter(tag=>followTagType(item,tag)!=='general')
+ .map(followTagKey).filter(Boolean));
+ return tags.filter(tag=>{
+ if(followTagType(item,tag)!=='general')return false;
+ if(FOLLOW_TAG_NOISE.test(String(tag).trim()))return false;
+ if(FOLLOW_TAG_TOPICAL.test(String(tag)))return false;
+ const key=followTagKey(tag);
+ // 与某个题材同名,或本身就是题材的子条目(Final_Fantasy_VII ⊂ finalfantasy)
+ return !!key&&![...topical].some(t=>key===t||key.startsWith(t)||t.startsWith(key));
+ });
+};
const followTagChip=(item,tag,kind='span')=>`<${kind} class="tg r34-${
esc(followTagType(item,tag))}" data-follow-tag="${esc(tag)}">${esc(tagLabel(tag))}</${kind}>`;