diff --git a/app/api/endpoints/subscribes.py b/app/api/endpoints/subscribes.py index 22711b5a..8e5487c4 100644 --- a/app/api/endpoints/subscribes.py +++ b/app/api/endpoints/subscribes.py @@ -15,12 +15,12 @@ from app.utils.types import MediaType router = APIRouter() -def start_subscribe_chain(title: str, +def start_subscribe_chain(title: str, year: str, mtype: MediaType, tmdbid: int, season: int, username: str): """ 启动订阅链式任务 """ - SubscribeChain().process(title=title, + SubscribeChain().process(title=title, year=year, mtype=mtype, tmdbid=tmdbid, season=season, username=username) @@ -113,6 +113,7 @@ async def seerr_subscribe(request: Request, background_tasks: BackgroundTasks, mtype=media_type, tmdbid=tmdbId, title=subject, + year="", season=0, username=user_name) else: @@ -126,6 +127,7 @@ async def seerr_subscribe(request: Request, background_tasks: BackgroundTasks, mtype=media_type, tmdbid=tmdbId, title=subject, + year="", season=season, username=user_name) diff --git a/app/api/servarr.py b/app/api/servarr.py index cc570330..0695d875 100644 --- a/app/api/servarr.py +++ b/app/api/servarr.py @@ -4,10 +4,12 @@ from fastapi import APIRouter, HTTPException, Depends from requests import Session from app import schemas +from app.chain.subscribe import SubscribeChain from app.core.config import settings from app.db import get_db from app.db.models.subscribe import Subscribe from app.schemas import RadarrMovie +from app.utils.types import MediaType from version import APP_VERSION arr_router = APIRouter() @@ -214,6 +216,37 @@ async def arr_movies(apikey: str, db: Session = Depends(get_db)) -> Any: return result +@arr_router.get("/movie/lookup", response_model=List[schemas.RadarrMovie]) +async def arr_movie_lookup(apikey: str, term: str, db: Session = Depends(get_db)) -> Any: + """ + 查询Rardar电影 term: `tmdb:${id}` + """ + if not apikey or apikey != settings.API_TOKEN: + raise HTTPException( + status_code=403, + detail="认证失败!", + ) + tmdbid = term.replace("tmdb:", "") + subscribe = Subscribe.get_by_tmdbid(db, int(tmdbid)) + if subscribe: + return [RadarrMovie( + id=subscribe.id, + title=subscribe.name, + isAvailable=True, + monitored=True, + tmdbId=subscribe.tmdbid, + profileId=1, + qualityProfileId=1, + added=True, + hasFile=False, + )] + else: + raise HTTPException( + status_code=404, + detail="未找到该电影!" + ) + + @arr_router.get("/movie/{mid}", response_model=schemas.RadarrMovie) async def arr_movie(apikey: str, mid: int, db: Session = Depends(get_db)) -> Any: """ @@ -244,39 +277,8 @@ async def arr_movie(apikey: str, mid: int, db: Session = Depends(get_db)) -> Any ) -@arr_router.get("/movie/lookup", response_model=List[schemas.RadarrMovie]) -async def arr_movie_lookup(apikey: str, term: str, db: Session = Depends(get_db)) -> Any: - """ - 查询Rardar电影 term: `tmdb:${id}` - """ - if not apikey or apikey != settings.API_TOKEN: - raise HTTPException( - status_code=403, - detail="认证失败!", - ) - tmdbid = term.replace("tmdb:", "") - subscribe = Subscribe.get_by_tmdbid(db, int(tmdbid)) - if subscribe: - return RadarrMovie( - id=subscribe.id, - title=subscribe.name, - isAvailable=True, - monitored=True, - tmdbId=subscribe.tmdbid, - profileId=1, - qualityProfileId=1, - added=True, - hasFile=False, - ) - else: - raise HTTPException( - status_code=404, - detail="未找到该电影!" - ) - - @arr_router.put("/movie", response_model=schemas.Response) -async def arr_add_movie(apikey: str, title: str, tmdbId: int, year: int) -> Any: +async def arr_add_movie(apikey: str, title: str, tmdbId: int, year: str) -> Any: """ 新增Rardar电影订阅 """ @@ -285,6 +287,13 @@ async def arr_add_movie(apikey: str, title: str, tmdbId: int, year: int) -> Any: status_code=403, detail="认证失败!", ) + if SubscribeChain().process(title=title, year=year, mtype=MediaType.MOVIE, tmdbid=tmdbId): + return {"success": True, "msg": "添加订阅成功!"} + else: + raise HTTPException( + status_code=500, + detail="添加订阅失败!" + ) @arr_router.delete("/movie/{mid}", response_model=schemas.Response) diff --git a/app/chain/douban_sync.py b/app/chain/douban_sync.py index 73c33255..bb86aa9a 100644 --- a/app/chain/douban_sync.py +++ b/app/chain/douban_sync.py @@ -96,6 +96,7 @@ class DoubanSyncChain(ChainBase): logger.info(f'{mediainfo.get_title_string()} 未下载未完整,添加订阅 ...') # 添加订阅 self.subscribechain.process(title=mediainfo.title, + year=mediainfo.year, mtype=mediainfo.type, tmdbid=mediainfo.tmdb_id, season=meta.begin_season, diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index b90833a3..2945114b 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -28,7 +28,7 @@ class SubscribeChain(ChainBase): self.subscribes = Subscribes() self.siteshelper = SitesHelper() - def process(self, title: str, + def process(self, title: str, year: str, mtype: MediaType = None, tmdbid: int = None, season: int = None, @@ -45,6 +45,8 @@ class SubscribeChain(ChainBase): title, _ = result # 识别元数据 metainfo = MetaInfo(title) + if year: + metainfo.year = year if mtype: metainfo.type = mtype if season: @@ -305,6 +307,7 @@ class SubscribeChain(ChainBase): if no_exists \ and no_exists.get(tmdb_id) \ and (total_episode or start_episode): + index = 0 for no_exist in no_exists.get(tmdb_id): # 替换原季值 if no_exist.get("season") == begin_season: @@ -315,7 +318,7 @@ class SubscribeChain(ChainBase): if total_episode and start_episode: # 有开始集和总集数 episodes = list(range(start_episode, total_episode + 1)) - no_exist = { + no_exists[tmdb_id][index] = { "season": begin_season, "episodes": episodes, "total_episodes": total_episode, @@ -324,7 +327,7 @@ class SubscribeChain(ChainBase): elif not start_episode: # 有总集数没有开始集 episodes = list(range(min(episode_list or [1]), total_episode + 1)) - no_exist = { + no_exists[tmdb_id][index] = { "season": begin_season, "episodes": episodes, "total_episodes": total_episode, @@ -333,10 +336,11 @@ class SubscribeChain(ChainBase): elif not total_episode: # 有开始集没有总集数 episodes = list(range(start_episode, max(episode_list or [total]) + 1)) - no_exist = { + no_exists[tmdb_id][index] = { "season": begin_season, "episodes": episodes, "total_episodes": max(episode_list or [total]), "start_episode": start_episode } + index += 1 return no_exists diff --git a/app/chain/user_message.py b/app/chain/user_message.py index 0b27b773..10b35943 100644 --- a/app/chain/user_message.py +++ b/app/chain/user_message.py @@ -129,6 +129,7 @@ class UserMessageChain(ChainBase): # 订阅媒体 mediainfo: MediaInfo = cache_list[int(text) - 1] self.subscribechain.process(title=mediainfo.title, + year=mediainfo.year, mtype=mediainfo.type, tmdbid=mediainfo.tmdb_id, season=self._current_meta.begin_season, @@ -157,6 +158,7 @@ class UserMessageChain(ChainBase): logger.info(f'{self._current_media.get_title_string()} 未下载未完整,添加订阅 ...') # 添加订阅 self.subscribechain.process(title=self._current_media.title, + year=self._current_media.year, mtype=self._current_media.type, tmdbid=self._current_media.tmdb_id, season=self._current_meta.begin_season,