Skip to content

armgit / peach

Public Imported from GitHub
0

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

fix(player): 按发行身份匹配 SubtitleCat 中文字幕

longmeidaoc19b7d961c5e
diff --git a/docs/REUSE.md b/docs/REUSE.md
index c6ab896d9..03ccd5ea5 100644
--- a/docs/REUSE.md
+++ b/docs/REUSE.md
@@ -140,7 +140,7 @@ Peach 播放器皮肤直接装饰 Video.js 10 的 light DOM:右侧控制组用
字幕文件加载复用 `subtitles.to_webvtt`,接受 SRT、ASS、SSA、VTT,兼容已有的编码探测;4 MiB 以内的文件经 `/api/subtitles/convert` 转换后挂到当前 video 的 TextTrack,关闭播放器释放 Blob URL。字幕列表、CC 开关与外观选项按用户截图和 [YouTube 官方说明](https://support.google.com/youtube/answer/100078?hl=en) 组织;字体、字号、颜色、不透明度、背景、窗口与边缘样式保存在当前浏览器,时间轴仍由浏览器驱动。字幕文件不写 ledger,不保存到媒体目录。
-在线中文字幕采用 [SubtitleCat](https://www.subtitlecat.com),复用现有 httpx 与 BeautifulSoup 依赖。来源选择参考 [PotPlayer JAV 中文字幕插件](https://github.com/736368217/PotplayerJavChineseSubs_subtitlecat/tree/0b569fdc3bd2cb27fca2140858ad77bf6d7ddcb8)(MIT;仅参考公开协议与语言选择,不复制代码)。2026-10-18 用 `SSIS-291` 实测三条结果的简繁中文链接,简体 SRT 均返回 200、927–981 条时间轴。搜索每页检查六条详情,仅列已有 `zh-CN`/`zh-TW` 文件的结果,最多检查前 60 条,翻页由用户触发;不触发生成翻译。下载限固定 HTTPS 主机、字幕路径与 4 MiB,复用现有转码器。界面说明可能包含机翻,不声称人工翻译。ASSRT 的 JAV 覆盖未验证;JavSub.ai 有中文目录但检索返回 403,未取得可用下载链路,因此不接入。无需凭据,不增加依赖,不写 ledger 或媒体目录。
+在线中文字幕只采用 [SubtitleCat](https://www.subtitlecat.com),复用现有 httpx 与 BeautifulSoup 依赖。来源选择参考 [PotPlayer JAV 中文字幕插件](https://github.com/736368217/PotplayerJavChineseSubs_subtitlecat/tree/0b569fdc3bd2cb27fca2140858ad77bf6d7ddcb8)(MIT;仅参考公开协议与语言选择,不复制代码)。番号检索复用 `catalog_rules` 的规范化、查询变体与身份比较,结合 `feeds.scan_code` 读取结果标题;仅保留同一发行身份,再按每页六条读取详情。标题支持紧凑番号、语言和版本后缀;日期分隔符与未经确认的发行前缀保持区分。片名检索保留用户关键词。只列已有 `zh-CN`/`zh-TW` 文件的结果,最多检查前 60 条,翻页由用户触发;不触发生成翻译。下载限固定 HTTPS 主机、字幕路径与 4 MiB,复用现有转码器。2026-10-18 固定 20 个公开番号均取得中文文件;该小样本不代表全站或馆藏覆盖率,也不证明翻译准确。界面说明可能包含机翻,不声称人工翻译。ASSRT 不接入;JavSub.ai 在同批样本中有 8 个中文条目,7 个需购买、1 个标为免费,但下载返回 403,故不作为在线来源。无需凭据,不增加依赖,不写 ledger 或媒体目录。
## 分级诊断
diff --git a/src/peach/api.py b/src/peach/api.py
index 69487291f..2b4b6bbb2 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -10,7 +10,7 @@
| `routes_configuration` | 本机配置与账号设置 |
| `routes_jav_browser` | JAV 用户脚本的专用权限、添加与状态查询 |
| `routes_media` | 播放、分片、缩图、封面、头像、外链圆标 |
-| `routes_subtitles` | 字幕转换、ASSRT 搜索与加载、本机字幕服务配置 |
+| `routes_subtitles` | 字幕转换、SubtitleCat 中文字幕搜索与加载 |
| `routes_api` | JSON 契约出口,含两条 `/api/{route:path}` catch-all |
`include_router` 的顺序是契约的一部分,不是排版:FastAPI 按注册顺序匹配,
diff --git a/src/peach/subtitle_search.py b/src/peach/subtitle_search.py
index 4c30a87df..7842dc010 100644
--- a/src/peach/subtitle_search.py
+++ b/src/peach/subtitle_search.py
@@ -10,6 +10,8 @@ import httpx
from bs4 import BeautifulSoup
from . import subtitles
+from .catalog_rules import code_query_variants, release_code_from_text, same_release_code
+from .feeds import scan_code
from .user_agent import USER_AGENT
MAX_BYTES = 4 * 1024 * 1024
@@ -90,10 +92,24 @@ def _files(client: httpx.Client, url: str) -> list[dict]:
for index, code in enumerate(LANGUAGES) if code in rows]
+def _matches(title: str, code: str) -> bool:
+ if same_release_code(scan_code(title), code):
+ return True
+ for variant in code_query_variants(code):
+ if not same_release_code(variant, code):
+ continue
+ pattern = r"(?<![A-Za-z0-9])" + re.escape(variant).replace(r"\-", r"[-_ ]?") + r"(?![A-Za-z0-9])"
+ if any(same_release_code(match.group(), code) for match in re.finditer(pattern, title, re.I)):
+ return True
+ return False
+
+
def search(query: str, offset: int = 0) -> dict:
query = query.strip()
if not 3 <= len(query) <= 200 or not 0 <= offset < 60 or offset % PAGE_SIZE:
raise SubtitleSearchError("搜索词需为 3–200 个字符,分页位置无效时请重新搜索")
+ code = release_code_from_text(query)
+ query = code or query
with _client() as client:
page = BeautifulSoup(_get(client, SITE + "/index.php", params={"search": query}), "html.parser")
table = page.select_one("table.sub-table")
@@ -101,11 +117,14 @@ def search(query: str, offset: int = 0) -> dict:
raise SubtitleSearchError("SubtitleCat 搜索页面未取得,请稍后重试")
candidates = {}
for link in table.select("a[href]"):
+ title = link.get_text(" ", strip=True)
+ if code and not _matches(title, code):
+ continue
try:
url = _url(str(link["href"]), ".html")
except SubtitleSearchError:
continue
- candidates.setdefault(url, link.get_text(" ", strip=True))
+ candidates.setdefault(url, title)
results = []
for url, title in list(candidates.items())[offset:offset + PAGE_SIZE]:
files = _files(client, url)
diff --git a/tests/test_subtitle_search.py b/tests/test_subtitle_search.py
index fd6ff5c86..34fdc806d 100644
--- a/tests/test_subtitle_search.py
+++ b/tests/test_subtitle_search.py
@@ -39,6 +39,45 @@ class SubtitleSearchTests(unittest.TestCase):
self.assertEqual([row['index'] for row in rows], [0, 1])
self.assertTrue(all(row['playable'] and 'url' not in row for row in rows))
+ def test_release_search_filters_other_identities_before_pagination(self):
+ titles = ['ABP-7580', 'XABP-758', 'ABP-757', 'ABP-00758', 'abp758 中文',
+ 'ABP-758-kor-ja', 'ABP_758 中文字幕', 'ABP-758中文']
+ html = '<table class="sub-table">' + ''.join(
+ f'<tr><td><a href="/subs/{index}/track.html">{title}</a></td></tr>'
+ for index, title in enumerate(titles)) + '</table>'
+ details = []
+ def respond(request):
+ if request.url.path == '/index.php':
+ self.assertEqual(request.url.params['search'], 'ABP-758')
+ return httpx.Response(200, text=html)
+ details.append(request.url.path)
+ return httpx.Response(200, text=DETAIL)
+ with self.client(respond):
+ result = source.search('abp00758')
+ self.assertEqual([row['title'] for row in result['results']], titles[3:])
+ self.assertFalse(result['more'])
+ self.assertEqual(len(details), 5)
+
+ def test_keyword_search_keeps_the_query_and_language_filter(self):
+ def respond(request):
+ if request.url.path == '/index.php':
+ self.assertEqual(request.url.params['search'], '电影片名')
+ return httpx.Response(200, text=SEARCH)
+ return httpx.Response(200, text=DETAIL)
+ with self.client(respond):
+ self.assertEqual(len(source.search('电影片名')['results']), 2)
+
+ def test_matching_preserves_distribution_prefixes_and_date_separators(self):
+ for title, code, expected in (
+ ('390JAC-040 中文', 'JAC-040', False),
+ ('259LUXU-1475 中文', 'LUXU-1475', True),
+ ('092415_001 中文', '092415-001', False),
+ ('FC2-PPV-1812235-zh', 'FC2-PPV-1812235', True),
+ ('SSIS-291_(extract_from_Chinese)-en', 'SSIS-291', True),
+ ):
+ with self.subTest(title=title):
+ self.assertEqual(bool(source._matches(title, code)), expected)
+
def test_download_uses_selected_chinese_and_reuses_encoding_conversion(self):
requested = []
def respond(request):