Skip to content

Commit

Permalink
initial support for Spotify Liked Songs
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-thoni authored Mar 21, 2024
1 parent dc11df7 commit aeab888
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions music_assistant/server/providers/spotify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,35 @@ async def get_library_tracks(self) -> AsyncGenerator[Track, None]:
if item and item["track"]["id"]:
yield await self._parse_track(item["track"])

async def _get_liked_songs_playlist(self) -> Playlist:
liked_songs = Playlist(
item_id="tracks",
provider=self.domain,
name="Liked Songs", # TODO to be translated
owner="Me", # TODO Get logged in user display name
provider_mappings={
ProviderMapping(
item_id="tracks",
provider_domain=self.domain,
provider_instance=self.instance_id,
url="https://open.spotify.com/collection/tracks",
)
},
)

liked_songs.is_editable = False # TODO Editing requires special endpoints

liked_songs.metadata.images = [
MediaItemImage(
type=ImageType.THUMB, path="https://misc.scdn.co/liked-songs/liked-songs-64.png"
)
]

return liked_songs

async def get_library_playlists(self) -> AsyncGenerator[Playlist, None]:
"""Retrieve playlists from the provider."""
yield await self._get_liked_songs_playlist()
for item in await self._get_all_items("me/playlists"):
if item and item["id"]:
yield await self._parse_playlist(item)
Expand All @@ -252,8 +279,11 @@ async def get_track(self, prov_track_id) -> Track:

async def get_playlist(self, prov_playlist_id) -> Playlist:
"""Get full playlist details by id."""
playlist_obj = await self._get_data(f"playlists/{prov_playlist_id}")
return await self._parse_playlist(playlist_obj)
if prov_playlist_id == "tracks":
return await self._get_liked_songs_playlist()
else:
playlist_obj = await self._get_data(f"playlists/{prov_playlist_id}")
return await self._parse_playlist(playlist_obj)

async def get_album_tracks(self, prov_album_id) -> list[AlbumTrack]:
"""Get all album tracks for given album id."""
Expand All @@ -266,8 +296,11 @@ async def get_album_tracks(self, prov_album_id) -> list[AlbumTrack]:
async def get_playlist_tracks(self, prov_playlist_id) -> AsyncGenerator[PlaylistTrack, None]:
"""Get all playlist tracks for given playlist id."""
count = 1
uri = (
"me/tracks" if prov_playlist_id == "tracks" else f"playlists/{prov_playlist_id}/tracks"
)
for item in await self._get_all_items(
f"playlists/{prov_playlist_id}/tracks",
uri,
):
if not (item and item["track"] and item["track"]["id"]):
continue
Expand Down

0 comments on commit aeab888

Please sign in to comment.