Skip to content

The only downloader you need. 下载器的集大成者。

feat(bittorrent): 添加保存 Magnet 种子文件功能

XiaoYouChR847468725641
diff --git a/features/bittorrent_pack/cards.py b/features/bittorrent_pack/cards.py
index 27e0e2d4..c8a0e092 100644
--- a/features/bittorrent_pack/cards.py
+++ b/features/bittorrent_pack/cards.py
@@ -489,6 +489,8 @@ class BitTorrentTaskCard(UniversalTaskCard):
if not completely:
return
_removePath(Path(self.task.resolvePath))
+ if self.task.magnetTorrentPath is not None:
+ _removePath(self.task.magnetTorrentPath)
def onTaskFinished(self):
super().onTaskFinished()
diff --git a/features/bittorrent_pack/config.py b/features/bittorrent_pack/config.py
index 2efcb924..fee44307 100644
--- a/features/bittorrent_pack/config.py
+++ b/features/bittorrent_pack/config.py
@@ -207,6 +207,7 @@ class BitTorrentConfig(PackConfig):
enableNATPMP = ConfigItem("BitTorrent", "EnableNATPMP", True, BoolValidator())
seedRatioLimitPercent = RangeConfigItem("BitTorrent", "SeedRatioLimitPercent", 0, RangeValidator(0, 10000))
seedTimeLimitMinutes = RangeConfigItem("BitTorrent", "SeedTimeLimitMinutes", 0, RangeValidator(0, 43200))
+ saveMagnetTorrentFile = ConfigItem("BitTorrent", "SaveMagnetTorrentFile", False, BoolValidator())
enableWebTrackers = ConfigItem("BitTorrent", "EnableWebTrackers", True, BoolValidator())
autoRefreshWebTrackers = ConfigItem("BitTorrent", "AutoRefreshWebTrackers", True, BoolValidator())
webTrackerSource = ConfigItem(
@@ -296,6 +297,13 @@ class BitTorrentConfig(PackConfig):
texts=[self.tr("稀疏分配"), self.tr("预分配")],
parent=self.bittorrentGroup,
)
+ self.saveMagnetTorrentFileCard = SwitchSettingCard(
+ FluentIcon.SAVE,
+ self.tr("保存 Magnet 种子文件"),
+ self.tr("下载 magnet 链接时,在下载目录额外保存解析得到的 .torrent 文件"),
+ self.saveMagnetTorrentFile,
+ self.bittorrentGroup,
+ )
self.sequentialDownloadCard = SwitchSettingCard(
FluentIcon.LIBRARY,
self.tr("顺序下载"),
@@ -356,6 +364,7 @@ class BitTorrentConfig(PackConfig):
self.seedRatioLimitCard,
self.seedTimeLimitCard,
self.storageModeCard,
+ self.saveMagnetTorrentFileCard,
self.sequentialDownloadCard,
self.enableDHTCard,
self.enableLSDCard,
diff --git a/features/bittorrent_pack/task.py b/features/bittorrent_pack/task.py
index e899bee7..f3214cb6 100644
--- a/features/bittorrent_pack/task.py
+++ b/features/bittorrent_pack/task.py
@@ -322,6 +322,7 @@ class BitTorrentTask(Task):
storageMode: str = field(default_factory=lambda: bittorrentConfig.storageMode.value)
seedRatioLimitPercent: int = field(default_factory=lambda: bittorrentConfig.seedRatioLimitPercent.value)
seedTimeLimitMinutes: int = field(default_factory=lambda: bittorrentConfig.seedTimeLimitMinutes.value)
+ saveMagnetTorrentFile: bool = field(default_factory=lambda: bittorrentConfig.saveMagnetTorrentFile.value)
shareRatioPercent: float = field(default=0)
seedingTimeSeconds: int = field(default=0)
isSeeding: bool = field(default=False)
@@ -345,6 +346,12 @@ class BitTorrentTask(Task):
def stage(self) -> BitTorrentTaskStage:
return self.stages[0]
+ @property
+ def magnetTorrentPath(self) -> Path | None:
+ if self.sourceType != "magnet" or not self.saveMagnetTorrentFile:
+ return None
+ return self.path / f"{self.title}.torrent"
+
@property
def selectedFileCount(self) -> int:
return sum(1 for file in self.files if file.selected)
@@ -675,12 +682,23 @@ class BitTorrentWorker(Worker):
except Exception:
pass
+ def _saveMagnetTorrentFile(self):
+ torrentPath = self.task.magnetTorrentPath
+ if torrentPath is None:
+ return
+
+ try:
+ torrentPath.write_bytes(_decodeBytes(self.task.torrentData))
+ except Exception as e:
+ logger.opt(exception=e).warning("保存 magnet 种子文件失败 {}", self.task.title)
+
async def run(self):
if self.task.selectedFileCount <= 0:
self.stage.setStatus(TaskStatus.FAILED)
raise RuntimeError("至少需要选择一个文件")
Path(self.task.path).mkdir(parents=True, exist_ok=True)
+ self._saveMagnetTorrentFile()
self.session = _createSession(
listenPort=self.task.listenPort,
@@ -902,6 +920,7 @@ def buildTaskFromTorrentInfo(
storageMode=bittorrentConfig.storageMode.value,
seedRatioLimitPercent=bittorrentConfig.seedRatioLimitPercent.value,
seedTimeLimitMinutes=bittorrentConfig.seedTimeLimitMinutes.value,
+ saveMagnetTorrentFile=bittorrentConfig.saveMagnetTorrentFile.value,
)
return task