Skip to content

Commit

Permalink
API version 5.4.7 - manga statistics batch and single split
Browse files Browse the repository at this point in the history
  • Loading branch information
AbstractUmbra committed Jan 4, 2022
1 parent 09b7186 commit 6c77846
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,16 @@ Statistics
----------
.. autoclass:: StatisticsRatingResponse

.. autoclass:: BatchStatisticsRatingResponse

.. autoclass:: StatisticsResponse

.. autoclass:: BatchStatisticsResponse

.. autoclass:: GetStatisticsResponse

.. autoclass:: BatchGetStatisticsResponse

.. autoclass:: PersonalMangaRatingsResponse

.. autoclass:: GetPersonalMangaRatingsResponse
Expand Down
24 changes: 22 additions & 2 deletions hondana/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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():
Expand Down
7 changes: 6 additions & 1 deletion hondana/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
18 changes: 14 additions & 4 deletions hondana/manga.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = (
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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__ = (
Expand All @@ -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"<MangaStatistics for={self.parent_id}>"
Expand Down
33 changes: 33 additions & 0 deletions hondana/types/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@

__all__ = (
"StatisticsRatingResponse",
"BatchStatisticsRatingResponse",
"StatisticsResponse",
"BatchStatisticsResponse",
"GetStatisticsResponse",
"BatchGetStatisticsResponse",
"PersonalMangaRatingsResponse",
"GetPersonalMangaRatingsResponse",
)
Expand All @@ -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`
Expand All @@ -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"``]
Expand All @@ -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`
Expand Down

0 comments on commit 6c77846

Please sign in to comment.