From 9484093d22ebf1590e63f6e235e6b50155f93a08 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 2 Jul 2024 11:11:25 +0800 Subject: [PATCH] fix downloader --- app/chain/__init__.py | 18 +++++++------- app/chain/download.py | 6 ++--- app/modules/qbittorrent/__init__.py | 36 +++++++++++++++------------- app/modules/transmission/__init__.py | 36 +++++++++++++++------------- 4 files changed, 50 insertions(+), 46 deletions(-) diff --git a/app/chain/__init__.py b/app/chain/__init__.py index a71e4187..d2f53397 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -329,8 +329,8 @@ class ChainBase(metaclass=ABCMeta): def download(self, content: Union[Path, str], download_dir: Path, cookie: str, episodes: Set[int] = None, category: str = None, - downloader: str = settings.DEFAULT_DOWNLOADER - ) -> Optional[Tuple[Optional[str], str]]: + downloader: str = None + ) -> Optional[Tuple[Optional[str], Optional[str], str]]: """ 根据种子文件,选择并添加下载任务 :param content: 种子文件地址或者磁力链接 @@ -339,7 +339,7 @@ class ChainBase(metaclass=ABCMeta): :param episodes: 需要下载的集数 :param category: 种子分类 :param downloader: 下载器 - :return: 种子Hash,错误信息 + :return: 下载器名称、种子Hash、错误信息 """ return self.run_module("download", content=content, download_dir=download_dir, cookie=cookie, episodes=episodes, category=category, @@ -358,7 +358,7 @@ class ChainBase(metaclass=ABCMeta): def list_torrents(self, status: TorrentStatus = None, hashs: Union[list, str] = None, - downloader: str = settings.DEFAULT_DOWNLOADER + downloader: str = None ) -> Optional[List[Union[TransferTorrent, DownloadingTorrent]]]: """ 获取下载器种子列表 @@ -390,7 +390,7 @@ class ChainBase(metaclass=ABCMeta): target_path=target_path, episodes_info=episodes_info, scrape=scrape) def transfer_completed(self, hashs: str, path: Path = None, - downloader: str = settings.DEFAULT_DOWNLOADER) -> None: + downloader: str = None) -> None: """ 转移完成后的处理 :param hashs: 种子Hash @@ -400,7 +400,7 @@ class ChainBase(metaclass=ABCMeta): return self.run_module("transfer_completed", hashs=hashs, path=path, downloader=downloader) def remove_torrents(self, hashs: Union[str, list], delete_file: bool = True, - downloader: str = settings.DEFAULT_DOWNLOADER) -> bool: + downloader: str = None) -> bool: """ 删除下载器种子 :param hashs: 种子Hash @@ -410,7 +410,7 @@ class ChainBase(metaclass=ABCMeta): """ return self.run_module("remove_torrents", hashs=hashs, delete_file=delete_file, downloader=downloader) - def start_torrents(self, hashs: Union[list, str], downloader: str = settings.DEFAULT_DOWNLOADER) -> bool: + def start_torrents(self, hashs: Union[list, str], downloader: str = None) -> bool: """ 开始下载 :param hashs: 种子Hash @@ -419,7 +419,7 @@ class ChainBase(metaclass=ABCMeta): """ return self.run_module("start_torrents", hashs=hashs, downloader=downloader) - def stop_torrents(self, hashs: Union[list, str], downloader: str = settings.DEFAULT_DOWNLOADER) -> bool: + def stop_torrents(self, hashs: Union[list, str], downloader: str = None) -> bool: """ 停止下载 :param hashs: 种子Hash @@ -429,7 +429,7 @@ class ChainBase(metaclass=ABCMeta): return self.run_module("stop_torrents", hashs=hashs, downloader=downloader) def torrent_files(self, tid: str, - downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[Union[TorrentFilesList, List[File]]]: + downloader: str = None) -> Optional[Union[TorrentFilesList, List[File]]]: """ 获取种子文件 :param tid: 种子Hash diff --git a/app/chain/download.py b/app/chain/download.py index 037c0c7b..8a37f64c 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -276,9 +276,9 @@ class DownloadChain(ChainBase): download_dir=download_dir, category=_media.category) if result: - _hash, error_msg = result + _downloader, _hash, error_msg = result else: - _hash, error_msg = None, "未知错误" + _downloader, _hash, error_msg = None, None, "未找到下载器" if _hash: # 下载文件路径 @@ -325,7 +325,7 @@ class DownloadChain(ChainBase): continue files_to_add.append({ "download_hash": _hash, - "downloader": settings.DEFAULT_DOWNLOADER, + "downloader": _downloader, "fullpath": str(download_dir / _folder_name / file), "savepath": str(download_dir / _folder_name), "filepath": file, diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index 7de90a89..17a93f58 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -21,6 +21,7 @@ from app.utils.system import SystemUtils class QbittorrentModule(_ModuleBase): _servers: Dict[str, Qbittorrent] = {} _default_server: Qbittorrent = None + _default_server_name: str = None def init_module(self) -> None: """ @@ -35,6 +36,7 @@ class QbittorrentModule(_ModuleBase): if server.type == "qbittorrent": self._servers[server.name] = Qbittorrent(**server.config) if server.default: + self._default_server_name = server.name self._default_server = self._servers[server.name] @staticmethod @@ -77,7 +79,7 @@ class QbittorrentModule(_ModuleBase): def download(self, content: Union[Path, str], download_dir: Path, cookie: str, episodes: Set[int] = None, category: str = None, - downloader: str = None) -> Optional[Tuple[Optional[str], str]]: + downloader: str = None) -> Optional[Tuple[Optional[str], Optional[str], str]]: """ 根据种子文件,选择并添加下载任务 :param content: 种子文件地址或者磁力链接 @@ -104,10 +106,10 @@ class QbittorrentModule(_ModuleBase): return "", 0 if not content: - return None, "下载内容为空" + return None, None, "下载内容为空" if isinstance(content, Path) and not content.exists(): logger.error(f"种子文件不存在:{content}") - return None, f"种子文件不存在:{content}" + return None, None, f"种子文件不存在:{content}" # 获取下载器 server = self.get_server(downloader) @@ -135,11 +137,11 @@ class QbittorrentModule(_ModuleBase): # 读取种子的名称 torrent_name, torrent_size = __get_torrent_info() if not torrent_name: - return None, f"添加种子任务失败:无法读取种子文件" + return None, None, f"添加种子任务失败:无法读取种子文件" # 查询所有下载器的种子 torrents, error = server.get_torrents() if error: - return None, "无法连接qbittorrent下载器" + return None, None, "无法连接qbittorrent下载器" if torrents: for torrent in torrents: # 名称与大小相等则认为是同一个种子 @@ -153,19 +155,19 @@ class QbittorrentModule(_ModuleBase): if settings.TORRENT_TAG and settings.TORRENT_TAG not in torrent_tags: logger.info(f"给种子 {torrent_hash} 打上标签:{settings.TORRENT_TAG}") server.set_torrents_tag(ids=torrent_hash, tags=[settings.TORRENT_TAG]) - return torrent_hash, f"下载任务已存在" - return None, f"添加种子任务失败:{content}" + return downloader or self._default_server, torrent_hash, f"下载任务已存在" + return None, None, f"添加种子任务失败:{content}" else: # 获取种子Hash torrent_hash = server.get_torrent_id_by_tag(tags=tag) if not torrent_hash: - return None, f"下载任务添加成功,但获取Qbittorrent任务信息失败:{content}" + return None, None, f"下载任务添加成功,但获取Qbittorrent任务信息失败:{content}" else: if is_paused: # 种子文件 torrent_files = server.get_files(torrent_hash) if not torrent_files: - return torrent_hash, "获取种子文件失败,下载任务可能在暂停状态" + return downloader or self._default_server, torrent_hash, "获取种子文件失败,下载任务可能在暂停状态" # 不需要的文件ID file_ids = [] @@ -190,15 +192,15 @@ class QbittorrentModule(_ModuleBase): server.torrents_set_force_start(torrent_hash) else: server.start_torrents(torrent_hash) - return torrent_hash, f"添加下载成功,已选择集数:{sucess_epidised}" + return downloader or self._default_server, torrent_hash, f"添加下载成功,已选择集数:{sucess_epidised}" else: if settings.QB_FORCE_RESUME: server.torrents_set_force_start(torrent_hash) - return torrent_hash, "添加下载成功" + return downloader or self._default_server, torrent_hash, "添加下载成功" def list_torrents(self, status: TorrentStatus = None, hashs: Union[list, str] = None, - downloader: str = settings.DEFAULT_DOWNLOADER + downloader: str = None ) -> Optional[List[Union[TransferTorrent, DownloadingTorrent]]]: """ 获取下载器种子列表 @@ -273,7 +275,7 @@ class QbittorrentModule(_ModuleBase): return ret_torrents def transfer_completed(self, hashs: str, path: Path = None, - downloader: str = settings.DEFAULT_DOWNLOADER) -> None: + downloader: str = None) -> None: """ 转移完成后的处理 :param hashs: 种子Hash @@ -296,7 +298,7 @@ class QbittorrentModule(_ModuleBase): shutil.rmtree(path, ignore_errors=True) def remove_torrents(self, hashs: Union[str, list], delete_file: bool = True, - downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[bool]: + downloader: str = None) -> Optional[bool]: """ 删除下载器种子 :param hashs: 种子Hash @@ -310,7 +312,7 @@ class QbittorrentModule(_ModuleBase): return server.delete_torrents(delete_file=delete_file, ids=hashs) def start_torrents(self, hashs: Union[list, str], - downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[bool]: + downloader: str = None) -> Optional[bool]: """ 开始下载 :param hashs: 种子Hash @@ -322,7 +324,7 @@ class QbittorrentModule(_ModuleBase): return None return server.start_torrents(ids=hashs) - def stop_torrents(self, hashs: Union[list, str], downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[bool]: + def stop_torrents(self, hashs: Union[list, str], downloader: str = None) -> Optional[bool]: """ 停止下载 :param hashs: 种子Hash @@ -334,7 +336,7 @@ class QbittorrentModule(_ModuleBase): return None return server.stop_torrents(ids=hashs) - def torrent_files(self, tid: str, downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[TorrentFilesList]: + def torrent_files(self, tid: str, downloader: str = None) -> Optional[TorrentFilesList]: """ 获取种子文件列表 """ diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index 8a3ec90e..2af4ec76 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -21,6 +21,7 @@ from app.utils.system import SystemUtils class TransmissionModule(_ModuleBase): _servers: Dict[str, Transmission] = {} _default_server: Transmission = None + _default_server_name: str = None def init_module(self) -> None: # 读取下载器配置 @@ -32,6 +33,7 @@ class TransmissionModule(_ModuleBase): if server.type == "transmission": self._servers[server.name] = Transmission(**server.config) if server.default: + self._default_server_name = server.name self._default_server = self._servers[server.name] @staticmethod @@ -77,7 +79,7 @@ class TransmissionModule(_ModuleBase): def download(self, content: Union[Path, str], download_dir: Path, cookie: str, episodes: Set[int] = None, category: str = None, - downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[Tuple[Optional[str], str]]: + downloader: str = None) -> Optional[Tuple[Optional[str], Optional[str], str]]: """ 根据种子文件,选择并添加下载任务 :param content: 种子文件地址或者磁力链接 @@ -86,7 +88,7 @@ class TransmissionModule(_ModuleBase): :param episodes: 需要下载的集数 :param category: 分类,TR中未使用 :param downloader: 下载器 - :return: 种子Hash + :return: 下载器名称、种子Hash、错误原因 """ def __get_torrent_info() -> Tuple[str, int]: @@ -104,9 +106,9 @@ class TransmissionModule(_ModuleBase): return "", 0 if not content: - return None, "下载内容为空" + return None, None, "下载内容为空" if isinstance(content, Path) and not content.exists(): - return None, f"种子文件不存在:{content}" + return None, None, f"种子文件不存在:{content}" # 获取下载器 server = self.get_server(downloader) @@ -132,11 +134,11 @@ class TransmissionModule(_ModuleBase): # 读取种子的名称 torrent_name, torrent_size = __get_torrent_info() if not torrent_name: - return None, f"添加种子任务失败:无法读取种子文件" + return None, None, f"添加种子任务失败:无法读取种子文件" # 查询所有下载器的种子 torrents, error = server.get_torrents() if error: - return None, "无法连接transmission下载器" + return None, None, "无法连接transmission下载器" if torrents: for torrent in torrents: # 名称与大小相等则认为是同一个种子 @@ -155,15 +157,15 @@ class TransmissionModule(_ModuleBase): if settings.TORRENT_TAG and settings.TORRENT_TAG not in labels: labels.append(settings.TORRENT_TAG) server.set_torrent_tag(ids=torrent_hash, tags=labels) - return torrent_hash, f"下载任务已存在" - return None, f"添加种子任务失败:{content}" + return downloader or self._default_server, torrent_hash, f"下载任务已存在" + return None, None, f"添加种子任务失败:{content}" else: torrent_hash = torrent.hashString if is_paused: # 选择文件 torrent_files = server.get_files(torrent_hash) if not torrent_files: - return torrent_hash, "获取种子文件失败,下载任务可能在暂停状态" + return downloader or self._default_server, torrent_hash, "获取种子文件失败,下载任务可能在暂停状态" # 需要的文件信息 file_ids = [] unwanted_file_ids = [] @@ -184,13 +186,13 @@ class TransmissionModule(_ModuleBase): server.set_unwanted_files(torrent_hash, unwanted_file_ids) # 开始任务 server.start_torrents(torrent_hash) - return torrent_hash, "添加下载任务成功" + return downloader or self._default_server, torrent_hash, "添加下载任务成功" else: - return torrent_hash, "添加下载任务成功" + return downloader or self._default_server, torrent_hash, "添加下载任务成功" def list_torrents(self, status: TorrentStatus = None, hashs: Union[list, str] = None, - downloader: str = settings.DEFAULT_DOWNLOADER + downloader: str = None ) -> Optional[List[Union[TransferTorrent, DownloadingTorrent]]]: """ 获取下载器种子列表 @@ -259,7 +261,7 @@ class TransmissionModule(_ModuleBase): return ret_torrents def transfer_completed(self, hashs: str, path: Path = None, - downloader: str = settings.DEFAULT_DOWNLOADER) -> None: + downloader: str = None) -> None: """ 转移完成后的处理 :param hashs: 种子Hash @@ -291,7 +293,7 @@ class TransmissionModule(_ModuleBase): shutil.rmtree(path, ignore_errors=True) def remove_torrents(self, hashs: Union[str, list], delete_file: bool = True, - downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[bool]: + downloader: str = None) -> Optional[bool]: """ 删除下载器种子 :param hashs: 种子Hash @@ -306,7 +308,7 @@ class TransmissionModule(_ModuleBase): return server.delete_torrents(delete_file=delete_file, ids=hashs) def start_torrents(self, hashs: Union[list, str], - downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[bool]: + downloader: str = None) -> Optional[bool]: """ 开始下载 :param hashs: 种子Hash @@ -320,7 +322,7 @@ class TransmissionModule(_ModuleBase): return server.start_torrents(ids=hashs) def stop_torrents(self, hashs: Union[list, str], - downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[bool]: + downloader: str = None) -> Optional[bool]: """ 停止下载 :param hashs: 种子Hash @@ -333,7 +335,7 @@ class TransmissionModule(_ModuleBase): return None return server.start_torrents(ids=hashs) - def torrent_files(self, tid: str, downloader: str = settings.DEFAULT_DOWNLOADER) -> Optional[List[File]]: + def torrent_files(self, tid: str, downloader: str = None) -> Optional[List[File]]: """ 获取种子文件列表 """