Handle missing translations for movies

This commit is contained in:
Zoe Roux 2024-05-14 01:46:51 +02:00
parent c8402780f3
commit 710e3cf10b
No known key found for this signature in database
3 changed files with 35 additions and 23 deletions

View File

@ -87,6 +87,7 @@ class Matcher:
async def search_movie(self, title: str, year: Optional[int], path: str): async def search_movie(self, title: str, year: Optional[int], path: str):
movie = await self._provider.search_movie(title, year) movie = await self._provider.search_movie(title, year)
movie.file_title = title
movie.path = path movie.path = path
logger.debug("Got movie: %s", movie) logger.debug("Got movie: %s", movie)
movie_id = await self._client.post("movies", data=movie.to_kyoo()) movie_id = await self._client.post("movies", data=movie.to_kyoo())

View File

@ -1,9 +1,10 @@
import os
from dataclasses import asdict, dataclass, field from dataclasses import asdict, dataclass, field
from datetime import date from datetime import date
from typing import Optional from typing import Optional
from enum import Enum from enum import Enum
from providers.utils import select_translation, select_image
from .collection import Collection from .collection import Collection
from .genre import Genre from .genre import Genre
from .studio import Studio from .studio import Studio
@ -44,22 +45,22 @@ class Movie:
external_id: dict[str, MetadataID] external_id: dict[str, MetadataID]
path: Optional[str] = None 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) collections: list[Collection] = field(default_factory=list)
translations: dict[str, MovieTranslation] = field(default_factory=dict) translations: dict[str, MovieTranslation] = field(default_factory=dict)
def to_kyoo(self): def to_kyoo(self):
from ..utils import select_image trans = select_translation(self) or MovieTranslation(name=self.file_title or "")
# For now, the API of kyoo only support one language so we remove the others.
default_language = os.environ["LIBRARY_LANGUAGES"].split(",")[0]
return { return {
**asdict(self), **asdict(self),
**asdict(self.translations[default_language]), **asdict(trans),
"poster": select_image(self, "posters"), "poster": select_image(self, "posters"),
"thumbnail": select_image(self, "thumbnails"), "thumbnail": select_image(self, "thumbnails"),
"logo": select_image(self, "logos"), "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), "studio": next((x.to_kyoo() for x in self.studios), None),
"genres": [x.to_kyoo() for x in self.genres], "genres": [x.to_kyoo() for x in self.genres],
"collections": None, "collections": None,
"file_title": None,
} }

View File

@ -1,6 +1,7 @@
from __future__ import annotations
import os import os
from datetime import date from datetime import date
from itertools import chain
from typing import Literal from typing import Literal
@ -16,24 +17,33 @@ def format_date(date: date | int | None) -> str | None:
return date.isoformat() 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( def select_image(
self: Movie | Show, value: Movie | Show,
type: Literal["posters"] | Literal["thumbnails"] | Literal["logos"], kind: Literal["posters"] | Literal["thumbnails"] | Literal["logos"],
) -> str | None: ) -> str | None:
# For now, the API of kyoo only support one language so we remove the others. trans = select_translation(value, prefer_orginal=True) or next(
default_language = os.environ["LIBRARY_LANGUAGES"].split(",")[0] iter(value.translations.values()), None
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,
) )
if trans is None:
return None
return getattr(trans, kind)
class ProviderError(RuntimeError): class ProviderError(RuntimeError):