diff --git a/app/chain/__init__.py b/app/chain/__init__.py index 85884b0c..e0fdced5 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -10,7 +10,7 @@ from app.core.module import ModuleManager from app.core.context import MediaInfo, TorrentInfo from app.core.meta import MetaBase from app.log import logger -from app.schemas.context import TransferInfo, TransferTorrent, ExistMediaInfo +from app.schemas.context import TransferInfo, TransferTorrent, ExistMediaInfo, DownloadingTorrent from app.utils.singleton import AbstractSingleton, Singleton from app.utils.types import TorrentStatus, MediaType @@ -111,7 +111,7 @@ class ChainBase(AbstractSingleton, metaclass=Singleton): return self.run_module("download_added", context=context, torrent_path=torrent_path) def list_torrents(self, status: TorrentStatus = None, - hashs: Union[list, str] = None) -> Optional[List[TransferTorrent]]: + hashs: Union[list, str] = None) -> Optional[List[Union[TransferTorrent, DownloadingTorrent]]]: return self.run_module("list_torrents", status=status, hashs=hashs) def transfer(self, path: Path, mediainfo: MediaInfo) -> Optional[TransferInfo]: diff --git a/app/chain/download.py b/app/chain/download.py index 7f9cf7ad..20394730 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -9,7 +9,7 @@ from app.helper.torrent import TorrentHelper from app.log import logger from app.schemas.context import ExistMediaInfo, NotExistMediaInfo from app.utils.string import StringUtils -from app.utils.types import MediaType +from app.utils.types import MediaType, TorrentStatus class DownloadChain(ChainBase): @@ -473,3 +473,20 @@ class DownloadChain(ChainBase): return False, no_exists # 全部存在 return True, no_exists + + def get_downloading(self): + """ + 查询正在下载的任务,并发送消息 + """ + torrents = self.list_torrents(status=TorrentStatus.DOWNLOADING) + if not torrents: + self.post_message(title="没有正在下载的任务!") + return + # 发送消息 + title = f"共 {len(torrents)} 个任务正在下载:" + messages = [] + for torrent in torrents: + messages.append(f"{torrent.title} " + f"{StringUtils.str_filesize(torrent.size)} " + f"{round(torrent.progress) * 100}%") + self.post_message(title=title, text="\n".join(messages)) diff --git a/app/command.py b/app/command.py index d5544fa5..e47903c4 100644 --- a/app/command.py +++ b/app/command.py @@ -5,6 +5,7 @@ from typing import Any from app.chain import ChainBase from app.chain.cookiecloud import CookieCloudChain from app.chain.douban_sync import DoubanSyncChain +from app.chain.download import DownloadChain from app.chain.site_message import SiteMessageChain from app.chain.subscribe import SubscribeChain from app.chain.transfer import TransferChain @@ -94,6 +95,11 @@ class Command(metaclass=Singleton): "description": "删除订阅", "data": {} }, + "/downloading": { + "func": DownloadChain().get_downloading, + "description": "正在下载", + "data": {} + }, "/transfer": { "func": TransferChain().process, "description": "下载文件整理", diff --git a/app/modules/__init__.py b/app/modules/__init__.py index 4c4057b0..f37de3c2 100644 --- a/app/modules/__init__.py +++ b/app/modules/__init__.py @@ -6,7 +6,7 @@ from ruamel.yaml import CommentedMap from app.core.context import MediaInfo, TorrentInfo, Context from app.core.meta import MetaBase -from app.schemas.context import TransferInfo, TransferTorrent, ExistMediaInfo +from app.schemas.context import TransferInfo, TransferTorrent, ExistMediaInfo, DownloadingTorrent from app.utils.types import TorrentStatus, MediaType @@ -165,7 +165,7 @@ class _ModuleBase(metaclass=ABCMeta): pass def list_torrents(self, status: TorrentStatus = None, - hashs: Union[list, str] = None) -> Optional[List[TransferTorrent]]: + hashs: Union[list, str] = None) -> Optional[List[Union[TransferTorrent, DownloadingTorrent]]]: """ 获取下载器种子列表 :param status: 种子状态 diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index 0b7eb298..56cf57c6 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -6,7 +6,7 @@ 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.context import TransferInfo, TransferTorrent +from app.schemas.context import TransferInfo, TransferTorrent, DownloadingTorrent from app.utils.string import StringUtils from app.utils.types import TorrentStatus @@ -86,7 +86,7 @@ class QbittorrentModule(_ModuleBase): return torrent_hash, "添加下载成功" def list_torrents(self, status: TorrentStatus = None, - hashs: Union[list, str] = None) -> Optional[List[TransferTorrent]]: + hashs: Union[list, str] = None) -> Optional[List[Union[TransferTorrent, DownloadingTorrent]]]: """ 获取下载器种子列表 :param status: 种子状态 @@ -128,6 +128,19 @@ class QbittorrentModule(_ModuleBase): hash=torrent.get('hash'), tags=torrent.get('tags') )) + elif status == TorrentStatus.DOWNLOADING: + # 获取正在下载的任务 + torrents = self.qbittorrent.get_downloading_torrents(tags=settings.TORRENT_TAG) + for torrent in torrents: + meta = MetaInfo(torrent.get('name')) + ret_torrents.append(DownloadingTorrent( + title=torrent.get('name'), + name=meta.name, + year=meta.year, + season_episode=meta.season_episode, + progress=torrent.get('progress'), + size=torrent.get('total_size') + )) else: return None return ret_torrents diff --git a/app/modules/slack/slack.py b/app/modules/slack/slack.py index dd56829c..667bbe6c 100644 --- a/app/modules/slack/slack.py +++ b/app/modules/slack/slack.py @@ -147,7 +147,8 @@ class Slack: result = self._client.chat_postMessage( channel=channel, text=message_text, - blocks=blocks + blocks=blocks, + mrkdwn=True ) return True, result except Exception as msg_e: diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index 7c627462..6e01df4d 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -6,7 +6,7 @@ from app.core.metainfo import MetaInfo from app.log import logger from app.modules import _ModuleBase from app.modules.transmission.transmission import Transmission -from app.schemas.context import TransferInfo, TransferTorrent +from app.schemas.context import TransferInfo, TransferTorrent, DownloadingTorrent from app.utils.types import TorrentStatus @@ -73,7 +73,7 @@ class TransmissionModule(_ModuleBase): return torrent_hash, "添加下载任务成功" def list_torrents(self, status: TorrentStatus = None, - hashs: Union[list, str] = None) -> Optional[List[TransferTorrent]]: + hashs: Union[list, str] = None) -> Optional[List[Union[TransferTorrent, DownloadingTorrent]]]: """ 获取下载器种子列表 :param status: 种子状态 @@ -110,6 +110,19 @@ class TransmissionModule(_ModuleBase): hash=torrent.hashString, tags=torrent.labels )) + elif status == TorrentStatus.DOWNLOADING: + # 获取正在下载的任务 + torrents = self.transmission.get_downloading_torrents(tags=settings.TORRENT_TAG) + for torrent in torrents: + meta = MetaInfo(torrent.name) + ret_torrents.append(DownloadingTorrent( + title=torrent.name, + name=meta.name, + year=meta.year, + season_episode=meta.season_episode, + progress=torrent.progress, + size=torrent.total_size + )) else: return None return ret_torrents diff --git a/app/schemas/context.py b/app/schemas/context.py index a0534b7d..6dade5a1 100644 --- a/app/schemas/context.py +++ b/app/schemas/context.py @@ -96,6 +96,15 @@ class TransferTorrent(BaseModel): tags: Optional[str] = None +class DownloadingTorrent(BaseModel): + title: Optional[str] = None + name: Optional[str] = None + year: Optional[str] = None + season_episode: Optional[str] = None + size: Optional[float] = 0 + progress: Optional[float] = 0 + + class TransferInfo(BaseModel): # 转移⼁路径 path: Optional[Path] = None diff --git a/app/utils/types.py b/app/utils/types.py index a678098d..1d94ee1b 100644 --- a/app/utils/types.py +++ b/app/utils/types.py @@ -9,6 +9,7 @@ class MediaType(Enum): class TorrentStatus(Enum): TRANSFER = "可转移" + DOWNLOADING = "下载中" # 可监听事件