Add identify apis for provider

This commit is contained in:
Zoe Roux 2024-04-11 23:12:19 +02:00
parent 097cbe6cf5
commit dcda8c9072
No known key found for this signature in database
3 changed files with 40 additions and 16 deletions

View File

@ -68,7 +68,7 @@ class Matcher:
logger.info("Identied %s: %s", path, raw) logger.info("Identied %s: %s", path, raw)
if raw["type"] == "movie": if raw["type"] == "movie":
movie = await self._provider.identify_movie(raw["title"], raw.get("year")) movie = await self._provider.search_movie(raw["title"], raw.get("year"))
movie.path = str(path) movie.path = str(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())
@ -81,7 +81,7 @@ class Matcher:
*(self._client.link_collection(x, "movie", movie_id) for x in ids) *(self._client.link_collection(x, "movie", movie_id) for x in ids)
) )
elif raw["type"] == "episode": elif raw["type"] == "episode":
episode = await self._provider.identify_episode( episode = await self._provider.search_episode(
raw["title"], raw["title"],
season=raw.get("season"), season=raw.get("season"),
episode_nbr=raw.get("episode"), episode_nbr=raw.get("episode"),

View File

@ -142,7 +142,7 @@ class TheMovieDatabase(Provider):
}, },
) )
async def identify_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})
)["results"] )["results"]
@ -150,6 +150,9 @@ class TheMovieDatabase(Provider):
raise ProviderError(f"No result for a movie named: {name}") raise ProviderError(f"No result for a movie named: {name}")
search = self.get_best_result(search_results, name, year) search = self.get_best_result(search_results, name, year)
movie_id = search["id"] movie_id = search["id"]
return await self.identify_movie(search["id"])
async def identify_movie(self, movie_id: str) -> Movie:
languages = self.get_languages(search["original_language"]) languages = self.get_languages(search["original_language"])
async def for_language(lng: str) -> Movie: async def for_language(lng: str) -> Movie:
@ -449,7 +452,7 @@ class TheMovieDatabase(Provider):
}, },
) )
async def identify_episode( async def search_episode(
self, self,
name: str, name: str,
season: Optional[int], season: Optional[int],
@ -458,11 +461,9 @@ class TheMovieDatabase(Provider):
year: Optional[int], year: Optional[int],
) -> Episode: ) -> Episode:
show = await self.search_show(name, year) show = await self.search_show(name, year)
languages = self.get_languages(show.original_language)
# Keep it for xem overrides of season/episode # Keep it for xem overrides of season/episode
old_name = name old_name = name
name = show.name name = show.name
show_id = show.external_id[self.name].data_id
# Handle weird season names overrides from thexem. # Handle weird season names overrides from thexem.
# For example when name is "Jojo's bizzare adventure - Stone Ocean", with season None, # For example when name is "Jojo's bizzare adventure - Stone Ocean", with season None,
@ -503,6 +504,15 @@ class TheMovieDatabase(Provider):
if absolute is None: if absolute is None:
absolute = await self.get_absolute_number(show_id, season, episode_nbr) absolute = await self.get_absolute_number(show_id, season, episode_nbr)
return await self._identify_episode(show, season, episode_nbr)
async def identify_episode(self, show_id: str, season_number: int, episode_number: int) -> Episode:
...
async def _identify_episode(self, show: PartialShow, season: int, episode_nbr: int, absolute: int) -> Episode:
show_id = show.external_id[self.name].data_id
async def for_language(lng: str) -> Episode: async def for_language(lng: str) -> Episode:
try: try:
@ -518,7 +528,7 @@ class TheMovieDatabase(Provider):
params={ params={
"language": lng, "language": lng,
}, },
not_found_fail=f"Could not find episode {episode_nbr} of season {season} of serie {name} (absolute: {absolute})", not_found_fail=f"Could not find episode {episode_nbr} of season {season} of serie {show.name} (absolute: {absolute})",
) )
logger.debug("TMDb responded: %s", episode) logger.debug("TMDb responded: %s", episode)
@ -550,8 +560,10 @@ class TheMovieDatabase(Provider):
ret.translations = {lng: translation} ret.translations = {lng: translation}
return ret return ret
languages = self.get_languages(show.original_language)
return await self.process_translations(for_language, languages) return await self.process_translations(for_language, languages)
def get_best_result( def get_best_result(
self, search_results: List[Any], name: str, year: Optional[int] self, search_results: List[Any], name: str, year: Optional[int]
) -> Any: ) -> Any:

View File

@ -51,7 +51,22 @@ class Provider:
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod
async def identify_movie(self, name: str, year: Optional[int]) -> Movie: async def search_movie(self, name: str, year: Optional[int]) -> Movie:
raise NotImplementedError
@abstractmethod
async def search_episode(
self,
name: str,
season: Optional[int],
episode_nbr: Optional[int],
absolute: Optional[int],
year: Optional[int],
) -> Episode:
raise NotImplementedError
@abstractmethod
async def identify_movie(self, movie_id: str) -> Movie:
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod
@ -63,14 +78,11 @@ class Provider:
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod
async def identify_episode( async def identify_episode(self, show_id: str, season_number: int, episode_number: int) -> Episode:
self, raise NotImplementedError
name: str,
season: Optional[int], @abstractmethod
episode_nbr: Optional[int], async def identify_absolute(self, show_id: str, absolute_number: int) -> Episode:
absolute: Optional[int],
year: Optional[int],
) -> Episode:
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod