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,68 +122,71 @@ class Client:
else:
request_url = AM_REQ_URL.format(country=country, type=type, id=id)
async with self.session.get(request_url, headers=self.headers) as resp:
if resp.status != 200:
raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}",
)
data: dict = await resp.json(loads=json.loads)
self._log.debug(
f"Made request to Apple Music API with status {resp.status} and response {data}",
resp = await self.session.get(request_url, headers=self.headers)
if resp.status != 200:
raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}",
)
data: dict = await resp.json(loads=json.loads)
self._log.debug(
f"Made request to Apple Music API with status {resp.status} and response {data}",
)
data = data["data"][0]
if type == "song":
return Song(data)
if type == "album":
elif type == "album":
return Album(data)
if type == "artist":
async with self.session.get(
elif type == "artist":
resp = await self.session.get(
f"{request_url}/view/top-songs",
headers=self.headers,
) as resp:
if resp.status != 200:
raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}",
)
top_tracks: dict = await resp.json(loads=json.loads)
artist_tracks: dict = top_tracks["data"]
)
if resp.status != 200:
raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}",
)
top_tracks: dict = await resp.json(loads=json.loads)
artist_tracks: dict = top_tracks["data"]
return Artist(data, tracks=artist_tracks)
else:
track_data: dict = data["relationships"]["tracks"]
album_tracks: List[Song] = [Song(track) for track in track_data["data"]]
track_data: dict = data["relationships"]["tracks"]
album_tracks: List[Song] = [Song(track) for track in track_data["data"]]
if not len(album_tracks):
raise AppleMusicRequestException(
"This playlist is empty and therefore cannot be queued.",
)
if not len(album_tracks):
raise AppleMusicRequestException(
"This playlist is empty and therefore cannot be queued.",
)
_next = track_data.get("next")
if _next:
next_page_url = AM_BASE_URL + _next
_next = track_data.get("next")
if _next:
next_page_url = AM_BASE_URL + _next
while next_page_url is not None:
resp = await self.session.get(next_page_url, headers=self.headers)
while next_page_url is not None:
async with self.session.get(next_page_url, headers=self.headers) as resp:
if resp.status != 200:
raise AppleMusicRequestException(
f"Error while fetching results: {resp.status} {resp.reason}",
)
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")
if _next:
next_page_url = AM_BASE_URL + _next
else:
next_page_url = None
_next = next_data.get("next")
if _next:
next_page_url = AM_BASE_URL + _next
else:
next_page_url = None
return Playlist(data, album_tracks)
return Playlist(data, album_tracks)
async def close(self) -> None:
if self.session:

View File

@ -83,6 +83,10 @@ class Filters:
"""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)
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:
"""Removes all filters from the list"""
self._filters = []