diff --git a/anmoku/clients/async_.py b/anmoku/clients/async_.py index 49a90ab..e1bb2ab 100644 --- a/anmoku/clients/async_.py +++ b/anmoku/clients/async_.py @@ -7,7 +7,7 @@ from ..typing.anmoku import SnowflakeT from ..typing.jikan import SearchResultData - from .base import ResourceGenericT, SearchResourceGenericT + from .base import ResourceGenericT, SearchResourceGenericT, RandomResourceGenericT from aiohttp import ClientSession from devgoldyutils import Colours @@ -77,6 +77,17 @@ async def search(self, resource: Type[SearchResourceGenericT], query: str) -> Se return SearchResult(json_data, resource) + async def random(self, resource: Type[RandomResourceGenericT]) -> RandomResourceGenericT: + """Fetches a random object of the specified resource.""" + url = resource._random_endpoint + + if url is None: + raise ResourceNotSupportedError(resource, "random") + + json_data = await self._request(url) + + return resource(json_data) + async def _request( self, route: str, diff --git a/anmoku/clients/base.py b/anmoku/clients/base.py index 7a38f04..d5eda30 100644 --- a/anmoku/clients/base.py +++ b/anmoku/clients/base.py @@ -20,6 +20,12 @@ resources.Manga ) + RandomResourceGenericT = TypeVar( + "RandomResourceGenericT", + resources.Anime, + resources.Manga + ) + import logging from urllib.parse import quote from abc import ABC, abstractmethod @@ -65,6 +71,11 @@ def get(self, resource: Type[ResourceGenericT], id: SnowflakeT) -> ResourceGener def search(self, resource: Type[SearchResourceGenericT], query: str) -> SearchResult[SearchResourceGenericT]: """Searches for the resource and returns a list of the results.""" ... + + @abstractmethod + def random(self, resource: Type[RandomResourceGenericT]) -> RandomResourceGenericT: + """Fetches a random object of the specified resource.""" + ... def _format_url(self, unformatted_url: str, resource: Type[ResourceGenericT], *args: str, **kwargs: str) -> str: """Formats the URL while also taking URL encoding into account.""" diff --git a/anmoku/clients/sync.py b/anmoku/clients/sync.py index aaaf872..4ab4b6b 100644 --- a/anmoku/clients/sync.py +++ b/anmoku/clients/sync.py @@ -7,7 +7,7 @@ from ..typing.anmoku import SnowflakeT from ..typing.jikan import SearchResultData - from .base import ResourceGenericT, SearchResourceGenericT + from .base import ResourceGenericT, SearchResourceGenericT, RandomResourceGenericT from requests import Session from devgoldyutils import Colours @@ -74,6 +74,17 @@ def search(self, resource: Type[SearchResourceGenericT], query: str) -> SearchRe json_data: SearchResultData[Any] = self._request(url, params = {"q": query}) return SearchResult(json_data, resource) + + def random(self, resource: Type[RandomResourceGenericT]) -> RandomResourceGenericT: + """Fetches a random object of the specified resource.""" + url = resource._random_endpoint + + if url is None: + raise ResourceNotSupportedError(resource, "random") + + json_data = self._request(url) + + return resource(json_data) def _request( self, diff --git a/anmoku/resources/anime/anime.py b/anmoku/resources/anime/anime.py index f1c52c1..ff5b324 100644 --- a/anmoku/resources/anime/anime.py +++ b/anmoku/resources/anime/anime.py @@ -71,6 +71,8 @@ class Anime(JikanResource): """ _get_endpoint = "/anime/{id}" _search_endpoint = "/anime" + _random_endpoint = "/random/anime" + data: JikanResponseData[AnimeData] = field(repr = False) diff --git a/anmoku/resources/base.py b/anmoku/resources/base.py index 8222f2f..7a48f28 100644 --- a/anmoku/resources/base.py +++ b/anmoku/resources/base.py @@ -14,5 +14,7 @@ class JikanResource(): """The jikan api endpoint where you can get this object.""" _search_endpoint: Optional[str] = field(init = False, default = None) """The jikan api endpoint to search with this resource.""" + _random_endpoint: Optional[str] = field(init = False, default = None) + """The jikan api endpoint to get a random object with this resource.""" data: JikanResponseData[Any] \ No newline at end of file diff --git a/anmoku/resources/manga/manga.py b/anmoku/resources/manga/manga.py index 02fb436..4e40b0f 100644 --- a/anmoku/resources/manga/manga.py +++ b/anmoku/resources/manga/manga.py @@ -64,6 +64,7 @@ class Manga(JikanResource): """ _get_endpoint = "/manga/{id}" _search_endpoint = "/manga" + _random_endpoint = "/random/manga" data: JikanResponseData[MangaData] = field(repr = False)