From 28b429a5d702a8d54fd54ddf33e3268474820e15 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Mon, 10 Jul 2023 18:38:56 +0800 Subject: [PATCH] add downloader info --- app/api/endpoints/dashboard.py | 22 +++++++++++++++++++--- app/chain/dashboard.py | 6 ++++++ app/modules/qbittorrent/__init__.py | 16 +++++++++++++++- app/modules/qbittorrent/qbittorrent.py | 11 +++++++++++ app/modules/transmission/__init__.py | 13 +++++++++++++ app/modules/transmission/transmission.py | 11 +++++++++++ app/schemas/dashboard.py | 13 +++++++++++++ 7 files changed, 88 insertions(+), 4 deletions(-) diff --git a/app/api/endpoints/dashboard.py b/app/api/endpoints/dashboard.py index c6d2bbc0..043d3df7 100644 --- a/app/api/endpoints/dashboard.py +++ b/app/api/endpoints/dashboard.py @@ -31,16 +31,32 @@ def storage(_: schemas.TokenPayload = Depends(verify_token)) -> Any: """ 查询存储空间信息 """ - total_storage, used_storage = SystemUtils.space_usage(Path(settings.LIBRARY_PATH)) + total_storage, free_storage = SystemUtils.space_usage(Path(settings.LIBRARY_PATH)) return schemas.Storage( total_storage=total_storage, - used_storage=used_storage + used_storage=total_storage - free_storage ) @router.get("/processes", summary="进程信息", response_model=List[schemas.ProcessInfo]) def processes(_: schemas.TokenPayload = Depends(verify_token)) -> Any: """ - 进程信息 + 查询进程信息 """ return SystemUtils.processes() + + +@router.get("/downloader", summary="下载器信息", response_model=schemas.DownloaderInfo) +def downloader(_: schemas.TokenPayload = Depends(verify_token)) -> Any: + """ + 查询下载器信息 + """ + transfer_info = DashboardChain().downloader_info() + free_space = SystemUtils.free_space(Path(settings.DOWNLOAD_PATH)) + return schemas.DownloaderInfo( + download_speed=transfer_info.download_speed, + upload_speed=transfer_info.upload_speed, + download_size=transfer_info.download_size, + upload_size=transfer_info.upload_size, + free_space=free_space + ) diff --git a/app/chain/dashboard.py b/app/chain/dashboard.py index cb03e3d7..c2b5c65f 100644 --- a/app/chain/dashboard.py +++ b/app/chain/dashboard.py @@ -11,3 +11,9 @@ class DashboardChain(ChainBase): 媒体数量统计 """ return self.run_module("media_statistic") + + def downloader_info(self) -> schemas.DownloaderInfo: + """ + 下载器信息 + """ + return self.run_module("downloader_info") diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index b0b7f12f..8932fd3c 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -1,14 +1,15 @@ from pathlib import Path from typing import Set, Tuple, Optional, Union, List +from app import schemas from app.core.config import settings from app.core.metainfo import MetaInfo from app.log import logger from app.modules import _ModuleBase from app.modules.qbittorrent.qbittorrent import Qbittorrent from app.schemas import TransferInfo, TransferTorrent, DownloadingTorrent -from app.utils.string import StringUtils from app.schemas.types import TorrentStatus +from app.utils.string import StringUtils class QbittorrentModule(_ModuleBase): @@ -184,3 +185,16 @@ class QbittorrentModule(_ModuleBase): :return: bool """ return self.qbittorrent.start_torrents(ids=hashs) + + def downloader_info(self) -> schemas.DownloaderInfo: + """ + 下载器信息 + """ + # 调用Qbittorrent API查询实时信息 + info = self.qbittorrent.transfer_info() + return schemas.DownloaderInfo( + download_speed=info.get("dl_info_speed"), + upload_speed=info.get("up_info_speed"), + download_size=info.get("dl_info_data"), + upload_size=info.get("up_info_data") + ) diff --git a/app/modules/qbittorrent/qbittorrent.py b/app/modules/qbittorrent/qbittorrent.py index 5d8300da..f4d9f20d 100644 --- a/app/modules/qbittorrent/qbittorrent.py +++ b/app/modules/qbittorrent/qbittorrent.py @@ -4,6 +4,7 @@ from typing import Optional, Union, Tuple, List import qbittorrentapi from qbittorrentapi import TorrentFilesList, TorrentDictionary from qbittorrentapi.client import Client +from qbittorrentapi.transfer import TransferInfoDictionary from app.core.config import settings from app.log import logger @@ -285,3 +286,13 @@ class Qbittorrent(metaclass=Singleton): except Exception as err: logger.error(f"设置种子文件状态出错:{err}") return False + + def transfer_info(self) -> Optional[TransferInfoDictionary]: + """ + 获取传输信息 + """ + try: + return self.qbc.transfer_info() + except Exception as err: + logger.error(f"获取传输信息出错:{err}") + return None diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index 47eb6686..47bbee50 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -1,6 +1,7 @@ from pathlib import Path from typing import Set, Tuple, Optional, Union, List +from app import schemas from app.core.config import settings from app.core.metainfo import MetaInfo from app.log import logger @@ -168,3 +169,15 @@ class TransmissionModule(_ModuleBase): :return: bool """ return self.transmission.start_torrents(ids=hashs) + + def downloader_info(self) -> schemas.DownloaderInfo: + """ + 下载器信息 + """ + info = self.transmission.transfer_info() + return schemas.DownloaderInfo( + download_speed=info.download_speed, + upload_speed=info.upload_speed, + download_size=info.current_stats.downloaded_bytes, + upload_size=info.current_stats.uploaded_bytes + ) diff --git a/app/modules/transmission/transmission.py b/app/modules/transmission/transmission.py index 7e07f1a4..3d6577ab 100644 --- a/app/modules/transmission/transmission.py +++ b/app/modules/transmission/transmission.py @@ -2,6 +2,7 @@ from typing import Optional, Union, Tuple, List import transmission_rpc from transmission_rpc import Client, Torrent, File +from transmission_rpc.session import SessionStats from app.core.config import settings from app.log import logger @@ -214,3 +215,13 @@ class Transmission(metaclass=Singleton): except Exception as err: logger.error(f"设置下载文件状态出错:{err}") return False + + def transfer_info(self) -> Optional[SessionStats]: + """ + 获取传输信息 + """ + try: + return self.trc.session_stats() + except Exception as err: + logger.error(f"获取传输信息出错:{err}") + return None diff --git a/app/schemas/dashboard.py b/app/schemas/dashboard.py index 27ea1133..621574b9 100644 --- a/app/schemas/dashboard.py +++ b/app/schemas/dashboard.py @@ -36,3 +36,16 @@ class ProcessInfo(BaseModel): create_time: Optional[float] = 0.0 # 进程运行时间 秒 run_time: Optional[float] = 0.0 + + +class DownloaderInfo(BaseModel): + # 下载速度 + download_speed: Optional[float] = 0.0 + # 上传速度 + upload_speed: Optional[float] = 0.0 + # 下载量 + download_size: Optional[float] = 0.0 + # 上传量 + upload_size: Optional[float] = 0.0 + # 剩余空间 + free_space: Optional[float] = 0.0