From d199cf5690cfffa71d3c1213662ddac6fdafa555 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sun, 2 Jul 2023 18:16:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/download.py | 23 ++++++++++++++++++++++- app/chain/__init__.py | 16 ++++++++++++++++ app/chain/download.py | 16 ++++++++++++++++ app/modules/qbittorrent/__init__.py | 17 +++++++++++++++++ app/modules/transmission/__init__.py | 17 +++++++++++++++++ 5 files changed, 88 insertions(+), 1 deletion(-) diff --git a/app/api/endpoints/download.py b/app/api/endpoints/download.py index 12eaff3c..c68c4c86 100644 --- a/app/api/endpoints/download.py +++ b/app/api/endpoints/download.py @@ -10,9 +10,30 @@ router = APIRouter() @router.get("/", summary="正在下载", response_model=List[schemas.DownloadingTorrent]) -async def read_subscribes( +async def read_downloading( _: schemas.TokenPayload = Depends(verify_token)) -> Any: """ 查询正在下载的任务 """ return DownloadChain().downloading() + + +@router.put("/{hashString}", summary="开始/暂停", response_model=schemas.Response) +async def set_downloading( + hashString: str, + oper: str, + _: schemas.TokenPayload = Depends(verify_token)) -> Any: + """ + 控制下载任务 + """ + return DownloadChain().set_downloading(hashString, oper) + + +@router.delete("/{hashString}", summary="删除下载任务", response_model=schemas.Response) +async def remove_downloading( + hashString: str, + _: schemas.TokenPayload = Depends(verify_token)) -> Any: + """ + 控制下载任务 + """ + return DownloadChain().remove_downloading(hashString) diff --git a/app/chain/__init__.py b/app/chain/__init__.py index a36822d7..cfbfc6a6 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -253,6 +253,22 @@ class ChainBase(AbstractSingleton, metaclass=Singleton): """ return self.run_module("remove_torrents", hashs=hashs) + def start_torrents(self, hashs: Union[list, str]) -> bool: + """ + 开始下载 + :param hashs: 种子Hash + :return: bool + """ + return self.run_module("start_torrent", hash_str=hashs) + + def stop_torrents(self, hashs: Union[list, str]) -> bool: + """ + 停止下载 + :param hashs: 种子Hash + :return: bool + """ + return self.run_module("stop_torrent", hashs=hashs) + def media_exists(self, mediainfo: MediaInfo) -> Optional[ExistMediaInfo]: """ 判断媒体文件是否存在 diff --git a/app/chain/download.py b/app/chain/download.py index a5e9bdce..bcdc0737 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -552,3 +552,19 @@ class DownloadChain(ChainBase): } ret_torrents.append(torrent) return ret_torrents + + def set_downloading(self, hash_str, oper: str) -> bool: + """ + 控制下载任务 start/pause + """ + if oper == "start": + return self.start_torrents(hashs=[hash_str]) + elif oper == "pause": + return self.stop_torrents(hashs=[hash_str]) + return False + + def remove_downloading(self, hash_str: str) -> bool: + """ + 删除下载任务 + """ + return self.remove_torrents(hashs=[hash_str]) diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index c604b6b2..c76309fa 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -141,6 +141,7 @@ class QbittorrentModule(_ModuleBase): season_episode=meta.season_episode, progress=torrent.get('progress'), size=torrent.get('total_size'), + state="downloading" if torrent.get('state') == "downloading" else "paused", dlspeed=StringUtils.str_filesize(torrent.get('dlspeed')), upspeed=StringUtils.str_filesize(torrent.get('upspeed')), )) @@ -167,3 +168,19 @@ class QbittorrentModule(_ModuleBase): :return: bool """ return self.qbittorrent.delete_torrents(delete_file=True, ids=hashs) + + def start_torrents(self, hashs: Union[list, str]) -> bool: + """ + 开始下载 + :param hashs: 种子Hash + :return: bool + """ + return self.qbittorrent.start_torrents(ids=hashs) + + def stop_torrents(self, hashs: Union[list, str]) -> bool: + """ + 停止下载 + :param hashs: 种子Hash + :return: bool + """ + return self.qbittorrent.start_torrents(ids=hashs) diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index dee5e3d0..47eb6686 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -124,6 +124,7 @@ class TransmissionModule(_ModuleBase): season_episode=meta.season_episode, progress=torrent.progress, size=torrent.total_size, + state="paused" if torrent.status == "stopped" else "downloading", dlspeed=StringUtils.str_filesize(torrent.download_speed), ulspeed=StringUtils.str_filesize(torrent.upload_speed), )) @@ -151,3 +152,19 @@ class TransmissionModule(_ModuleBase): :return: bool """ return self.transmission.delete_torrents(delete_file=True, ids=hashs) + + def start_torrents(self, hashs: Union[list, str]) -> bool: + """ + 开始下载 + :param hashs: 种子Hash + :return: bool + """ + return self.transmission.start_torrents(ids=hashs) + + def stop_torrents(self, hashs: Union[list, str]) -> bool: + """ + 停止下载 + :param hashs: 种子Hash + :return: bool + """ + return self.transmission.start_torrents(ids=hashs)