diff --git a/app/modules/rtorrent/__init__.py b/app/modules/rtorrent/__init__.py index 5dc9aaad6..b7072d63c 100644 --- a/app/modules/rtorrent/__init__.py +++ b/app/modules/rtorrent/__init__.py @@ -6,7 +6,7 @@ from app.foundation import size as size_tools from app.foundation import temporal as time_tools from app.foundation import text as text_tools from app.modules._base.downloader import _DownloaderModuleBase -from app.modules.rtorrent.rtorrent import Rtorrent +from app.modules.rtorrent.rtorrent import Rtorrent, TorrentData from app.runtime.log import logger from app.runtime.settings import get_runtime_setting from app.schemas.dashboard import DownloaderInfo as _SchemaDownloaderInfo @@ -208,8 +208,8 @@ class RtorrentModule(_DownloaderModuleBase[Rtorrent]): sucess_epidised = set() try: for torrent_file in torrent_files: - file_id = torrent_file.get("id") - file_name = torrent_file.get("name") + file_id = torrent_file["id"] + file_name = torrent_file["name"] meta_info = MetaInfo(file_name) if not meta_info.episode_list or not set( meta_info.episode_list @@ -269,20 +269,20 @@ class RtorrentModule(_DownloaderModuleBase[Rtorrent]): query_status = self._normalize_query_status(status) query_tags = None if include_all_tags else get_runtime_setting('TORRENT_TAG') - def __get_torrent_path(torrent_data: dict) -> Path: + def __get_torrent_path(torrent_data: TorrentData) -> Path: """ 获取种子内容路径。 """ content_path = torrent_data.get("content_path") if content_path: return Path(content_path) - return Path(torrent_data.get("save_path")) / torrent_data.get("name") + return Path(torrent_data["save_path"]) / torrent_data["name"] - def __build_torrent(downloader_name: str, torrent_data: dict) -> DownloaderTorrent: + def __build_torrent(downloader_name: str, torrent_data: TorrentData) -> DownloaderTorrent: """ 构造统一下载器任务对象。 """ - meta = MetaInfo(torrent_data.get("name")) + meta = MetaInfo(torrent_data["name"]) dlspeed = torrent_data.get("dlspeed") or 0 upspeed = torrent_data.get("upspeed") or 0 total_size = torrent_data.get("total_size") or 0 @@ -381,8 +381,8 @@ class RtorrentModule(_DownloaderModuleBase[Rtorrent]): @staticmethod def __normalize_torrent_state( - state: Optional[Union[int, str]], - complete: Optional[Union[int, str]], + state: Optional[Union[int, float, str]], + complete: Optional[Union[int, float, str]], ) -> str: """ 归一 rTorrent 原始任务状态。 diff --git a/app/modules/rtorrent/rtorrent.py b/app/modules/rtorrent/rtorrent.py index 504fa97b6..f126d0368 100644 --- a/app/modules/rtorrent/rtorrent.py +++ b/app/modules/rtorrent/rtorrent.py @@ -11,7 +11,7 @@ from urllib.parse import urlparse from app.runtime.log import logger -class _Torrent(TypedDict): +class TorrentData(TypedDict): """rTorrent 多调用结果映射,保留现有下载器字典字段。""" hash: str @@ -31,6 +31,16 @@ class _Torrent(TypedDict): progress: float +class TorrentFileData(TypedDict): + """rTorrent 文件索引和分块进度的字典合同。""" + + id: int + name: str + size: Union[int, float] + priority: Union[int, float] + progress: float + + def _rpc_rows(value: object) -> list[list[object]]: """校验 XML-RPC 多调用的二维数组,异常仍由调用方原有错误分支处理。""" if not isinstance(value, (list, tuple)): @@ -218,7 +228,7 @@ class Rtorrent: ids: Optional[Union[str, list[str]]] = None, status: Optional[str] = None, tags: Optional[Union[str, list[str]]] = None, - ) -> Tuple[List[_Torrent], bool]: + ) -> Tuple[List[TorrentData], bool]: """ 获取种子列表 :return: 种子列表, 是否发生异常 @@ -245,9 +255,9 @@ class Rtorrent: ] # 获取所有种子 results = self._proxy.d.multicall2("", "main", *fields) - torrents: list[_Torrent] = [] + torrents: list[TorrentData] = [] for r in _rpc_rows(results): - torrent: _Torrent = { + torrent: TorrentData = { "hash": _rpc_text(r[0]), "name": _rpc_text(r[1]), "total_size": _rpc_number(r[2]), @@ -301,7 +311,7 @@ class Rtorrent: def get_completed_torrents( self, ids: Optional[Union[str, list[str]]] = None, tags: Optional[Union[str, list[str]]] = None - ) -> Optional[List[_Torrent]]: + ) -> Optional[List[TorrentData]]: """ 获取已完成的种子 """ @@ -314,7 +324,7 @@ class Rtorrent: def get_downloading_torrents( self, ids: Optional[Union[str, list[str]]] = None, tags: Optional[Union[str, list[str]]] = None - ) -> Optional[List[_Torrent]]: + ) -> Optional[List[TorrentData]]: """ 获取正在下载的种子 """ @@ -444,7 +454,7 @@ class Rtorrent: logger.error(f"删除种子出错:{str(err)}") return False - def get_files(self, tid: str) -> Optional[List[Dict[str, object]]]: + def get_files(self, tid: str) -> Optional[List[TorrentFileData]]: """ 获取种子文件列表 """ @@ -462,14 +472,14 @@ class Rtorrent: "f.completed_chunks=", "f.size_chunks=", ) - result: list[dict[str, object]] = [] + result: list[TorrentFileData] = [] for idx, f in enumerate(_rpc_rows(files)): result.append( { "id": idx, - "name": f[0], - "size": f[1], - "priority": f[2], + "name": _rpc_text(f[0]), + "size": _rpc_number(f[1]), + "priority": _rpc_number(f[2]), "progress": _rpc_integer(f[3]) / _rpc_integer(f[4]) * 100 if _rpc_integer(f[4]) > 0 else 0, } ) @@ -479,7 +489,7 @@ class Rtorrent: return None def set_files( - self, torrent_hash: Optional[str] = None, file_ids: Optional[list[Union[int, str]]] = None, priority: int = 0 + self, torrent_hash: Optional[str] = None, file_ids: Optional[Union[list[int], list[str]]] = None, priority: int = 0 ) -> bool: """ 设置下载文件的优先级,priority为0为不下载,priority为1为普通 diff --git a/tests/test_rtorrent_responses.py b/tests/test_rtorrent_responses.py index 58d4e6339..ab0d50d04 100644 --- a/tests/test_rtorrent_responses.py +++ b/tests/test_rtorrent_responses.py @@ -54,7 +54,7 @@ def test_malformed_multicall_keeps_error_contract(client, payload): assert client.get_torrents() == ([], True) -@pytest.mark.parametrize("index,value", [(0, 12), (2, "1000"), (9, ["tag"]), (12, {})]) +@pytest.mark.parametrize("index,value", [(0, 12), (2, "1000"), (9, ["tag"]), (12, {"invalid": True})]) def test_invalid_torrent_fields_keep_error_contract(client, torrent_row, index, value): """身份、算术和标签字段不接受不符合 XML-RPC 合同的类型。""" torrent_row[index] = value