Skip to content

Commit

Permalink
feat: random manga and anime
Browse files Browse the repository at this point in the history
  • Loading branch information
r3tr0ananas committed Nov 25, 2024
1 parent 4f5f89f commit 5be587f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
13 changes: 12 additions & 1 deletion anmoku/clients/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions anmoku/clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down
13 changes: 12 additions & 1 deletion anmoku/clients/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions anmoku/resources/anime/anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Anime(JikanResource):
"""
_get_endpoint = "/anime/{id}"
_search_endpoint = "/anime"
_random_endpoint = "/random/anime"


data: JikanResponseData[AnimeData] = field(repr = False)

Expand Down
2 changes: 2 additions & 0 deletions anmoku/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
1 change: 1 addition & 0 deletions anmoku/resources/manga/manga.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Manga(JikanResource):
"""
_get_endpoint = "/manga/{id}"
_search_endpoint = "/manga"
_random_endpoint = "/random/manga"

data: JikanResponseData[MangaData] = field(repr = False)

Expand Down

0 comments on commit 5be587f

Please sign in to comment.