add has filter type

This commit is contained in:
cloudwithax 2023-05-01 07:42:58 -04:00
parent 248cce6656
commit b91f6ec04e
No known key found for this signature in database
GPG Key ID: 5DBE54E45794983E
2 changed files with 44 additions and 37 deletions

View File

@ -122,11 +122,13 @@ class Client:
else: else:
request_url = AM_REQ_URL.format(country=country, type=type, id=id) request_url = AM_REQ_URL.format(country=country, type=type, id=id)
async with self.session.get(request_url, headers=self.headers) as resp: resp = await self.session.get(request_url, headers=self.headers)
if resp.status != 200: if resp.status != 200:
raise AppleMusicRequestException( raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}", f"Error while fetching results: {resp.status} {resp.reason}",
) )
data: dict = await resp.json(loads=json.loads) data: dict = await resp.json(loads=json.loads)
self._log.debug( self._log.debug(
f"Made request to Apple Music API with status {resp.status} and response {data}", f"Made request to Apple Music API with status {resp.status} and response {data}",
@ -137,23 +139,24 @@ class Client:
if type == "song": if type == "song":
return Song(data) return Song(data)
if type == "album": elif type == "album":
return Album(data) return Album(data)
if type == "artist": elif type == "artist":
async with self.session.get( resp = await self.session.get(
f"{request_url}/view/top-songs", f"{request_url}/view/top-songs",
headers=self.headers, headers=self.headers,
) as resp: )
if resp.status != 200: if resp.status != 200:
raise AppleMusicRequestException( raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}", f"Error while fetching results: {resp.status} {resp.reason}",
) )
top_tracks: dict = await resp.json(loads=json.loads) top_tracks: dict = await resp.json(loads=json.loads)
artist_tracks: dict = top_tracks["data"] artist_tracks: dict = top_tracks["data"]
return Artist(data, tracks=artist_tracks) return Artist(data, tracks=artist_tracks)
else:
track_data: dict = data["relationships"]["tracks"] track_data: dict = data["relationships"]["tracks"]
album_tracks: List[Song] = [Song(track) for track in track_data["data"]] album_tracks: List[Song] = [Song(track) for track in track_data["data"]]
@ -167,14 +170,14 @@ class Client:
next_page_url = AM_BASE_URL + _next next_page_url = AM_BASE_URL + _next
while next_page_url is not None: while next_page_url is not None:
async with self.session.get(next_page_url, headers=self.headers) as resp: resp = await self.session.get(next_page_url, headers=self.headers)
if resp.status != 200: if resp.status != 200:
raise AppleMusicRequestException( raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}", f"Error while fetching results: {resp.status} {resp.reason}",
) )
next_data: dict = await resp.json(loads=json.loads) next_data: dict = await resp.json(loads=json.loads)
album_tracks.extend(Song(track) for track in next_data["data"]) album_tracks.extend(Song(track) for track in next_data["data"])
_next = next_data.get("next") _next = next_data.get("next")

View File

@ -83,6 +83,10 @@ class Filters:
"""Checks if a filter exists in the list of filters using its filter tag""" """Checks if a filter exists in the list of filters using its filter tag"""
return any(f for f in self._filters if f.tag == filter_tag) return any(f for f in self._filters if f.tag == filter_tag)
def has_filter_type(self, *, filter_type: Filter) -> bool:
"""Checks if any filters applied match the specified filter type."""
return any(f for f in self._filters if type(f) == type(filter_type))
def reset_filters(self) -> None: def reset_filters(self) -> None:
"""Removes all filters from the list""" """Removes all filters from the list"""
self._filters = [] self._filters = []