Skip to content

Commit 5bed6b2

Browse files
authored
Get list of albums (#349)
1 parent adfa9e5 commit 5bed6b2

6 files changed

Lines changed: 8465 additions & 2 deletions

File tree

src/spotifyaio/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,10 @@ def __pre_deserialize__(cls, d: dict[str, Any]) -> dict[str, Any]:
519519
"""Pre deserialize hook."""
520520
episodes = d.get("episodes", {}).pop("items", [])
521521
return {**d, "episodes": episodes}
522+
523+
524+
@dataclass
525+
class AlbumsResponse(DataClassORJSONMixin):
526+
"""Albums response model."""
527+
528+
albums: list[Album]

src/spotifyaio/spotify.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from spotifyaio.exceptions import SpotifyConnectionError
1515
from spotifyaio.models import (
1616
Album,
17+
AlbumsResponse,
1718
Artist,
1819
ArtistResponse,
1920
BasePlaylist,
@@ -47,6 +48,7 @@
4748
TopTracksResponse,
4849
UserProfile,
4950
)
51+
from spotifyaio.util import get_identifier
5052

5153
if TYPE_CHECKING:
5254
from spotifyaio import SimplifiedAlbum, Track
@@ -143,11 +145,22 @@ async def _put(
143145

144146
async def get_album(self, album_id: str) -> Album:
145147
"""Get album."""
146-
identifier = album_id.split(":")[-1]
148+
identifier = get_identifier(album_id)
147149
response = await self._get(f"v1/albums/{identifier}")
148150
return Album.from_json(response)
149151

150-
# Get a list of albums
152+
async def get_albums(self, album_ids: list[str]) -> list[Album]:
153+
"""Get albums."""
154+
if not album_ids:
155+
return []
156+
if len(album_ids) > 20:
157+
msg = "Maximum of 20 albums can be requested at once"
158+
raise ValueError(msg)
159+
params: dict[str, Any] = {
160+
"ids": ",".join([get_identifier(i) for i in album_ids])
161+
}
162+
response = await self._get("v1/albums", params=params)
163+
return AlbumsResponse.from_json(response).albums
151164

152165
# Get an album's tracks
153166

src/spotifyaio/util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""Utility functions for the SpotifyAIO package."""
2+
3+
4+
def get_identifier(identifier: str) -> str:
5+
"""Get the identifier from a Spotify URI."""
6+
return identifier.split(":")[-1]

0 commit comments

Comments
 (0)