feat: add tmdb poster.

This commit is contained in:
EstrellaXD
2023-09-06 23:48:39 +08:00
parent 3bb4d6280b
commit 2e4cf0d621
2 changed files with 19 additions and 12 deletions

View File

@@ -6,6 +6,9 @@ from module.conf import TMDB_API
from module.network import RequestContent
TMDB_URL = "https://api.themoviedb.org"
@dataclass
class TMDBInfo:
id: int
@@ -14,17 +17,18 @@ class TMDBInfo:
season: list[dict]
last_season: int
year: str
poster_link: str = None
LANGUAGE = {"zh": "zh-CN", "jp": "ja-JP", "en": "en-US"}
def search_url(e):
return f"https://api.themoviedb.org/3/search/tv?api_key={TMDB_API}&page=1&query={e}&include_adult=false"
return f"{TMDB_URL}/3/search/tv?api_key={TMDB_API}&page=1&query={e}&include_adult=false"
def info_url(e, key):
return f"https://api.themoviedb.org/3/tv/{e}?api_key={TMDB_API}&language={LANGUAGE[key]}"
return f"{TMDB_URL}/3/tv/{e}?api_key={TMDB_API}&language={LANGUAGE[key]}"
def is_animation(tv_id, language) -> bool:
@@ -37,7 +41,7 @@ def is_animation(tv_id, language) -> bool:
return False
def get_season(seasons: list) -> int:
def get_season(seasons: list) -> tuple[int, str]:
ss = sorted(seasons, key=lambda e: e.get("air_date"), reverse=True)
for season in ss:
if re.search(r"\d 季", season.get("season")) is not None:
@@ -45,7 +49,7 @@ def get_season(seasons: list) -> int:
[year, _, _] = date
now_year = time.localtime().tm_year
if int(year) <= now_year:
return int(re.findall(r"\d", season.get("season"))[0])
return int(re.findall(r"\d", season.get("season"))[0]), season.get("poster_path")
def tmdb_parser(title, language) -> TMDBInfo | None:
@@ -71,10 +75,11 @@ def tmdb_parser(title, language) -> TMDBInfo | None:
}
for s in info_content.get("seasons")
]
last_season = get_season(season)
last_season, poster_path = get_season(season)
original_title = info_content.get("original_name")
official_title = info_content.get("name")
year_number = info_content.get("first_air_date").split("-")[0]
poster_link = "https://image.tmdb.org/t/p/w300" + poster_path
return TMDBInfo(
id,
official_title,
@@ -82,6 +87,11 @@ def tmdb_parser(title, language) -> TMDBInfo | None:
season,
last_season,
str(year_number),
poster_link,
)
else:
return None
if __name__ == '__main__':
print(tmdb_parser("魔法禁书目录", "zh"))

View File

@@ -26,17 +26,15 @@ class TitleParser:
@staticmethod
def tmdb_parser(title: str, season: int, language: str):
official_title, tmdb_season, year = title, season, None
tmdb_info = tmdb_parser(title, language)
if tmdb_info:
logger.debug(f"TMDB Matched, official title is {tmdb_info.title}")
tmdb_season = tmdb_info.last_season if tmdb_info.last_season else season
official_title = tmdb_info.title
year = tmdb_info.year
return tmdb_info.title, tmdb_season, tmdb_info.year, tmdb_info.poster_link
else:
logger.warning(f"Cannot match {title} in TMDB. Use raw title instead.")
logger.warning("Please change bangumi info manually.")
return official_title, tmdb_season, year
return title, season, None, None
@staticmethod
def raw_parser(raw: str) -> Bangumi | None:
@@ -60,7 +58,8 @@ class TitleParser:
else:
official_title = title_raw
_season = episode.season
data = Bangumi(
logger.debug(f"RAW:{raw} >> {title_raw}")
return Bangumi(
official_title=official_title,
title_raw=title_raw,
season=_season,
@@ -73,8 +72,6 @@ class TitleParser:
offset=0,
filter=",".join(settings.rss_parser.filter),
)
logger.debug(f"RAW:{raw} >> {title_raw}")
return data
except Exception as e:
logger.debug(e)
logger.warning(f"Cannot parse {raw}.")