Add placeholder seasons for anilist

This commit is contained in:
Zoe Roux 2024-04-26 22:33:19 +02:00
parent 833775fede
commit fb0af0855c
No known key found for this signature in database
3 changed files with 54 additions and 25 deletions

View File

@ -1,9 +1,8 @@
import asyncio import asyncio
from aiohttp import ClientSession from aiohttp import ClientSession
from datetime import date from datetime import date, timedelta
from logging import getLogger from logging import getLogger
from typing import Awaitable, Callable, Dict, List, Optional, Any, TypeVar from typing import Optional
from itertools import accumulate, zip_longest
from providers.utils import ProviderError from providers.utils import ProviderError
from matcher.cache import cache from matcher.cache import cache
@ -56,24 +55,30 @@ class AniList(Provider):
return "anilist" return "anilist"
async def get(self, query: str, not_found: str, **variables: Optional[str | int]): async def get(self, query: str, not_found: str, **variables: Optional[str | int]):
logger.error(variables) while True:
async with self._client.post( async with self._client.post(
self.base, self.base,
json={ json={
"query": query, "query": query,
"variables": {k: v for (k, v) in variables.items() if v is not None}, "variables": {
}, k: v for (k, v) in variables.items() if v is not None
) as r: },
if r.status == 404: },
raise ProviderError(not_found) ) as r:
ret = await r.json() if r.status == 404:
logger.error(ret) raise ProviderError(not_found)
r.raise_for_status() if r.status == 429:
if "errors" in ret: await asyncio.sleep(float(r.headers["Retry-After"]))
continue
ret = await r.json()
logger.error(ret) logger.error(ret)
raise Exception(ret["errors"]) r.raise_for_status()
return ret["data"] if "errors" in ret:
logger.error(ret)
raise Exception(ret["errors"])
return ret["data"]
@cache(ttl=timedelta(days=1))
async def query_anime( async def query_anime(
self, self,
*, *,
@ -94,6 +99,7 @@ class AniList(Provider):
} }
description(asHtml: false) description(asHtml: false)
status status
episodes
startDate { startDate {
year year
month month
@ -153,7 +159,7 @@ class AniList(Provider):
not_found=f"Could not find the show {id or ''}{search or ''}", not_found=f"Could not find the show {id or ''}{search or ''}",
) )
ret = q["Media"] ret = q["Media"]
return Show( show = Show(
translations={ translations={
"en": ShowTranslation( "en": ShowTranslation(
name=ret["title"]["romaji"], name=ret["title"]["romaji"],
@ -217,7 +223,27 @@ class AniList(Provider):
}, },
seasons=[], seasons=[],
) )
show.seasons.append(
Season(
# TODO: fill this approprietly
season_number=1,
episodes_count=ret["episodes"],
start_air=show.start_air,
end_air=show.end_air,
external_id=show.external_id,
translations={
"en": SeasonTranslation(
name=show.translations["en"].name,
overview=show.translations["en"].overview,
posters=show.translations["en"].posters,
thumbnails=[],
)
},
)
)
return show
@cache(ttl=timedelta(days=1))
async def query_movie( async def query_movie(
self, self,
*, *,
@ -378,7 +404,8 @@ class AniList(Provider):
return await self.query_anime(id=show_id) return await self.query_anime(id=show_id)
async def identify_season(self, show_id: str, season: int) -> Season: async def identify_season(self, show_id: str, season: int) -> Season:
raise NotImplementedError show = await self.query_anime(id=show_id)
return next((x for x in show.seasons if x.season_number == season))
async def identify_episode( async def identify_episode(
self, show_id: str, season: Optional[int], episode_nbr: int, absolute: int self, show_id: str, season: Optional[int], episode_nbr: int, absolute: int

View File

@ -2,7 +2,7 @@ import asyncio
from aiohttp import ClientSession from aiohttp import ClientSession
from datetime import datetime, timedelta from datetime import datetime, timedelta
from logging import getLogger from logging import getLogger
from typing import Awaitable, Callable, Dict, List, Optional, Any, TypeVar from typing import cast, Awaitable, Callable, Dict, List, Optional, Any, TypeVar
from itertools import accumulate, zip_longest from itertools import accumulate, zip_longest
from providers.utils import ProviderError from providers.utils import ProviderError
@ -635,7 +635,9 @@ class TheMovieDatabase(Provider):
show = await self.identify_show(show_id) show = await self.identify_show(show_id)
# Dont forget to ingore the special season (season_number 0) # Dont forget to ingore the special season (season_number 0)
seasons_nbrs = [x.season_number for x in show.seasons if x.season_number != 0] seasons_nbrs = [x.season_number for x in show.seasons if x.season_number != 0]
seasons_eps = [x.episodes_count for x in show.seasons if x.season_number != 0] seasons_eps = [
cast(int, x.episodes_count) for x in show.seasons if x.season_number != 0
]
if not any(seasons_nbrs): if not any(seasons_nbrs):
return (None, None) return (None, None)
@ -663,7 +665,7 @@ class TheMovieDatabase(Provider):
show = await self.identify_show(show_id) show = await self.identify_show(show_id)
return ( return (
sum( sum(
x.episodes_count cast(int, x.episodes_count)
for x in show.seasons for x in show.seasons
if 0 < x.season_number < season if 0 < x.season_number < season
) )

View File

@ -19,7 +19,7 @@ class Season:
season_number: int season_number: int
# This is not used by kyoo, this is just used internaly by the TMDB provider. # This is not used by kyoo, this is just used internaly by the TMDB provider.
# maybe this should be moved? # maybe this should be moved?
episodes_count: int episodes_count: Optional[int]
start_air: Optional[date | int] = None start_air: Optional[date | int] = None
end_air: Optional[date | int] = None end_air: Optional[date | int] = None
external_id: dict[str, MetadataID] = field(default_factory=dict) external_id: dict[str, MetadataID] = field(default_factory=dict)