add 正在下载查询

This commit is contained in:
jxxghp
2023-06-15 18:10:04 +08:00
parent 237fe69d82
commit 84e4a4a527
9 changed files with 70 additions and 10 deletions

View File

@ -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]:

View File

@ -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))

View File

@ -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": "下载文件整理",

View File

@ -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: 种子状态

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -9,6 +9,7 @@ class MediaType(Enum):
class TorrentStatus(Enum):
TRANSFER = "可转移"
DOWNLOADING = "下载中"
# 可监听事件