From 4699f511bff309546245a03de37d398c0eb24bb3 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 23 Aug 2025 10:51:32 +0800 Subject: [PATCH] Handle magnet links in torrent parsing and downloader modules (#4815) Co-authored-by: Cursor Agent Co-authored-by: jxxghp --- app/helper/torrent.py | 9 +++++++++ app/modules/qbittorrent/__init__.py | 12 +++++++++++- app/modules/transmission/__init__.py | 12 +++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/helper/torrent.py b/app/helper/torrent.py index f501c63b..8989f2ca 100644 --- a/app/helper/torrent.py +++ b/app/helper/torrent.py @@ -201,6 +201,15 @@ class TorrentHelper(metaclass=WeakSingleton): """ if not torrent_content: return "", [] + + # 检查是否为磁力链接 + if isinstance(torrent_content, str) and torrent_content.startswith("magnet:"): + # 磁力链接无法预先获取文件信息,返回空 + return "", [] + elif isinstance(torrent_content, bytes) and torrent_content.startswith(b"magnet:"): + # 磁力链接无法预先获取文件信息,返回空 + return "", [] + try: # 解析种子内容 torrentinfo = Torrent.from_string(torrent_content) diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index 0d872505..f018541d 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -125,6 +125,14 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]): else: torrent_content = content + # 检查是否为磁力链接 + if isinstance(torrent_content, str) and torrent_content.startswith("magnet:"): + # 磁力链接不需要解析种子文件 + return None, torrent_content + elif isinstance(torrent_content, bytes) and torrent_content.startswith(b"magnet:"): + # 磁力链接不需要解析种子文件 + return None, torrent_content + if torrent_content: torrent_info = Torrent.from_string(torrent_content) @@ -138,7 +146,9 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]): # 读取种子的名称 torrent, content = __get_torrent_info() - if not torrent: + # 检查是否为磁力链接 + is_magnet = isinstance(content, str) and content.startswith("magnet:") or isinstance(content, bytes) and content.startswith(b"magnet:") + if not torrent and not is_magnet: return None, None, None, f"添加种子任务失败:无法读取种子文件" # 获取下载器 diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index 6c2d3e84..9b5fd654 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -126,6 +126,14 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]): else: torrent_content = content + # 检查是否为磁力链接 + if isinstance(torrent_content, str) and torrent_content.startswith("magnet:"): + # 磁力链接不需要解析种子文件 + return None, torrent_content + elif isinstance(torrent_content, bytes) and torrent_content.startswith(b"magnet:"): + # 磁力链接不需要解析种子文件 + return None, torrent_content + if torrent_content: torrent_info = Torrent.from_string(torrent_content) @@ -139,7 +147,9 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]): # 读取种子的名称 torrent, content = __get_torrent_info() - if not torrent: + # 检查是否为磁力链接 + is_magnet = isinstance(content, str) and content.startswith("magnet:") or isinstance(content, bytes) and content.startswith(b"magnet:") + if not torrent and not is_magnet: return None, None, None, f"添加种子任务失败:无法读取种子文件" # 获取下载器