The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
feat(bittorrent): 支持直接粘贴 Info Hash 添加磁力任务 #1024
XiaoYouChRbb6e77d2727b
diff --git a/GLOSSARY.md b/GLOSSARY.mdindex 6badfb28..87810371 100644--- a/GLOSSARY.md+++ b/GLOSSARY.md@@ -109,6 +109,10 @@ _Avoid_: pending task、unconfirmed task在任何入口,Wrapped Link 的表现都与直接给出它的 Inner Link 相同;Task 只记 Inner Link。只解一层。_Avoid_: 加密链接(Base64 不是加密)、专用链、转换+**Info Hash**:+种子内容的标识,写作 40 位十六进制或 32 位 Base32。主动添加时单独给出 Info Hash,表现与只含它的 magnet 链接相同;Task 记 magnet 链接。Passive Capture 不识别。+_Avoid_: 特征码、哈希链接+**Resource**:浏览器扩展捕获的可下载物。由 Browser Service 转换为 Task Options 进入任务创建流程。_Avoid_: 与泛义"资源"混淆diff --git a/features/bittorrent_pack/pack.py b/features/bittorrent_pack/pack.pyindex 52d98b3a..430ad11e 100644--- a/features/bittorrent_pack/pack.py+++ b/features/bittorrent_pack/pack.py@@ -1,6 +1,7 @@from __future__ import annotationsimport asyncio+import refrom base64 import b64encodefrom pathlib import Path, PurePosixPathfrom urllib.parse import urlparse@@ -20,6 +21,12 @@ from .session import PROXY_SCHEMES, btSessionfrom .task import BTFile, BTTask, BTTaskStep, BTTorrentFileStepfrom .trackers import mergedTrackers, trackerList+INFO_HASH_PATTERN = re.compile(r"[0-9a-f]{40}|[a-z2-7]{32}", re.IGNORECASE)+++def toMagnetUrl(url: str) -> str:+ return f"magnet:?xt=urn:btih:{url}" if INFO_HASH_PATTERN.fullmatch(url) else url+def refreshTrackerListIfStale() -> None:if bittorrentConfig.autoRefreshWebTrackers.value and trackerList.isStale():@@ -32,13 +39,13 @@ class TorrentParser(TaskParser):def match(self, options: TaskOptions) -> bool:if options.isProbed:return options.file.matchType({".torrent"}, {"application/x-bittorrent"})- url = options.url.strip()+ url = toMagnetUrl(options.url.strip())if urlparse(url).scheme.lower() == "magnet":return Truereturn localFilePath(url, {".torrent"}) is not Noneasync def parse(self, options: TaskOptions) -> Task:- url = options.url.strip()+ url = toMagnetUrl(options.url.strip())outputFolder = options.outputFolderrefreshTrackerListIfStale()diff --git a/tests/bittorrent_pack/test_task_parser.py b/tests/bittorrent_pack/test_task_parser.pyindex f1c9434a..f2a574f1 100644--- a/tests/bittorrent_pack/test_task_parser.py+++ b/tests/bittorrent_pack/test_task_parser.py@@ -8,6 +8,7 @@ from app.models.pack import PackServicesfrom app.models.task import TaskOptionsfrom bittorrent_pack.config import bittorrentConfigfrom bittorrent_pack.pack import BitTorrentPack+from bittorrent_pack.session import btSessionfrom bittorrent_pack.task import BTTaskfrom http_pack.pack import HttpPackfrom http_pack.task import HttpTask@@ -68,3 +69,32 @@ async def test_parse_invalid_online_torrent_adds_plain_file(features, startServeasync def test_probe_magnet_link_is_unsupported(features):with pytest.raises(NotImplementedError):await features.probe(TaskOptions(url=f"magnet:?xt=urn:btih:{'0' * 40}"))++[email protected]("infoHash", ["ab" * 20, "AB" * 20, "abcdefgh234567ab" * 2, "ABCDEFGH234567AB" * 2])+def test_match_bare_info_hash(features, infoHash):+ assert features.match(f" {infoHash} ")++[email protected]("text", ["ab" * 19, "ab" * 21, "g" + "a" * 39, "abcdefgh234567ab" * 2 + "c", "ab" * 32])+def test_match_rejects_text_that_is_not_info_hash(features, text):+ assert not features.match(text)++[email protected]("ignore::DeprecationWarning:bittorrent_pack.pack")+async def test_parse_bare_info_hash_as_magnet_link(features, monkeypatch, tmp_path):+ infoHash = "AB" * 20+ torrentBytes = buildTorrent(tmp_path)+ fetchedUrls = []++ async def fetchMetadata(magnetUri, trackers):+ fetchedUrls.append(magnetUri)+ return torrentBytes+ monkeypatch.setattr(btSession, "fetchMetadata", fetchMetadata)++ task = await features.parse(TaskOptions(url=f" {infoHash} "))++ assert isinstance(task, BTTask)+ assert task.url == f"magnet:?xt=urn:btih:{infoHash}"+ assert task.sourceType == "magnet"+ assert fetchedUrls == [task.url]