diff --git a/app/api/endpoints/download.py b/app/api/endpoints/download.py index 7475cf2d..7717caeb 100644 --- a/app/api/endpoints/download.py +++ b/app/api/endpoints/download.py @@ -4,7 +4,10 @@ from fastapi import APIRouter, Depends from app import schemas 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.schemas import NotExistMediaInfo, MediaType router = APIRouter() @@ -18,6 +21,28 @@ async def read_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) async def start_downloading( hashString: str, diff --git a/app/api/endpoints/media.py b/app/api/endpoints/media.py index 4053c119..0c1d6918 100644 --- a/app/api/endpoints/media.py +++ b/app/api/endpoints/media.py @@ -31,5 +31,5 @@ async def search_by_title(title: str, """ _, medias = MediaChain().search(title=title) if medias: - return [media.to_dict() for media in medias[(page-1) * count: page * count]] + return [media.to_dict() for media in medias[(page - 1) * count: page * count]] return [] diff --git a/app/chain/download.py b/app/chain/download.py index fe0fd081..9093e130 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -479,6 +479,8 @@ class DownloadChain(ChainBase): if not exists_tvs: # 所有剧集均缺失 for season, episodes in mediainfo.seasons.items(): + if not episodes: + continue # 全季不存在 if meta.begin_season \ and season not in meta.season_list: @@ -494,12 +496,12 @@ class DownloadChain(ChainBase): exist_seasons = exists_tvs.seasons if exist_seasons.get(season): # 取差集 - episodes = list(set(episodes).difference(set(exist_seasons[season]))) - if not episodes: + lack_episodes = list(set(episodes).difference(set(exist_seasons[season]))) + if not lack_episodes: # 全部集存在 continue # 添加不存在的季集信息 - __append_no_exists(_season=season, _episodes=episodes, + __append_no_exists(_season=season, _episodes=lack_episodes, _total=len(episodes), _start=min(episodes)) else: # 全季不存在 @@ -507,7 +509,7 @@ class DownloadChain(ChainBase): _total=len(episodes), _start=min(episodes)) # 存在不完整的剧集 if no_exists: - logger.info(f"媒体库中已存在部分剧集,缺失:{no_exists}") + logger.debug(f"媒体库中已存在部分剧集,缺失:{no_exists}") return False, no_exists # 全部存在 return True, no_exists diff --git a/app/core/config.py b/app/core/config.py index a369b873..40303c3c 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -21,8 +21,8 @@ class Settings(BaseSettings): HOST: str = "0.0.0.0" # API监听端口 PORT: int = 3001 - # 是否自动重载 - RELOAD: bool = False + # 是否调试模式 + DEBUG: bool = False # 配置文件目录 CONFIG_DIR: str = None # 超级管理员 diff --git a/app/core/context.py b/app/core/context.py index b45af685..e6b088e6 100644 --- a/app/core/context.py +++ b/app/core/context.py @@ -173,6 +173,19 @@ class MediaInfo: def __setattr__(self, name: str, value: Any): 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): """ 设置图片地址 diff --git a/app/main.py b/app/main.py index cfca054e..c57d677d 100644 --- a/app/main.py +++ b/app/main.py @@ -26,7 +26,7 @@ App.add_middleware( ) # 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(): diff --git a/app/scheduler.py b/app/scheduler.py index dc998098..b27c2370 100644 --- a/app/scheduler.py +++ b/app/scheduler.py @@ -37,6 +37,9 @@ class Scheduler(metaclass=Singleton): }) def __init__(self): + # 调试模式不启动定时服务 + if settings.DEBUG: + return # CookieCloud定时同步 if settings.COOKIECLOUD_INTERVAL: self._scheduler.add_job(CookieCloudChain().process, diff --git a/app/schemas/context.py b/app/schemas/context.py index a1152ab0..849edc86 100644 --- a/app/schemas/context.py +++ b/app/schemas/context.py @@ -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):