From b66a5e9cc1098e1ce2721ca8513cab0e41f1ab03 Mon Sep 17 00:00:00 2001 From: Monkey Do Date: Wed, 4 Dec 2024 15:07:12 +0100 Subject: [PATCH] Get all tracks from spotify playlist The spotipy playlist_items method (and underlying spotify api endpoint) only allows fetching 100 tracks at a time. To get all the tracks in a playlist, we need to call this endpoint repeatedly with an offset updated each loop --- troi/tools/spotify_lookup.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/troi/tools/spotify_lookup.py b/troi/tools/spotify_lookup.py index aebb3d3a..654429b4 100644 --- a/troi/tools/spotify_lookup.py +++ b/troi/tools/spotify_lookup.py @@ -177,7 +177,20 @@ def get_tracks_from_spotify_playlist(spotify_token, playlist_id): """ sp = spotipy.Spotify(auth=spotify_token, requests_timeout=10, retries=10) playlist_info = sp.playlist(playlist_id) - tracks = sp.playlist_items(playlist_id, limit=100) + + offset = 0 + tracks = [] + + # spotipy limits to 100 items for each call, so run iteratively with an offset until there are no more tracks + while True: + results = sp.playlist_items(playlist_id, limit=100, offset=offset) + if len(results['items']) == 0: + break + + tracks.extend(results['items']) + # set new offset for the next loop + offset = offset + len(results['items']) + name = playlist_info["name"] description = playlist_info["description"] @@ -185,9 +198,9 @@ def get_tracks_from_spotify_playlist(spotify_token, playlist_id): return tracks, name, description -def _convert_spotify_tracks_to_json(spotify_tracks): +def _convert_spotify_tracks_to_json(spotify_tracks: list): tracks = [] - for track in spotify_tracks["items"]: + for track in spotify_tracks: artists = track["track"].get("artists", []) artist_names = [] for a in artists: