Skip to content

Commit

Permalink
feat: add get_beatmap_packs and get_beatmap_pack (#191)
Browse files Browse the repository at this point in the history
* feat: add models for beatmap packs

* feat: add `get_beatmap_packs` and `get_beatmap_pack`

* feat: add tests for beatmap pack endpoints

* chore: update test data
  • Loading branch information
NiceAesth authored Oct 20, 2023
1 parent 98024c6 commit 75ef930
Show file tree
Hide file tree
Showing 55 changed files with 205 additions and 48 deletions.
77 changes: 77 additions & 0 deletions aiosu/models/beatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
"BeatmapsetDiscussionVoteResponse",
"BeatmapsetSearchResponse",
"UserBeatmapType",
"BeatmapPackType",
"BeatmapPackUserCompletion",
"BeatmapPack",
"BeatmapPacksResponse",
)

BeatmapsetDisscussionType = Literal[
Expand Down Expand Up @@ -146,6 +150,44 @@ def _missing_(cls, query: object) -> BeatmapRankStatus:
raise ValueError(f"BeatmapRankStatus {query} does not exist.")


@unique
class BeatmapPackType(Enum):
STANDARD = ("S", "Standard")
FEATURED = ("F", "Featured Artist")
TOURNAMENT = ("P", "Tournament")
LOVED = ("L", "Project Loved")
CHART = ("R", "Spotlights")
THEME = ("T", "Theme")
ARTIST = ("A", "Artist/Album")

def __init__(self, tag: str, description: str) -> None:
self.tag = tag
self.description = description

@classmethod
def from_tag(cls, tag: str) -> BeatmapPackType:
beatmap_pack_type = next((x for x in cls if x.tag == tag), None)
if beatmap_pack_type is None:
raise ValueError(f"BeatmapPackType {tag} does not exist.")
return beatmap_pack_type

def __str__(self) -> str:
return self.name.lower()

@classmethod
def _missing_(cls, query: object) -> BeatmapPackType:
if isinstance(query, cls):
return query
if isinstance(query, str):
query = query.upper()
try:
return BeatmapPackType[query]
except KeyError:
return cls.from_tag(query)

raise ValueError(f"BeatmapPackType {query} does not exist.")


class BeatmapDescription(BaseModel):
bbcode: Optional[str] = None
description: Optional[str] = None
Expand Down Expand Up @@ -497,6 +539,41 @@ class BeatmapsetEvent(BaseModel):
comment: Optional[dict] = None


class BeatmapPackUserCompletion(BaseModel):
beatmapset_ids: list[int]
completed: bool


class BeatmapPack(BaseModel):
author: str
date: datetime
name: str
no_diff_reduction: bool
tag: str
url: str
ruleset_id: Optional[int] = None
beatmapsets: Optional[list[Beatmapset]] = None
user_completion_data: Optional[BeatmapPackUserCompletion] = None

@property
def mode(self) -> Optional[Gamemode]:
if self.ruleset_id is None:
return None
return Gamemode(self.ruleset_id)

@property
def pack_type(self) -> BeatmapPackType:
return BeatmapPackType.from_tag(self.tag[0])

@property
def id(self) -> int:
return int(self.tag[1:])


class BeatmapPacksResponse(CursorModel):
beatmap_packs: list[BeatmapPack]


class BeatmapsetDiscussionResponse(CursorModel):
beatmaps: list[Beatmap]
discussions: list[BeatmapsetDiscussion]
Expand Down
55 changes: 55 additions & 0 deletions aiosu/v2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
from ..models import ArtistResponse
from ..models import Beatmap
from ..models import BeatmapDifficultyAttributes
from ..models import BeatmapPack
from ..models import BeatmapPacksResponse
from ..models import BeatmapPackType
from ..models import Beatmapset
from ..models import BeatmapsetDiscussionPostResponse
from ..models import BeatmapsetDiscussionResponse
Expand Down Expand Up @@ -1361,6 +1364,58 @@ async def search_beatmapsets(
resp.next = partial(self.search_beatmapsets, **kwargs)
return resp

@prepare_token
@check_token
@requires_scope(Scopes.PUBLIC)
async def get_beatmap_packs(self, **kwargs: Any) -> BeatmapPacksResponse:
r"""Get beatmap packs.
:param \**kwargs:
See below
:Keyword Arguments:
* *type* (``aiosu.models.beatmap.BeatmapPackType``) --
Optional, beatmap pack type
* *cursor_string* (``str``) --
Optional, cursor string to get the next page of results
:raises APIException: Contains status code and error message
:return: Beatmap packs response
:rtype: aiosu.models.beatmap.BeatmapPacksResponse
"""
url = f"{self.base_url}/api/v2/beatmaps/packs"
params: dict[str, object] = {}
add_param(
params,
kwargs,
key="type",
converter=lambda x: str(BeatmapPackType[x]),
)
add_param(params, kwargs, key="cursor_string")
json = await self._request("GET", url, params=params)
resp = BeatmapPacksResponse.model_validate(json)
if resp.cursor_string:
kwargs["cursor_string"] = resp.cursor_string
resp.next = partial(self.get_beatmap_packs, **kwargs)
return resp

@prepare_token
@check_token
@requires_scope(Scopes.PUBLIC)
async def get_beatmap_pack(self, pack_tag: str) -> BeatmapPack:
r"""Get beatmap pack.
:param pack_tag: The tag of the beatmap pack
:type pack_tag: str
:raises APIException: Contains status code and error message
:return: Beatmap pack object
:rtype: aiosu.models.beatmap.BeatmapPack
"""
url = f"{self.base_url}/api/v2/beatmaps/packs/{pack_tag}"
json = await self._request("GET", url)
return BeatmapPack.model_validate(json)

@prepare_token
@check_token
@requires_scope(Scopes.PUBLIC)
Expand Down
2 changes: 1 addition & 1 deletion tests/data/v1/get_beatmap_200.json

Large diffs are not rendered by default.

Loading

0 comments on commit 75ef930

Please sign in to comment.