feat: write all required NFO tags on creation
This commit is contained in:
@@ -15,16 +15,10 @@ from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from lxml import etree
|
||||
|
||||
from src.core.entities.nfo_models import (
|
||||
ActorInfo,
|
||||
ImageInfo,
|
||||
RatingInfo,
|
||||
TVShowNFO,
|
||||
UniqueID,
|
||||
)
|
||||
from src.core.services.tmdb_client import TMDBAPIError, TMDBClient
|
||||
from src.core.utils.image_downloader import ImageDownloader
|
||||
from src.core.utils.nfo_generator import generate_tvshow_nfo
|
||||
from src.core.utils.nfo_mapper import tmdb_to_nfo_model
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -176,7 +170,12 @@ class NFOService:
|
||||
content_ratings = await self.tmdb_client.get_tv_show_content_ratings(tv_id)
|
||||
|
||||
# Convert TMDB data to TVShowNFO model
|
||||
nfo_model = self._tmdb_to_nfo_model(details, content_ratings)
|
||||
nfo_model = tmdb_to_nfo_model(
|
||||
details,
|
||||
content_ratings,
|
||||
self.tmdb_client.get_image_url,
|
||||
self.image_size,
|
||||
)
|
||||
|
||||
# Generate XML
|
||||
nfo_xml = generate_tvshow_nfo(nfo_model)
|
||||
@@ -266,7 +265,12 @@ class NFOService:
|
||||
content_ratings = await self.tmdb_client.get_tv_show_content_ratings(tmdb_id)
|
||||
|
||||
# Convert TMDB data to TVShowNFO model
|
||||
nfo_model = self._tmdb_to_nfo_model(details, content_ratings)
|
||||
nfo_model = tmdb_to_nfo_model(
|
||||
details,
|
||||
content_ratings,
|
||||
self.tmdb_client.get_image_url,
|
||||
self.image_size,
|
||||
)
|
||||
|
||||
# Generate XML
|
||||
nfo_xml = generate_tvshow_nfo(nfo_model)
|
||||
@@ -398,137 +402,7 @@ class NFOService:
|
||||
# Return first result (usually best match)
|
||||
return results[0]
|
||||
|
||||
def _tmdb_to_nfo_model(
|
||||
self,
|
||||
tmdb_data: Dict[str, Any],
|
||||
content_ratings: Optional[Dict[str, Any]] = None
|
||||
) -> TVShowNFO:
|
||||
"""Convert TMDB API data to TVShowNFO model.
|
||||
|
||||
Args:
|
||||
tmdb_data: TMDB TV show details
|
||||
content_ratings: TMDB content ratings data
|
||||
|
||||
Returns:
|
||||
TVShowNFO Pydantic model
|
||||
"""
|
||||
# Extract basic info
|
||||
title = tmdb_data["name"]
|
||||
original_title = tmdb_data.get("original_name", title)
|
||||
year = None
|
||||
if tmdb_data.get("first_air_date"):
|
||||
year = int(tmdb_data["first_air_date"][:4])
|
||||
|
||||
# Extract ratings
|
||||
ratings = []
|
||||
if tmdb_data.get("vote_average"):
|
||||
ratings.append(RatingInfo(
|
||||
name="themoviedb",
|
||||
value=float(tmdb_data["vote_average"]),
|
||||
votes=tmdb_data.get("vote_count", 0),
|
||||
max_rating=10,
|
||||
default=True
|
||||
))
|
||||
|
||||
# Extract external IDs
|
||||
external_ids = tmdb_data.get("external_ids", {})
|
||||
imdb_id = external_ids.get("imdb_id")
|
||||
tvdb_id = external_ids.get("tvdb_id")
|
||||
|
||||
# Extract images
|
||||
thumb_images = []
|
||||
fanart_images = []
|
||||
|
||||
# Poster
|
||||
if tmdb_data.get("poster_path"):
|
||||
poster_url = self.tmdb_client.get_image_url(
|
||||
tmdb_data["poster_path"],
|
||||
self.image_size
|
||||
)
|
||||
thumb_images.append(ImageInfo(url=poster_url, aspect="poster"))
|
||||
|
||||
# Backdrop/Fanart
|
||||
if tmdb_data.get("backdrop_path"):
|
||||
fanart_url = self.tmdb_client.get_image_url(
|
||||
tmdb_data["backdrop_path"],
|
||||
self.image_size
|
||||
)
|
||||
fanart_images.append(ImageInfo(url=fanart_url))
|
||||
|
||||
# Logo from images if available
|
||||
images_data = tmdb_data.get("images", {})
|
||||
logos = images_data.get("logos", [])
|
||||
if logos:
|
||||
logo_url = self.tmdb_client.get_image_url(
|
||||
logos[0]["file_path"],
|
||||
self.image_size
|
||||
)
|
||||
thumb_images.append(ImageInfo(url=logo_url, aspect="clearlogo"))
|
||||
|
||||
# Extract cast
|
||||
actors = []
|
||||
credits = tmdb_data.get("credits", {})
|
||||
for cast_member in credits.get("cast", [])[:10]: # Top 10 actors
|
||||
actor_thumb = None
|
||||
if cast_member.get("profile_path"):
|
||||
actor_thumb = self.tmdb_client.get_image_url(
|
||||
cast_member["profile_path"],
|
||||
"h632"
|
||||
)
|
||||
|
||||
actors.append(ActorInfo(
|
||||
name=cast_member["name"],
|
||||
role=cast_member.get("character"),
|
||||
thumb=actor_thumb,
|
||||
tmdbid=cast_member["id"]
|
||||
))
|
||||
|
||||
# Create unique IDs
|
||||
unique_ids = []
|
||||
if tmdb_data.get("id"):
|
||||
unique_ids.append(UniqueID(
|
||||
type="tmdb",
|
||||
value=str(tmdb_data["id"]),
|
||||
default=False
|
||||
))
|
||||
if imdb_id:
|
||||
unique_ids.append(UniqueID(
|
||||
type="imdb",
|
||||
value=imdb_id,
|
||||
default=False
|
||||
))
|
||||
if tvdb_id:
|
||||
unique_ids.append(UniqueID(
|
||||
type="tvdb",
|
||||
value=str(tvdb_id),
|
||||
default=True
|
||||
))
|
||||
|
||||
# Extract FSK rating from content ratings
|
||||
fsk_rating = self._extract_fsk_rating(content_ratings) if content_ratings else None
|
||||
|
||||
# Create NFO model
|
||||
return TVShowNFO(
|
||||
title=title,
|
||||
originaltitle=original_title,
|
||||
year=year,
|
||||
plot=tmdb_data.get("overview"),
|
||||
runtime=tmdb_data.get("episode_run_time", [None])[0] if tmdb_data.get("episode_run_time") else None,
|
||||
premiered=tmdb_data.get("first_air_date"),
|
||||
status=tmdb_data.get("status"),
|
||||
genre=[g["name"] for g in tmdb_data.get("genres", [])],
|
||||
studio=[n["name"] for n in tmdb_data.get("networks", [])],
|
||||
country=[c["name"] for c in tmdb_data.get("production_countries", [])],
|
||||
ratings=ratings,
|
||||
fsk=fsk_rating,
|
||||
tmdbid=tmdb_data.get("id"),
|
||||
imdbid=imdb_id,
|
||||
tvdbid=tvdb_id,
|
||||
uniqueid=unique_ids,
|
||||
thumb=thumb_images,
|
||||
fanart=fanart_images,
|
||||
actors=actors
|
||||
)
|
||||
|
||||
|
||||
async def _download_media_files(
|
||||
self,
|
||||
@@ -590,49 +464,7 @@ class NFOService:
|
||||
logger.info(f"Media download results: {results}")
|
||||
return results
|
||||
|
||||
def _extract_fsk_rating(self, content_ratings: Dict[str, Any]) -> Optional[str]:
|
||||
"""Extract German FSK rating from TMDB content ratings.
|
||||
|
||||
Args:
|
||||
content_ratings: TMDB content ratings response
|
||||
|
||||
Returns:
|
||||
FSK rating string (e.g., 'FSK 12') or None
|
||||
"""
|
||||
if not content_ratings or "results" not in content_ratings:
|
||||
return None
|
||||
|
||||
# Find German rating (iso_3166_1: "DE")
|
||||
for rating in content_ratings["results"]:
|
||||
if rating.get("iso_3166_1") == "DE":
|
||||
rating_value = rating.get("rating", "")
|
||||
|
||||
# Map TMDB German ratings to FSK format
|
||||
fsk_mapping = {
|
||||
"0": "FSK 0",
|
||||
"6": "FSK 6",
|
||||
"12": "FSK 12",
|
||||
"16": "FSK 16",
|
||||
"18": "FSK 18",
|
||||
}
|
||||
|
||||
# Try direct mapping
|
||||
if rating_value in fsk_mapping:
|
||||
return fsk_mapping[rating_value]
|
||||
|
||||
# Try to extract number from rating string (ordered from highest to lowest to avoid partial matches)
|
||||
for key in ["18", "16", "12", "6", "0"]:
|
||||
if key in rating_value:
|
||||
return fsk_mapping[key]
|
||||
|
||||
# Return as-is if it already starts with FSK
|
||||
if rating_value.startswith("FSK"):
|
||||
return rating_value
|
||||
|
||||
logger.debug(f"Unmapped German rating: {rating_value}")
|
||||
return None
|
||||
|
||||
return None
|
||||
|
||||
|
||||
async def close(self):
|
||||
"""Clean up resources."""
|
||||
|
||||
Reference in New Issue
Block a user