chg: fix: apply MR suggestions

This commit is contained in:
Felipe Marinho 2024-11-15 10:51:52 -03:00 committed by Zoe Roux
parent 60410481c1
commit 9416c06a0d
No known key found for this signature in database
2 changed files with 23 additions and 27 deletions

View File

@ -13,7 +13,7 @@ LIBRARY_LANGUAGES=en
# If this is true, kyoo will prefer to download the media in the original language of the item. # If this is true, kyoo will prefer to download the media in the original language of the item.
MEDIA_PREFER_ORIGINAL_LANGUAGE=false MEDIA_PREFER_ORIGINAL_LANGUAGE=false
# A pattern (regex) to ignore files. # A pattern (regex) to ignore files.
LIBRARY_IGNORE_PATTERN=".*/[dD]ownloads?/.*|.*\.(mp3|srt|jpg|jpeg|png|gif|bmp|tiff|svg)$|.*[Tt][Rr][Aa][Ii][Ll][Ee][Rr].*" LIBRARY_IGNORE_PATTERN=".*/[dD]ownloads?/.*|.*[Tt][Rr][Aa][Ii][Ll][Ee][Rr].*"
# If this is true, new accounts wont have any permissions before you approve them in your admin dashboard. # If this is true, new accounts wont have any permissions before you approve them in your admin dashboard.
REQUIRE_ACCOUNT_VERIFICATION=true REQUIRE_ACCOUNT_VERIFICATION=true

View File

@ -140,7 +140,7 @@ class TheMovieDatabase(Provider):
}, },
) )
async def get_best_image(self, item, lng, key): def get_best_image(self, item: dict[str, Any], lng: Language, key: str) -> list[dict]:
""" """
Retrieves the best available images for a item based on localization. Retrieves the best available images for a item based on localization.
@ -177,25 +177,23 @@ class TheMovieDatabase(Provider):
if not localized_images: if not localized_images:
localized_images = item["images"][key] localized_images = item["images"][key]
# Corner case: If there are no images at all, call TMDB images API. # Step 4: If there are no images at all, fallback to _path attribute.
# Although doing another API call is not ideal, this would only be called very rarely.
if not localized_images: if not localized_images:
logger.debug( localized_images = self._get_image_fallback(item, key)
"[Fallback] No images found for %s %s. Calling TMDB images API.",
item["media_type"],
item["id"],
)
images = await self.get(
f"{item['media_type']}/{item['id']}/images",
)
localized_images = sorted(
images[key],
key=lambda x: x.get("vote_average", 0),
reverse=True,
)
return self.get_image(localized_images) return self.get_image(localized_images)
def _get_image_fallback(self, item: dict[str, Any], key: str) -> list[dict]:
"""
Fallback to _path attribute if there are no images available in the images list.
"""
if key == "posters":
return [{"file_path": item.get("poster_path")}]
elif key == "backdrops":
return [{"file_path": item.get("backdrop_path")}]
return []
async def search_movie(self, name: str, year: Optional[int]) -> Movie: async def search_movie(self, name: str, year: Optional[int]) -> Movie:
search_results = ( search_results = (
await self.get("search/movie", params={"query": name, "year": year}) await self.get("search/movie", params={"query": name, "year": year})
@ -208,7 +206,7 @@ class TheMovieDatabase(Provider):
) )
async def identify_movie( async def identify_movie(
self, movie_id: str, original_language: Optional[str] = "null" self, movie_id: str, original_language: Optional[Language] = None
) -> Movie: ) -> Movie:
languages = self.get_languages() languages = self.get_languages()
@ -218,10 +216,9 @@ class TheMovieDatabase(Provider):
params={ params={
"language": lng.to_tag(), "language": lng.to_tag(),
"append_to_response": "alternative_titles,videos,credits,keywords,images", "append_to_response": "alternative_titles,videos,credits,keywords,images",
"include_image_language": f"{lng.language},null,{original_language}", "include_image_language": f"{lng.language},null,{original_language.language if original_language else ""}",
}, },
) )
movie["media_type"] = "movie"
logger.debug("TMDb responded: %s", movie) logger.debug("TMDb responded: %s", movie)
ret = Movie( ret = Movie(
@ -274,9 +271,9 @@ class TheMovieDatabase(Provider):
tagline=movie["tagline"] if movie["tagline"] else None, tagline=movie["tagline"] if movie["tagline"] else None,
tags=list(map(lambda x: x["name"], movie["keywords"]["keywords"])), tags=list(map(lambda x: x["name"], movie["keywords"]["keywords"])),
overview=movie["overview"], overview=movie["overview"],
posters=(await self.get_best_image(movie, lng, "posters")), posters=self.get_best_image(movie, lng, "posters"),
logos=(await self.get_best_image(movie, lng, "logos")), logos=self.get_best_image(movie, lng, "logos"),
thumbnails=(await self.get_best_image(movie, lng, "backdrops")), thumbnails=self.get_best_image(movie, lng, "backdrops"),
trailers=[ trailers=[
f"https://www.youtube.com/watch?v={x['key']}" f"https://www.youtube.com/watch?v={x['key']}"
for x in movie["videos"]["results"] for x in movie["videos"]["results"]
@ -313,7 +310,6 @@ class TheMovieDatabase(Provider):
"include_image_language": f"{lng.language},null,en", "include_image_language": f"{lng.language},null,en",
}, },
) )
show["media_type"] = "tv"
logger.debug("TMDb responded: %s", show) logger.debug("TMDb responded: %s", show)
ret = Show( ret = Show(
@ -364,9 +360,9 @@ class TheMovieDatabase(Provider):
tagline=show["tagline"] if show["tagline"] else None, tagline=show["tagline"] if show["tagline"] else None,
tags=list(map(lambda x: x["name"], show["keywords"]["results"])), tags=list(map(lambda x: x["name"], show["keywords"]["results"])),
overview=show["overview"], overview=show["overview"],
posters=(await self.get_best_image(show, lng, "posters")), posters=self.get_best_image(show, lng, "posters"),
logos=(await self.get_best_image(show, lng, "logos")), logos=self.get_best_image(show, lng, "logos"),
thumbnails=(await self.get_best_image(show, lng, "backdrops")), thumbnails=self.get_best_image(show, lng, "backdrops"),
trailers=[ trailers=[
f"https://www.youtube.com/watch?v={x['key']}" f"https://www.youtube.com/watch?v={x['key']}"
for x in show["videos"]["results"] for x in show["videos"]["results"]