diff --git a/scanner/matcher/matcher.py b/scanner/matcher/matcher.py index 0fd597d2..982a1aa1 100644 --- a/scanner/matcher/matcher.py +++ b/scanner/matcher/matcher.py @@ -87,6 +87,7 @@ class Matcher: async def search_movie(self, title: str, year: Optional[int], path: str): movie = await self._provider.search_movie(title, year) + movie.file_title = title movie.path = path logger.debug("Got movie: %s", movie) movie_id = await self._client.post("movies", data=movie.to_kyoo()) diff --git a/scanner/providers/types/movie.py b/scanner/providers/types/movie.py index 5ae09ce4..b9f3c63c 100644 --- a/scanner/providers/types/movie.py +++ b/scanner/providers/types/movie.py @@ -1,9 +1,10 @@ -import os from dataclasses import asdict, dataclass, field from datetime import date from typing import Optional from enum import Enum +from providers.utils import select_translation, select_image + from .collection import Collection from .genre import Genre from .studio import Studio @@ -44,22 +45,22 @@ class Movie: external_id: dict[str, MetadataID] path: Optional[str] = None + # The title of this show according to it's filename (None only for ease of use in providers) + file_title: Optional[str] = None collections: list[Collection] = field(default_factory=list) translations: dict[str, MovieTranslation] = field(default_factory=dict) def to_kyoo(self): - from ..utils import select_image - - # For now, the API of kyoo only support one language so we remove the others. - default_language = os.environ["LIBRARY_LANGUAGES"].split(",")[0] + trans = select_translation(self) or MovieTranslation(name=self.file_title or "") return { **asdict(self), - **asdict(self.translations[default_language]), + **asdict(trans), "poster": select_image(self, "posters"), "thumbnail": select_image(self, "thumbnails"), "logo": select_image(self, "logos"), - "trailer": next(iter(self.translations[default_language].trailers), None), + "trailer": next(iter(trans.trailers), None), "studio": next((x.to_kyoo() for x in self.studios), None), "genres": [x.to_kyoo() for x in self.genres], "collections": None, + "file_title": None, } diff --git a/scanner/providers/utils.py b/scanner/providers/utils.py index 1024de92..452cc713 100644 --- a/scanner/providers/utils.py +++ b/scanner/providers/utils.py @@ -1,6 +1,7 @@ +from __future__ import annotations + import os from datetime import date -from itertools import chain from typing import Literal @@ -16,24 +17,33 @@ def format_date(date: date | int | None) -> str | None: return date.isoformat() +# For now, the API of kyoo only support one language so we remove the others. +default_languages = os.environ["LIBRARY_LANGUAGES"].split(",") + + +def select_translation(value: Movie | Show, *, prefer_orginal=False): + if ( + prefer_orginal + and value.original_language + and value.original_language in value.translations + ): + return value.translations[value.original_language] + for lang in default_languages: + if lang in value.translations: + return value.translations[lang] + return None + + def select_image( - self: Movie | Show, - type: Literal["posters"] | Literal["thumbnails"] | Literal["logos"], + value: Movie | Show, + kind: Literal["posters"] | Literal["thumbnails"] | Literal["logos"], ) -> str | None: - # For now, the API of kyoo only support one language so we remove the others. - default_language = os.environ["LIBRARY_LANGUAGES"].split(",")[0] - return next( - chain( - ( - getattr(self.translations[self.original_language], type) - if self.original_language - else [] - ), - getattr(self.translations[default_language], type), - *(getattr(x, type) for x in self.translations.values()), - ), - None, + trans = select_translation(value, prefer_orginal=True) or next( + iter(value.translations.values()), None ) + if trans is None: + return None + return getattr(trans, kind) class ProviderError(RuntimeError):