From 42cd35ab3c296032d21b71b22640917356efd279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=AF=E5=A4=A7=E4=BE=A0?= Date: Wed, 29 Jan 2025 14:10:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(TMDB):=20=E5=A2=9E=E5=8A=A0=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E5=88=86=E7=BA=A7=E7=9A=84=E5=88=AE=E5=89=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/core/context.py | 2 + app/modules/themoviedb/scraper.py | 3 ++ app/modules/themoviedb/tmdbapi.py | 66 +++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/app/core/context.py b/app/core/context.py index 6fd42f67..bf869a57 100644 --- a/app/core/context.py +++ b/app/core/context.py @@ -262,6 +262,8 @@ class MediaInfo: runtime: int = None # 下一集 next_episode_to_air: dict = field(default_factory=dict) + # 内容分级 + content_rating: str = None def __post_init__(self): # 设置媒体信息 diff --git a/app/modules/themoviedb/scraper.py b/app/modules/themoviedb/scraper.py index bb79f80b..d0535375 100644 --- a/app/modules/themoviedb/scraper.py +++ b/app/modules/themoviedb/scraper.py @@ -170,6 +170,9 @@ class TmdbScraper: DomUtils.add_node(doc, root, "genre", genre.get("name") or "") # 评分 DomUtils.add_node(doc, root, "rating", mediainfo.vote_average or "0") + # 内容分级 + if content_rating := mediainfo.content_rating: + DomUtils.add_node(doc, root, "mpaa", content_rating) return doc diff --git a/app/modules/themoviedb/tmdbapi.py b/app/modules/themoviedb/tmdbapi.py index bfcb9f5e..391ecdfe 100644 --- a/app/modules/themoviedb/tmdbapi.py +++ b/app/modules/themoviedb/tmdbapi.py @@ -601,6 +601,8 @@ class TmdbApi: tmdb_info['genre_ids'] = __get_genre_ids(tmdb_info.get('genres')) # 别名和译名 tmdb_info['names'] = self.__get_names(tmdb_info) + # 内容分级 + tmdb_info['content_rating'] = self.__get_content_rating(tmdb_info) # 转换多语种标题 self.__update_tmdbinfo_extra_title(tmdb_info) # 转换中文标题 @@ -608,6 +610,68 @@ class TmdbApi: return tmdb_info + @staticmethod + def __get_content_rating(tmdb_info: dict) -> Optional[str]: + """ + 获得tmdb中的内容评级 + :param tmdb_info: TMDB信息 + :return: 内容评级 + """ + if not tmdb_info: + return None + # dict[地区:分级] + ratings = {} + if results := (tmdb_info.get("release_dates") or {}).get("results"): + """ + [ + { + "iso_3166_1": "AR", + "release_dates": [ + { + "certification": "+13", + "descriptors": [], + "iso_639_1": "", + "note": "", + "release_date": "2025-01-23T00:00:00.000Z", + "type": 3 + } + ] + } + ] + """ + for item in results: + iso_3166_1 = item.get("iso_3166_1") + if not iso_3166_1: + continue + dates = item.get("release_dates") + if not dates: + continue + certification = dates[0].get("certification") + if not certification: + continue + ratings[iso_3166_1] = certification + elif results := (tmdb_info.get("content_ratings") or {}).get("results"): + """ + [ + { + "descriptors": [], + "iso_3166_1": "US", + "rating": "TV-MA" + } + ] + """ + for item in results: + iso_3166_1 = item.get("iso_3166_1") + if not iso_3166_1: + continue + rating = item.get("rating") + if not rating: + continue + ratings[iso_3166_1] = rating + if not ratings: + return None + return ratings.get("CN") or ratings.get("US") + @staticmethod def __update_tmdbinfo_cn_title(tmdb_info: dict): """ @@ -700,6 +764,7 @@ class TmdbApi: "credits," "alternative_titles," "translations," + "release_dates," "external_ids") -> Optional[dict]: """ 获取电影的详情 @@ -812,6 +877,7 @@ class TmdbApi: "credits," "alternative_titles," "translations," + "content_ratings," "external_ids") -> Optional[dict]: """ 获取电视剧的详情