add media exists api

This commit is contained in:
jxxghp 2023-07-06 15:36:32 +08:00
parent 84acb70ec2
commit a9dc77ce0c
8 changed files with 55 additions and 12 deletions

View File

@ -4,7 +4,10 @@ from fastapi import APIRouter, Depends
from app import schemas from app import schemas
from app.chain.download import DownloadChain from app.chain.download import DownloadChain
from app.core.context import MediaInfo
from app.core.metainfo import MetaInfo
from app.core.security import verify_token from app.core.security import verify_token
from app.schemas import NotExistMediaInfo, MediaType
router = APIRouter() router = APIRouter()
@ -18,6 +21,28 @@ async def read_downloading(
return DownloadChain().downloading() return DownloadChain().downloading()
@router.post("/notexists", summary="查询电视剧缺失的剧集", response_model=List[NotExistMediaInfo])
async def exists(media_in: schemas.MediaInfo,
_: schemas.TokenPayload = Depends(verify_token)) -> Any:
"""
查询已存在的媒体信息
"""
# 媒体信息
mediainfo = MediaInfo()
mediainfo.from_dict(media_in.dict())
# 元数据
meta = MetaInfo(title=mediainfo.title)
# 查询缺失信息
exist_flag, no_exists = DownloadChain().get_no_exists_info(meta=meta, mediainfo=mediainfo)
if mediainfo.type == MediaType.MOVIE:
# 电影已存在时返回空列表,存在时返回空对像列表
return [] if exist_flag else [NotExistMediaInfo()]
elif no_exists and no_exists.get(mediainfo.tmdb_id):
# 电视剧返回缺失的剧集
return list(no_exists.get(mediainfo.tmdb_id).values())
return []
@router.put("/{hashString}/start", summary="开始任务", response_model=schemas.Response) @router.put("/{hashString}/start", summary="开始任务", response_model=schemas.Response)
async def start_downloading( async def start_downloading(
hashString: str, hashString: str,

View File

@ -479,6 +479,8 @@ class DownloadChain(ChainBase):
if not exists_tvs: if not exists_tvs:
# 所有剧集均缺失 # 所有剧集均缺失
for season, episodes in mediainfo.seasons.items(): for season, episodes in mediainfo.seasons.items():
if not episodes:
continue
# 全季不存在 # 全季不存在
if meta.begin_season \ if meta.begin_season \
and season not in meta.season_list: and season not in meta.season_list:
@ -494,12 +496,12 @@ class DownloadChain(ChainBase):
exist_seasons = exists_tvs.seasons exist_seasons = exists_tvs.seasons
if exist_seasons.get(season): if exist_seasons.get(season):
# 取差集 # 取差集
episodes = list(set(episodes).difference(set(exist_seasons[season]))) lack_episodes = list(set(episodes).difference(set(exist_seasons[season])))
if not episodes: if not lack_episodes:
# 全部集存在 # 全部集存在
continue continue
# 添加不存在的季集信息 # 添加不存在的季集信息
__append_no_exists(_season=season, _episodes=episodes, __append_no_exists(_season=season, _episodes=lack_episodes,
_total=len(episodes), _start=min(episodes)) _total=len(episodes), _start=min(episodes))
else: else:
# 全季不存在 # 全季不存在
@ -507,7 +509,7 @@ class DownloadChain(ChainBase):
_total=len(episodes), _start=min(episodes)) _total=len(episodes), _start=min(episodes))
# 存在不完整的剧集 # 存在不完整的剧集
if no_exists: if no_exists:
logger.info(f"媒体库中已存在部分剧集,缺失:{no_exists}") logger.debug(f"媒体库中已存在部分剧集,缺失:{no_exists}")
return False, no_exists return False, no_exists
# 全部存在 # 全部存在
return True, no_exists return True, no_exists

View File

@ -21,8 +21,8 @@ class Settings(BaseSettings):
HOST: str = "0.0.0.0" HOST: str = "0.0.0.0"
# API监听端口 # API监听端口
PORT: int = 3001 PORT: int = 3001
# 是否自动重载 # 是否调试模式
RELOAD: bool = False DEBUG: bool = False
# 配置文件目录 # 配置文件目录
CONFIG_DIR: str = None CONFIG_DIR: str = None
# 超级管理员 # 超级管理员

View File

@ -173,6 +173,19 @@ class MediaInfo:
def __setattr__(self, name: str, value: Any): def __setattr__(self, name: str, value: Any):
self.__dict__[name] = value self.__dict__[name] = value
def from_dict(self, data: dict):
"""
从字典中初始化
"""
for key, value in data.items():
if key == "type":
if value == MediaType.MOVIE.value:
setattr(self, key, MediaType.MOVIE)
elif value == MediaType.TV.value:
setattr(self, key, MediaType.TV)
continue
setattr(self, key, value)
def set_image(self, name: str, image: str): def set_image(self, name: str, image: str):
""" """
设置图片地址 设置图片地址

View File

@ -26,7 +26,7 @@ App.add_middleware(
) )
# uvicorn服务 # uvicorn服务
Server = uvicorn.Server(Config(App, host=settings.HOST, port=settings.PORT, reload=settings.RELOAD)) Server = uvicorn.Server(Config(App, host=settings.HOST, port=settings.PORT, reload=settings.DEBUG))
def init_routers(): def init_routers():

View File

@ -37,6 +37,9 @@ class Scheduler(metaclass=Singleton):
}) })
def __init__(self): def __init__(self):
# 调试模式不启动定时服务
if settings.DEBUG:
return
# CookieCloud定时同步 # CookieCloud定时同步
if settings.COOKIECLOUD_INTERVAL: if settings.COOKIECLOUD_INTERVAL:
self._scheduler.add_job(CookieCloudChain().process, self._scheduler.add_job(CookieCloudChain().process,

View File

@ -245,13 +245,13 @@ class NotExistMediaInfo(BaseModel):
媒体服务器不存在媒体信息 媒体服务器不存在媒体信息
""" """
# 季 # 季
season: int season: Optional[int] = None
# 剧集列表 # 剧集列表
episodes: list = [] episodes: Optional[list] = []
# 总集数 # 总集数
total_episodes: int = 0 total_episodes: Optional[int] = 0
# 开始集 # 开始集
start_episode: int = 0 start_episode: Optional[int] = 0
class RefreshMediaItem(BaseModel): class RefreshMediaItem(BaseModel):