From a4ccd86d85d0636517c695dd9244949889b92e0b Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 10 Nov 2026 07:05:07 +0800 Subject: [PATCH] fix(torrent): support traditional first-download prompts --- app/application/torrent/download.py | 8 ++++++-- tests/test_torrent_download_retry.py | 5 +++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/application/torrent/download.py b/app/application/torrent/download.py index a02e2d725..535827c27 100644 --- a/app/application/torrent/download.py +++ b/app/application/torrent/download.py @@ -36,6 +36,10 @@ from app.schemas.media import resolve_media_identity from app.schemas.types import MediaSource, MediaType, SystemConfigKey _SIZE_UNIT = 1024 * 1024 +# NexusPHP 首次下载提示会随站点会话语言返回简体或繁体文案。 +_FIRST_DOWNLOAD_MARKERS = tuple( + marker.encode("utf-8") for marker in ("下载种子文件", "下載種子檔案") +) def _is_transient_torrent_status(status: int) -> bool: @@ -457,7 +461,7 @@ class TorrentHelper: return False if self.get_fileinfo_from_torrent_content(content)[1]: return False - return "下载种子文件".encode("utf-8") not in content + return not any(marker in content for marker in _FIRST_DOWNLOAD_MARKERS) def _cached_torrent_content(self, cache_backend: Any, cache_path: Path) -> Optional[tuple[ Union[str, bytes], str, list[str], @@ -532,7 +536,7 @@ class TorrentHelper: ) if file_list: return cache_path, req.content, folder_name, file_list, "" - if "下载种子文件".encode("utf-8") in req.content: + if any(marker in req.content for marker in _FIRST_DOWNLOAD_MARKERS): confirmation = _first_download_form(req.text, url) if confirmation is None: logger.warning("触发首次种子下载,但未找到可安全提交的确认表单") diff --git a/tests/test_torrent_download_retry.py b/tests/test_torrent_download_retry.py index ef12f27a8..b7031d28e 100644 --- a/tests/test_torrent_download_retry.py +++ b/tests/test_torrent_download_retry.py @@ -417,6 +417,7 @@ def test_magnet_body_on_error_does_not_bypass_status_check(boundary): cache.set.assert_not_called() +@pytest.mark.parametrize("marker", ["下载种子文件", "下載種子檔案"]) @pytest.mark.parametrize("form", [ '
', "
", @@ -424,12 +425,12 @@ def test_magnet_body_on_error_does_not_bypass_status_check(boundary): '
', '
', ]) -def test_first_download_form_variants_are_confirmed(boundary, form): +def test_first_download_form_variants_are_confirmed(boundary, form, marker): """ 按 HTML 表单语义提交确认,兼容属性顺序、引号、实体和提交按钮。 """ helper, port, cache, sleep = boundary - page = ("下载种子文件" + form).encode() + page = (marker + form).encode() port.request.side_effect = [_response(content=page), _response()] assert helper.download_torrent(URL, cookie="session=test")[1] == CONTENT