fix typing

This commit is contained in:
jxxghp
2023-06-13 23:41:50 +08:00
parent c1a22518a2
commit c06122ff19
15 changed files with 186 additions and 132 deletions

View File

@ -10,6 +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.utils.singleton import AbstractSingleton, Singleton
from app.utils.types import TorrentStatus, MediaType
@ -106,22 +107,23 @@ class ChainBase(AbstractSingleton, metaclass=Singleton):
def download_added(self, context: Context, torrent_path: Path) -> None:
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[dict]]:
def list_torrents(self, status: TorrentStatus = None,
hashs: Union[list, str] = None) -> Optional[List[TransferTorrent]]:
return self.run_module("list_torrents", status=status, hashs=hashs)
def transfer(self, path: str, mediainfo: MediaInfo) -> Optional[dict]:
def transfer(self, path: Path, mediainfo: MediaInfo) -> Optional[TransferInfo]:
return self.run_module("transfer", path=path, mediainfo=mediainfo)
def transfer_completed(self, hashs: Union[str, list], transinfo: dict) -> None:
def transfer_completed(self, hashs: Union[str, list], transinfo: TransferInfo) -> None:
return self.run_module("transfer_completed", hashs=hashs, transinfo=transinfo)
def remove_torrents(self, hashs: Union[str, list]) -> bool:
return self.run_module("remove_torrents", hashs=hashs)
def media_exists(self, mediainfo: MediaInfo) -> Optional[dict]:
def media_exists(self, mediainfo: MediaInfo) -> Optional[ExistMediaInfo]:
return self.run_module("media_exists", mediainfo=mediainfo)
def refresh_mediaserver(self, mediainfo: MediaInfo, file_path: str) -> Optional[bool]:
def refresh_mediaserver(self, mediainfo: MediaInfo, file_path: Path) -> Optional[bool]:
return self.run_module("refresh_mediaserver", mediainfo=mediainfo, file_path=file_path)
def post_message(self, title: str, text: str = None,

View File

@ -8,6 +8,7 @@ from app.core.meta import MetaBase
from app.core.metainfo import MetaInfo
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
@ -57,7 +58,7 @@ class DownloadChain(ChainBase):
def batch_download(self,
contexts: List[Context],
need_tvs: dict = None,
need_tvs: Dict[int, List[NotExistMediaInfo]] = None,
userid: str = None) -> Tuple[List[Context], dict]:
"""
根据缺失数据,自动种子列表中组合择优下载
@ -155,7 +156,13 @@ class DownloadChain(ChainBase):
"""
need = list(set(need).difference(set(current)))
if need:
need_tvs[tmdbid][seq]["episodes"] = need
not_exist = need_tvs[tmdbid][seq]
need_tvs[tmdbid][seq] = NotExistMediaInfo(
season=not_exist.season,
episodes=need,
total_episodes=not_exist.total_episodes,
start_episode=not_exist.start_episode
)
else:
need_tvs[tmdbid].pop(seq)
if not need_tvs.get(tmdbid) and need_tvs.get(tmdbid) is not None:
@ -189,10 +196,10 @@ class DownloadChain(ChainBase):
for tv in need_tv:
if not tv:
continue
if not tv.get("episodes"):
if not tv.episodes:
if not need_seasons.get(need_tmdbid):
need_seasons[need_tmdbid] = []
need_seasons[need_tmdbid].append(tv.get("season") or 1)
need_seasons[need_tmdbid].append(tv.season or 1)
# 查找整季包含的种子,只处理整季没集的种子或者是集数超过季的种子
for need_tmdbid, need_season in need_seasons.items():
for context in contexts:
@ -236,10 +243,10 @@ class DownloadChain(ChainBase):
continue
index = 0
for tv in need_tv:
need_season = tv.get("season") or 1
need_episodes = tv.get("episodes")
total_episodes = tv.get("total_episodes")
start_episode = tv.get("start_episode") or 1
need_season = tv.season or 1
need_episodes = tv.episodes
total_episodes = tv.total_episodes
start_episode = tv.start_episode or 1
# 缺失整季的转化为缺失集进行比较
if not need_episodes:
need_episodes = list(range(start_episode, total_episodes + start_episode))
@ -278,8 +285,8 @@ class DownloadChain(ChainBase):
continue
index = 0
for tv in need_tv:
need_season = tv.get("season") or 1
need_episodes = tv.get("episodes")
need_season = tv.season or 1
need_episodes = tv.episodes
if not need_episodes:
continue
for context in contexts:
@ -326,7 +333,9 @@ class DownloadChain(ChainBase):
return downloaded_list, need_tvs
def get_no_exists_info(self, meta: MetaBase,
mediainfo: MediaInfo, no_exists: dict = None) -> Tuple[bool, dict]:
mediainfo: MediaInfo,
no_exists: Dict[int, List[NotExistMediaInfo]] = None
) -> Tuple[bool, Dict[int, List[NotExistMediaInfo]]]:
"""
检查媒体库,查询是否存在,对于剧集同时返回不存在的季集信息
:param meta: 元数据
@ -347,26 +356,26 @@ class DownloadChain(ChainBase):
"""
if not no_exists.get(mediainfo.tmdb_id):
no_exists[mediainfo.tmdb_id] = [
{
"season": _season,
"episodes": _episodes,
"total_episodes": _total,
"start_episode": _start
}
NotExistMediaInfo(
season=_season,
episodes=_episodes,
total_episodes=_total,
start_episode=_start)
]
else:
no_exists[mediainfo.tmdb_id].append({
"season": _season,
"episodes": _episodes,
"total_episodes": _total,
"start_episode": _start
})
no_exists[mediainfo.tmdb_id].append(
NotExistMediaInfo(
season=_season,
episodes=_episodes,
total_episodes=_total,
start_episode=_start)
)
if not no_exists:
no_exists = {}
if mediainfo.type == MediaType.MOVIE:
# 电影
exists_movies: Optional[dict] = self.media_exists(mediainfo)
exists_movies: Optional[ExistMediaInfo] = self.media_exists(mediainfo)
if exists_movies:
logger.info(f"媒体库中已存在电影:{mediainfo.get_title_string()}")
return True, {}
@ -384,7 +393,7 @@ class DownloadChain(ChainBase):
logger.error(f"媒体信息中没有季集信息:{mediainfo.get_title_string()}")
return False, {}
# 电视剧
exists_tvs: Optional[dict] = self.media_exists(mediainfo)
exists_tvs: Optional[ExistMediaInfo] = self.media_exists(mediainfo)
if not exists_tvs:
# 所有剧集均缺失
for season, episodes in mediainfo.seasons.items():
@ -400,7 +409,7 @@ class DownloadChain(ChainBase):
if meta.begin_season \
and season not in meta.get_season_list():
continue
exist_seasons = exists_tvs.get("seasons")
exist_seasons = exists_tvs.seasons
if exist_seasons.get(season):
# 取差集
episodes = list(set(episodes).difference(set(exist_seasons[season])))

View File

@ -7,6 +7,7 @@ from app.core.meta import MetaBase
from app.core.metainfo import MetaInfo
from app.helper.sites import SitesHelper
from app.log import logger
from app.schemas.context import NotExistMediaInfo
from app.utils.string import StringUtils
from app.utils.types import MediaType
@ -22,7 +23,7 @@ class SearchChain(ChainBase):
def process(self, meta: MetaBase, mediainfo: MediaInfo,
keyword: str = None,
no_exists: Dict[int, List[dict]] = None) -> Optional[List[Context]]:
no_exists: Dict[int, List[NotExistMediaInfo]] = None) -> Optional[List[Context]]:
"""
根据媒体信息,执行搜索
:param meta: 元数据

View File

@ -9,6 +9,7 @@ from app.core.config import settings
from app.db.subscribes import Subscribes
from app.helper.sites import SitesHelper
from app.log import logger
from app.schemas.context import NotExistMediaInfo
from app.utils.string import StringUtils
from app.utils.types import MediaType
@ -290,7 +291,7 @@ class SubscribeChain(ChainBase):
})
@staticmethod
def __get_subscribe_no_exits(no_exists: Dict[int, List[dict]],
def __get_subscribe_no_exits(no_exists: Dict[int, List[NotExistMediaInfo]],
tmdb_id: int,
begin_season: int,
total_episode: int,
@ -310,37 +311,37 @@ class SubscribeChain(ChainBase):
index = 0
for no_exist in no_exists.get(tmdb_id):
# 替换原季值
if no_exist.get("season") == begin_season:
if no_exist.season == begin_season:
# 原季集列表
episode_list = no_exist.get("episodes")
episode_list = no_exist.episodes
# 原总集数
total = no_exist.get("total_episodes")
total = no_exist.total_episodes
if total_episode and start_episode:
# 有开始集和总集数
episodes = list(range(start_episode, total_episode + 1))
no_exists[tmdb_id][index] = {
"season": begin_season,
"episodes": episodes,
"total_episodes": total_episode,
"start_episode": start_episode
}
no_exists[tmdb_id][index] = NotExistMediaInfo(
season=begin_season,
episodes=episodes,
total_episodes=total_episode,
start_episode=start_episode
)
elif not start_episode:
# 有总集数没有开始集
episodes = list(range(min(episode_list or [1]), total_episode + 1))
no_exists[tmdb_id][index] = {
"season": begin_season,
"episodes": episodes,
"total_episodes": total_episode,
"start_episode": min(episode_list or [1])
}
no_exists[tmdb_id][index] = NotExistMediaInfo(
season=begin_season,
episodes=episodes,
total_episodes=total_episode,
start_episode=min(episode_list or [1])
)
elif not total_episode:
# 有开始集没有总集数
episodes = list(range(start_episode, max(episode_list or [total]) + 1))
no_exists[tmdb_id][index] = {
"season": begin_season,
"episodes": episodes,
"total_episodes": max(episode_list or [total]),
"start_episode": start_episode
}
no_exists[tmdb_id][index] = NotExistMediaInfo(
season=begin_season,
episodes=episodes,
total_episodes=max(episode_list or [total]),
start_episode=start_episode
)
index += 1
return no_exists

View File

@ -1,12 +1,13 @@
import re
from pathlib import Path
from typing import List, Optional
from app.chain import ChainBase
from app.core.config import settings
from app.core.context import MediaInfo
from app.core.meta import MetaBase
from app.core.metainfo import MetaInfo
from app.log import logger
from app.schemas.context import TransferInfo, TransferTorrent
from app.utils.string import StringUtils
from app.utils.system import SystemUtils
from app.utils.types import TorrentStatus
@ -43,16 +44,16 @@ class TransferChain(ChainBase):
logger.error(f"参数错误,参数:{arg_str}")
return False
# 获取种子
torrents: Optional[List[dict]] = self.list_torrents(hashs=torrent_hash)
torrents: Optional[List[TransferTorrent]] = self.list_torrents(hashs=torrent_hash)
if not torrents:
logger.error(f"没有获取到种子,参数:{arg_str}")
return False
# 识别前预处理
result: Optional[tuple] = self.prepare_recognize(title=torrents[0].get("title"))
result: Optional[tuple] = self.prepare_recognize(title=torrents[0].title)
if result:
title, subtitle = result
else:
title, subtitle = torrents[0].get("title"), None
title, subtitle = torrents[0].title, None
# 识别
meta = MetaInfo(title=title, subtitle=subtitle)
# 查询媒体信息
@ -61,7 +62,7 @@ class TransferChain(ChainBase):
arg_mediainfo = None
logger.info("开始执行下载器文件转移 ...")
# 从下载器获取种子列表
torrents: Optional[List[dict]] = self.list_torrents(status=TorrentStatus.TRANSFER)
torrents: Optional[List[TransferTorrent]] = self.list_torrents(status=TorrentStatus.TRANSFER)
if not torrents:
logger.info("没有获取到已完成的下载任务")
return False
@ -70,11 +71,11 @@ class TransferChain(ChainBase):
# 识别
for torrent in torrents:
# 识别前预处理
result: Optional[tuple] = self.prepare_recognize(title=torrent.get("title"))
result: Optional[tuple] = self.prepare_recognize(title=torrent.title)
if result:
title, subtitle = result
else:
title, subtitle = torrent.get("title"), None
title, subtitle = torrent.title, None
# 识别元数据
meta: MetaBase = MetaInfo(title=title, subtitle=subtitle)
if not meta.get_name():
@ -84,45 +85,45 @@ class TransferChain(ChainBase):
# 识别媒体信息
mediainfo: MediaInfo = self.recognize_media(meta=meta)
if not mediainfo:
logger.warn(f'未识别到媒体信息,标题:{torrent.get("title")}')
self.post_message(title=f"{torrent.get('title')} 未识别到媒体信息,无法入库!\n"
f"回复:```\n/transfer {torrent.get('hash')} [tmdbid]\n``` 手动识别转移。")
logger.warn(f'未识别到媒体信息,标题:{torrent.title}')
self.post_message(title=f"{torrent.title} 未识别到媒体信息,无法入库!\n"
f"回复:```\n/transfer {torrent.hash} [tmdbid]\n``` 手动识别转移。")
continue
else:
mediainfo = arg_mediainfo
logger.info(f"{torrent.get('title')} 识别为:{mediainfo.type.value} {mediainfo.get_title_string()}")
logger.info(f"{torrent.title} 识别为:{mediainfo.type.value} {mediainfo.get_title_string()}")
# 更新媒体图片
self.obtain_image(mediainfo=mediainfo)
# 转移
transferinfo: dict = self.transfer(mediainfo=mediainfo, path=torrent.get("path"))
if not transferinfo or not transferinfo.get("target_path"):
logger.warn(f"{torrent.get('title')} 入库失败")
transferinfo: TransferInfo = self.transfer(mediainfo=mediainfo, path=Path(torrent.path))
if not transferinfo or not transferinfo.target_path:
logger.warn(f"{torrent.title} 入库失败")
self.post_message(
title=f"{mediainfo.get_title_string()}{meta.get_season_episode_string()} 入库失败!",
text=f"原因:{transferinfo.get('message') if transferinfo else '未知'}",
text=f"原因:{transferinfo.message if transferinfo else '未知'}",
image=mediainfo.get_message_image()
),
continue
# 转移完成
self.transfer_completed(hashs=torrent.get("hash"), transinfo=transferinfo)
self.transfer_completed(hashs=torrent.hash, transinfo=transferinfo)
# 刮剥
self.scrape_metadata(path=transferinfo.get('target_path'), mediainfo=mediainfo)
self.scrape_metadata(path=transferinfo.target_path, mediainfo=mediainfo)
# 刷新媒体库
self.refresh_mediaserver(mediainfo=mediainfo, file_path=transferinfo.get('target_path'))
self.refresh_mediaserver(mediainfo=mediainfo, file_path=transferinfo.target_path)
# 发送通知
self.__send_transfer_message(meta=meta, mediainfo=mediainfo, transferinfo=transferinfo)
logger.info("下载器文件转移执行完成")
return True
def __send_transfer_message(self, meta: MetaBase, mediainfo: MediaInfo, transferinfo: dict):
def __send_transfer_message(self, meta: MetaBase, mediainfo: MediaInfo, transferinfo: TransferInfo):
"""
发送入库成功的消息
"""
# 文件大小
file_size = StringUtils.str_filesize(
SystemUtils.get_directory_size(
transferinfo.get('target_path')
transferinfo.target_path
)
)
msg_title = f"{mediainfo.get_title_string()} 已入库"