Type check guessit results

This commit is contained in:
Zoe Roux 2024-04-28 01:50:31 +02:00
parent edf482de03
commit 4ffc112196
No known key found for this signature in database

View File

@ -1,5 +1,5 @@
from datetime import timedelta from datetime import timedelta
from typing import Literal from typing import Literal, Optional
import asyncio import asyncio
from logging import getLogger from logging import getLogger
from providers.provider import Provider, ProviderError from providers.provider import Provider, ProviderError
@ -55,20 +55,44 @@ class Matcher:
if raw.get("season") == raw.get("year") and "season" in raw: if raw.get("season") == raw.get("year") and "season" in raw:
del raw["season"] del raw["season"]
if isinstance(raw.get("season"), list): logger.info("Identied %s: %s", path, raw)
title = raw.get("title")
if isinstance(title, list):
pass
if not isinstance(title, str):
raise ProviderError(f"Could not guess title, found: {title}")
year = raw.get("year")
if year is not None and not isinstance(year, int):
year = None
logger.warn(f"Invalid year value. Found {year}. Ignoring")
if raw["type"] == "movie":
await self.search_movie(title, year, path)
elif raw["type"] == "episode":
season = raw.get("season")
if isinstance(season, list):
raise ProviderError( raise ProviderError(
f"An episode can't have multiple seasons (found {raw.get('season')} for {path})" f"An episode can't have multiple seasons (found {raw.get('season')} for {path})"
) )
if isinstance(raw.get("episode"), list): if season is not None and not isinstance(season, int):
raise ProviderError(f"Could not guess season, found: {season}")
episode = raw.get("episode")
if isinstance(episode, list):
raise ProviderError( raise ProviderError(
f"Multi-episodes files are not yet supported (for {path})" f"Multi-episodes files are not yet supported (for {path})"
) )
if not isinstance(episode, int):
raise ProviderError(f"Could not guess episode, found: {episode}")
logger.info("Identied %s: %s", path, raw) await self.search_episode(title, year, season, episode, path)
else:
logger.warn("Unknown video file type: %s", raw["type"])
if raw["type"] == "movie": async def search_movie(self, title: str, year: Optional[int], path: str):
movie = await self._provider.search_movie(raw["title"], raw.get("year")) movie = await self._provider.search_movie(title, year)
movie.path = str(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())
@ -79,15 +103,23 @@ class Matcher:
await asyncio.gather( await asyncio.gather(
*(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":
async def search_episode(
self,
title: str,
year: Optional[int],
season: Optional[int],
episode_nbr: int,
path: str,
):
episode = await self._provider.search_episode( episode = await self._provider.search_episode(
raw["title"], title,
season=raw.get("season"), season=season,
episode_nbr=raw.get("episode"), episode_nbr=episode_nbr,
absolute=raw.get("episode") if "season" not in raw else None, absolute=episode_nbr if season is None else None,
year=raw.get("year"), year=year,
) )
episode.path = str(path) episode.path = path
logger.debug("Got episode: %s", episode) logger.debug("Got episode: %s", episode)
episode.show_id = await self.create_or_get_show(episode) episode.show_id = await self.create_or_get_show(episode)
@ -96,8 +128,6 @@ class Matcher:
episode.show, episode.show_id, episode.season_number episode.show, episode.show_id, episode.season_number
) )
await self._client.post("episodes", data=episode.to_kyoo()) await self._client.post("episodes", data=episode.to_kyoo())
else:
logger.warn("Unknown video file type: %s", raw["type"])
async def create_or_get_collection(self, collection: Collection) -> str: async def create_or_get_collection(self, collection: Collection) -> str:
@cache(ttl=timedelta(days=1), cache=self._collection_cache) @cache(ttl=timedelta(days=1), cache=self._collection_cache)