From e1f19b2ce0857881eabc4c9e0ddb76c98f6ac814 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Wed, 14 Jun 2023 12:45:59 +0800 Subject: [PATCH] fix --- app/api/servarr.py | 21 +++++++++++++-------- app/chain/__init__.py | 3 +++ app/core/config.py | 2 ++ app/modules/__init__.py | 8 ++++++++ app/modules/douban/__init__.py | 1 + app/modules/thetvdb/__init__.py | 33 +++++++++++++++++++++++++++++++++ requirements.txt | 3 ++- 7 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 app/modules/thetvdb/__init__.py diff --git a/app/api/servarr.py b/app/api/servarr.py index 665d174b..80535634 100644 --- a/app/api/servarr.py +++ b/app/api/servarr.py @@ -4,6 +4,7 @@ from fastapi import APIRouter, HTTPException, Depends, Request from requests import Session from app import schemas +from app.chain.identify import IdentifyChain from app.chain.subscribe import SubscribeChain from app.core.config import settings from app.db import get_db @@ -468,21 +469,27 @@ async def arr_series_lookup(apikey: str, term: str, db: Session = Depends(get_db status_code=403, detail="认证失败!", ) + # 季列表 + seasons = [] if not term.startswith("tvdb:"): title = term subscribe = Subscribe.get_by_title(db, title) else: tmdbid = term.replace("tvdb:", "") subscribe = Subscribe.get_by_tmdbid(db, int(tmdbid)) + if not subscribe: + # 查询TMDB季信息 + tmdbinfo = IdentifyChain().tvdb_info(tvdbid=int(tmdbid)) + if tmdbinfo: + season_num = tmdbinfo.get('season') + if season_num: + seasons = list(range(1, season_num + 1)) if subscribe: return [SonarrSeries( id=subscribe.id, title=subscribe.name, seasonCount=1, - seasons=[{ - "seasonNumber": subscribe.season, - "monitored": True, - }], + seasons=[subscribe.season], year=subscribe.year, isAvailable=True, monitored=True, @@ -493,7 +500,7 @@ async def arr_series_lookup(apikey: str, term: str, db: Session = Depends(get_db hasFile=False, )] else: - return [SonarrSeries()] + return [SonarrSeries(seasons=seasons)] @arr_router.get("/series/{tid}") @@ -534,7 +541,7 @@ async def arr_serie(apikey: str, tid: int, db: Session = Depends(get_db)) -> Any @arr_router.post("/series") -async def arr_add_series(request: Request, apikey: str) -> Any: +async def arr_add_series(request: Request, apikey: str, tv: schemas.SonarrSeries) -> Any: """ 新增Sonarr剧集订阅 """ @@ -544,7 +551,6 @@ async def arr_add_series(request: Request, apikey: str) -> Any: detail="认证失败!", ) logger.info(await request.body()) - ''' sid = 0 for season in tv.seasons: sid = SubscribeChain().process(title=tv.title, @@ -562,7 +568,6 @@ async def arr_add_series(request: Request, apikey: str) -> Any: status_code=500, detail="添加订阅失败!" ) - ''' @arr_router.delete("/series/{tid}") diff --git a/app/chain/__init__.py b/app/chain/__init__.py index 1b6f88be..22175458 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -80,6 +80,9 @@ class ChainBase(AbstractSingleton, metaclass=Singleton): def douban_info(self, doubanid: str) -> Optional[dict]: return self.run_module("douban_info", doubanid=doubanid) + def tvdb_info(self, tvdbid: int) -> Optional[dict]: + return self.run_module("tvdb_info", tvdbid=tvdbid) + def message_parser(self, body: Any, form: Any, args: Any) -> Optional[dict]: return self.run_module("message_parser", body=body, form=form, args=args) diff --git a/app/core/config.py b/app/core/config.py index 879b663c..5e464535 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -41,6 +41,8 @@ class Settings(BaseSettings): TMDB_API_DOMAIN: str = "api.themoviedb.org" # TMDB API Key TMDB_API_KEY: str = "db55323b8d3e4154498498a75642b381" + # TVDB API Key + TVDB_API_KEY: str = "6b481081-10aa-440c-99f2-21d17717ee02" # Fanart API Key FANART_API_KEY: str = "d2d31f9ecabea050fc7d68aa3146015f" # 支持的后缀格式 diff --git a/app/modules/__init__.py b/app/modules/__init__.py index 45b5f3d9..f86ed846 100644 --- a/app/modules/__init__.py +++ b/app/modules/__init__.py @@ -68,6 +68,14 @@ class _ModuleBase(metaclass=ABCMeta): """ pass + def tvdb_info(self, tvdbid: int) -> Optional[dict]: + """ + 获取TVDB信息 + :param tvdbid: int + :return: 识别的媒体信息,包括剧集信息 + """ + pass + def message_parser(self, body: Any, form: Any, args: Any) -> Optional[dict]: """ 解析消息内容,返回字典,注意以下约定值: diff --git a/app/modules/douban/__init__.py b/app/modules/douban/__init__.py index 3c20bf58..53416d5b 100644 --- a/app/modules/douban/__init__.py +++ b/app/modules/douban/__init__.py @@ -37,6 +37,7 @@ class DoubanModule(_ModuleBase): """ if not doubanid: return None + logger.info(f"开始获取豆瓣信息:{doubanid} ...") douban_info = self.doubanapi.movie_detail(doubanid) if douban_info: celebrities = self.doubanapi.movie_celebrities(doubanid) diff --git a/app/modules/thetvdb/__init__.py b/app/modules/thetvdb/__init__.py new file mode 100644 index 00000000..650b19ed --- /dev/null +++ b/app/modules/thetvdb/__init__.py @@ -0,0 +1,33 @@ +from typing import Optional, Tuple, Union + +import tvdb_api + +from app.core.config import settings +from app.log import logger +from app.modules import _ModuleBase + + +class TheTvDbModule(_ModuleBase): + + tvdb: tvdb_api.Tvdb = None + + def init_module(self) -> None: + self.tvdb = tvdb_api.Tvdb(apikey=settings.TVDB_API_KEY) + + def stop(self): + pass + + def init_setting(self) -> Tuple[str, Union[str, bool]]: + pass + + def tvdb_info(self, tvdbid: int) -> Optional[dict]: + """ + 获取TVDB信息 + :param tvdbid: int + :return: 识别的媒体信息,包括剧集信息 + """ + try: + logger.info(f"开始获取TVDB信息: {tvdbid} ...") + return self.tvdb[tvdbid].data + except Exception as err: + logger.error(f"获取TVDB信息失败: {err}") diff --git a/requirements.txt b/requirements.txt index 25d8c1a0..bb1daf9d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -41,4 +41,5 @@ slack_sdk~=3.21.3 chardet~=4.0.0 starlette~=0.27.0 PyVirtualDisplay~=3.0 -Cython~=0.29.35 \ No newline at end of file +Cython~=0.29.35 +tvdb_api~=3.1 \ No newline at end of file