From 6a8c684af05e479822243010eeaccd1a9859733b Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 30 Nov 2023 15:58:44 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=85=BC=E5=AE=B9TR=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E9=87=8D=E5=A4=8D=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/modules/qbittorrent/__init__.py | 6 ++-- app/modules/transmission/__init__.py | 42 ++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index 917492cd..a8d3b8d6 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -98,11 +98,13 @@ class QbittorrentModule(_ModuleBase): for torrent in torrents: if torrent.get("name") == torrent_name: torrent_hash = torrent.get("hash") + torrent_tags = [str(tag).strip() for tag in torrent.get("tags").split(',')] logger.warn(f"下载器中已存在该种子任务:{torrent_hash} - {torrent.get('name')}") # 给种子打上标签 - if settings.TORRENT_TAG: - logger.info(f"给种子 {torrent_hash} 打上标签:{settings.TORRENT_TAG}") + if "已整理" in torrent_tags: self.qbittorrent.remove_torrents_tag(ids=torrent_hash, tag=['已整理']) + if settings.TORRENT_TAG and settings.TORRENT_TAG not in torrent_tags: + logger.info(f"给种子 {torrent_hash} 打上标签:{settings.TORRENT_TAG}") self.qbittorrent.set_torrents_tag(ids=torrent_hash, tags=[settings.TORRENT_TAG]) return torrent_hash, f"下载任务已存在" return None, f"添加种子任务失败:{content}" diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index 72d42de1..cccd384b 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -2,6 +2,7 @@ import shutil from pathlib import Path from typing import Set, Tuple, Optional, Union, List +from torrentool.torrent import Torrent from transmission_rpc import File from app import schemas @@ -47,6 +48,21 @@ class TransmissionModule(_ModuleBase): :param category: 分类,TR中未使用 :return: 种子Hash """ + + def __get_torrent_name(): + """ + 获取种子名称 + """ + try: + if isinstance(content, Path): + torrentinfo = Torrent.from_file(content) + else: + torrentinfo = Torrent.from_string(content) + return torrentinfo.name + except Exception as e: + logger.error(f"获取种子名称失败:{e}") + return "" + if not content: return if isinstance(content, Path) and not content.exists(): @@ -68,6 +84,32 @@ class TransmissionModule(_ModuleBase): cookie=cookie ) if not torrent: + # 读取种子的名称 + torrent_name = __get_torrent_name() + if not torrent_name: + return None, f"添加种子任务失败:无法读取种子文件" + # 查询所有下载器的种子 + torrents, error = self.transmission.get_torrents() + if error: + return None, "无法连接transmission下载器" + if torrents: + for torrent in torrents: + if torrent.name == torrent_name: + torrent_hash = torrent.hashString + logger.warn(f"下载器中已存在该种子任务:{torrent_hash} - {torrent.name}") + # 给种子打上标签 + if settings.TORRENT_TAG: + logger.info(f"给种子 {torrent_hash} 打上标签:{settings.TORRENT_TAG}") + # 种子标签 + labels = [str(tag).strip() + for tag in torrent.labels] if hasattr(torrent, "labels") else [] + if "已整理" in labels: + labels.remove("已整理") + self.transmission.set_torrent_tag(ids=torrent_hash, tags=labels) + if settings.TORRENT_TAG and settings.TORRENT_TAG not in labels: + labels.append(settings.TORRENT_TAG) + self.transmission.set_torrent_tag(ids=torrent_hash, tags=labels) + return torrent_hash, f"下载任务已存在" return None, f"添加种子任务失败:{content}" else: torrent_hash = torrent.hashString