Skip to content

Commit

Permalink
feat: genres
Browse files Browse the repository at this point in the history
  • Loading branch information
r3tr0ananas committed Nov 25, 2024
1 parent 7292eb4 commit 42b33b3
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 7 deletions.
18 changes: 17 additions & 1 deletion anmoku/clients/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from ..typing.anmoku import SnowflakeT
from ..typing.jikan import SearchResultData

from .base import ResourceGenericT, SearchResourceGenericT, RandomResourceGenericT
from .base import (
ResourceGenericT,
SearchResourceGenericT,
RandomResourceGenericT,
GenresResourceGenericT
)

from aiohttp import ClientSession
from devgoldyutils import Colours
Expand Down Expand Up @@ -88,6 +93,17 @@ async def random(self, resource: Type[RandomResourceGenericT]) -> RandomResource

return resource(json_data)

async def genres(self, resource: Type[GenresResourceGenericT]) -> GenresResourceGenericT:
"""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 @@ -27,6 +27,12 @@
resources.Manga
)

GenresResourceGenericT = TypeVar(
"GenresResourceGenericT",
resources.AnimeGenres,
resources.MangaGenres
)

import logging
from urllib.parse import quote
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -77,6 +83,11 @@ def search(self, resource: Type[SearchResourceGenericT], query: str) -> SearchRe
def random(self, resource: Type[RandomResourceGenericT]) -> RandomResourceGenericT:
"""Fetches a random object of the specified resource."""
...

@abstractmethod
def genres(self, resource: Type[GenresResourceGenericT]) -> GenresResourceGenericT:
"""Fetches a genres 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
18 changes: 17 additions & 1 deletion anmoku/clients/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from ..typing.anmoku import SnowflakeT
from ..typing.jikan import SearchResultData

from .base import ResourceGenericT, SearchResourceGenericT, RandomResourceGenericT
from .base import (
ResourceGenericT,
SearchResourceGenericT,
RandomResourceGenericT,
GenresResourceGenericT
)

from requests import Session
from devgoldyutils import Colours
Expand Down Expand Up @@ -86,6 +91,17 @@ def random(self, resource: Type[RandomResourceGenericT]) -> RandomResourceGeneri

return resource(json_data)

def genres(self, resource: Type[GenresResourceGenericT]) -> GenresResourceGenericT:
"""Fetches a random object of the specified resource."""
url = resource._genres_endpoint

if url is None:
raise ResourceNotSupportedError(resource, "random")

json_data = self._request(url)

return resource(json_data)

def _request(
self,
route: str,
Expand Down
3 changes: 2 additions & 1 deletion anmoku/resources/anime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
from .recommendations import *
from .reviews import *
from .relations import *
from .external import *
from .external import *
from .genre import *
2 changes: 1 addition & 1 deletion anmoku/resources/anime/anime.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Anime(JikanResource):
_get_endpoint = "/anime/{id}"
_search_endpoint = "/anime"
_random_endpoint = "/random/anime"

_genres_endpoint = "/genres/anime"

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

Expand Down
29 changes: 29 additions & 0 deletions anmoku/resources/anime/genre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import List

from ...typing.jikan import (
GenreData,
JikanResponseData,
)

from dataclasses import dataclass, field

from ..base import JikanResource
from ..helpers import Genre

__all__ = (
"AnimeGenres",
)

@dataclass
class AnimeGenres(JikanResource):
_genres_endpoint = "/genres/anime"

data: JikanResponseData[List[GenreData]] = field(repr = False)

def __iter__(self):
for genre in self.data["data"]:
yield Genre(genre)
2 changes: 2 additions & 0 deletions anmoku/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ class JikanResource():
"""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."""
_genres_endpoint: Optional[str] = field(init = False, default = None)
"""The jikan api endpoint where you can get the genres of this resource."""

data: JikanResponseData[Any]
3 changes: 2 additions & 1 deletion anmoku/resources/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
from .review import *
from .user import *
from .relations import *
from .external import *
from .external import *
from .genre import *
30 changes: 30 additions & 0 deletions anmoku/resources/helpers/genre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from ...typing.jikan import (
GenreData,
)

from dataclasses import dataclass, field

__all__ = (
"Genre",
)

@dataclass
class Genre():
data: GenreData = field(repr = False)

id: int = field(init = False)
name: str = field(init = False)
url: str = field(init = False)
count: int = field(init = False)

def __post_init__(self):
genre = self.data

self.id = genre["mal_id"]
self.name = genre["name"]
self.url = genre["url"]
self.count = genre["count"]
3 changes: 2 additions & 1 deletion anmoku/resources/manga/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
from .reviews import *
from .user_updates import *
from .relations import *
from .external import *
from .external import *
from .genre import *
29 changes: 29 additions & 0 deletions anmoku/resources/manga/genre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import List

from ...typing.jikan import (
GenreData,
JikanResponseData,
)

from dataclasses import dataclass, field

from ..base import JikanResource
from ..helpers import Genre

__all__ = (
"MangaGenres",
)

@dataclass
class MangaGenres(JikanResource):
_genres_endpoint = "/genres/manga"

data: JikanResponseData[List[GenreData]] = field(repr = False)

def __iter__(self):
for genre in self.data["data"]:
yield Genre(genre)
3 changes: 2 additions & 1 deletion anmoku/typing/jikan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
from .review import *
from .user import *
from .entry import *
from .external import *
from .external import *
from .genre import *
12 changes: 12 additions & 0 deletions anmoku/typing/jikan/genre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from __future__ import annotations
from typing import TypedDict

__all__ = (
"GenreData",
)

class GenreData(TypedDict):
mal_id: int
name: str
url: str
count: int

0 comments on commit 42b33b3

Please sign in to comment.