Files
MoviePilot/app/actions/fetch_downloads.py
2025-02-22 09:57:32 +08:00

53 lines
1.4 KiB
Python

from app.actions import BaseAction
from app.schemas import ActionParams, ActionContext
from log import logger
class FetchDownloadsParams(ActionParams):
"""
获取下载任务参数
"""
pass
class FetchDownloadsAction(BaseAction):
"""
获取下载任务
"""
_downloads = []
@property
def name(self) -> str:
return "获取下载任务"
@property
def description(self) -> str:
return "获取下载任务,更新任务状态"
@property
def success(self) -> bool:
if not self._downloads:
return True
return True if all([d.completed for d in self._downloads]) else False
async def execute(self, params: FetchDownloadsParams, context: ActionContext) -> ActionContext:
"""
更新downloads中的下载任务状态
"""
self._downloads = context.downloads
for download in self._downloads:
logger.info(f"获取下载任务 {download.download_id} 状态 ...")
torrents = self.chain.list_torrents(hashs=[download.download_id])
if not torrents:
download.completed = True
continue
for t in torrents:
download.path = t.path
if t.progress >= 100:
logger.info(f"下载任务 {download.download_id} 已完成")
download.completed = True
self.job_done()
return context