From 6c778469bb7d6264a421c3bd68d27403616e504c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20N=C3=B8rgaard?= Date: Tue, 4 Jan 2022 15:44:57 +0000 Subject: [PATCH] API version 5.4.7 - manga statistics batch and single split --- docs/types.rst | 6 ++++++ hondana/client.py | 24 ++++++++++++++++++++++-- hondana/http.py | 7 ++++++- hondana/manga.py | 18 ++++++++++++++---- hondana/types/statistics.py | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 7 deletions(-) diff --git a/docs/types.rst b/docs/types.rst index d824e28..a0a33eb 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -239,10 +239,16 @@ Statistics ---------- .. autoclass:: StatisticsRatingResponse +.. autoclass:: BatchStatisticsRatingResponse + .. autoclass:: StatisticsResponse +.. autoclass:: BatchStatisticsResponse + .. autoclass:: GetStatisticsResponse +.. autoclass:: BatchGetStatisticsResponse + .. autoclass:: PersonalMangaRatingsResponse .. autoclass:: GetPersonalMangaRatingsResponse diff --git a/hondana/client.py b/hondana/client.py index de8b5ba..3c6dfb9 100644 --- a/hondana/client.py +++ b/hondana/client.py @@ -3206,7 +3206,27 @@ async def delete_manga_rating(self, manga_id: str, /) -> None: await self._http._delete_manga_rating(manga_id) @require_authentication - async def get_manga_statistics(self, manga_ids: list[str], /) -> list[MangaStatistics]: + async def get_manga_statistics(self, manga_id: str, /) -> MangaStatistics: + """|coro| + + This method will return the statistics for the passed manga. + + Parameters + ----------- + manga_id: :class:`str` + The manga id to fetch the statistics for. + + Returns + --------- + List[:class:`~hondana.MangaStatistics`] + """ + data = await self._http._get_manga_statistics(manga_id) + + key = next(iter(data["statistics"])) + return MangaStatistics(self._http, key, data["statistics"][key]) + + @require_authentication + async def find_manga_statistics(self, manga_ids: list[str], /) -> list[MangaStatistics]: """|coro| This method will return the statistics for the passed manga. @@ -3220,7 +3240,7 @@ async def get_manga_statistics(self, manga_ids: list[str], /) -> list[MangaStati --------- List[:class:`~hondana.MangaStatistics`] """ - data = await self._http._get_manga_statistics(manga_ids) + data = await self._http._find_manga_statistics(manga_ids) fmt: list[MangaStatistics] = [] for id_, stats in data["statistics"].items(): diff --git a/hondana/http.py b/hondana/http.py index bd93706..324654a 100644 --- a/hondana/http.py +++ b/hondana/http.py @@ -2018,7 +2018,12 @@ def _delete_manga_rating(self, manga_id: str, /) -> Response[Literal["ok", "erro return self.request(route) - def _get_manga_statistics(self, manga_ids: list[str], /) -> Response[statistics.GetStatisticsResponse]: + def _get_manga_statistics(self, manga_id: str, /) -> Response[statistics.GetStatisticsResponse]: + route = Route("GET", "/statistics/manga/{manga_id}", manga_id=manga_id) + + return self.request(route) + + def _find_manga_statistics(self, manga_ids: list[str], /) -> Response[statistics.BatchGetStatisticsResponse]: route = Route("GET", "/statistics/manga") query: dict[str, Any] = {"manga": manga_ids} diff --git a/hondana/manga.py b/hondana/manga.py index 201de10..ca39bcf 100644 --- a/hondana/manga.py +++ b/hondana/manga.py @@ -50,7 +50,11 @@ from .types.author import AuthorResponse from .types.common import ContentRating, LanguageCode, LocalisedString from .types.relationship import RelationshipResponse - from .types.statistics import PersonalMangaRatingsResponse, StatisticsResponse + from .types.statistics import ( + BatchStatisticsResponse, + PersonalMangaRatingsResponse, + StatisticsResponse, + ) __all__ = ( @@ -1280,7 +1284,7 @@ async def get_statistics(self) -> MangaStatistics: -------- :class:`~hondana.MangaStatistics` """ - data = await self._http._get_manga_statistics([self.id]) + data = await self._http._get_manga_statistics(self.id) key = next(iter(data["statistics"])) stats = MangaStatistics(self._http, self.id, data["statistics"][key]) @@ -1349,6 +1353,10 @@ class MangaStatistics: The average mean score of the manga. distribution: List[:class:`int`] The scoring distribution of the manga. + + + .. note:: + The :attr:`distribution` attribute will be None unless this object was created with :meth:`~hondana.Client.get_manga_statistics` or :meth:`~hondana.Manga.get_statistics` """ __slots__ = ( @@ -1361,14 +1369,16 @@ class MangaStatistics: "distribution", ) - def __init__(self, http: HTTPClient, parent_id: str, payload: StatisticsResponse) -> None: + def __init__( + self, http: HTTPClient, parent_id: str, payload: Union[StatisticsResponse, BatchStatisticsResponse] + ) -> None: self._http = http self._data = payload self._rating = payload["rating"] self.follows: int = payload["follows"] self.parent_id: str = parent_id self.average: float = self._rating["average"] - self.distribution: list[int] = self._rating["distribution"] + self.distribution: Optional[list[int]] = self._rating.get("distribution") def __repr__(self) -> str: return f"" diff --git a/hondana/types/statistics.py b/hondana/types/statistics.py index 4950144..54d71ad 100644 --- a/hondana/types/statistics.py +++ b/hondana/types/statistics.py @@ -29,8 +29,11 @@ __all__ = ( "StatisticsRatingResponse", + "BatchStatisticsRatingResponse", "StatisticsResponse", + "BatchStatisticsResponse", "GetStatisticsResponse", + "BatchGetStatisticsResponse", "PersonalMangaRatingsResponse", "GetPersonalMangaRatingsResponse", ) @@ -47,6 +50,14 @@ class StatisticsRatingResponse(TypedDict): distribution: list[int] +class BatchStatisticsRatingResponse(TypedDict): + """ + average: :class:`float` + """ + + average: float + + class StatisticsResponse(TypedDict): """ rating: :class:`~hondana.types.StatisticsRatingResponse` @@ -58,6 +69,17 @@ class StatisticsResponse(TypedDict): follows: int +class BatchStatisticsResponse(TypedDict): + """ + rating: :class:`~hondana.types.BatchStatisticsRatingResponse` + + follows: :class:`int` + """ + + rating: BatchStatisticsRatingResponse + follows: int + + class GetStatisticsResponse(TypedDict): """ result: Literal[``"ok"``] @@ -69,6 +91,17 @@ class GetStatisticsResponse(TypedDict): statistics: dict[str, StatisticsResponse] +class BatchGetStatisticsResponse(TypedDict): + """ + result: Literal[``"ok"``] + + statistics: Dict[:class:`str`, :class:`~hondana.types.BatchStatisticsResponse`] + """ + + result: Literal["ok"] + statistics: dict[str, BatchStatisticsResponse] + + class PersonalMangaRatingsResponse(TypedDict): """ rating: :class:`int`