def current_playlist_tracks(sp: spotipy.client.Spotify, playlist_id: str): playlist_cont = [] # Getting current songs in playlist to add to list for no duplicates results = sp.playlist_tracks(playlist_id=playlist_id) while len(playlist_cont) < results['total']: for tracks in results['items']: playlist_cont.append(tracks['track']['id']) results = sp.playlist_tracks(playlist_id=playlist_id, offset=len(playlist_cont)) return playlist_cont
def get_tracks_artist_info(sp: spotipy.client.Spotify, pl_uri: str) -> List[List[Dict]]: artists_info = list() # Start retrieving tracks from the beginning of the playlist offset = 0 pl_length = get_pl_length(sp, pl_uri) # Playlist track retrieval only fetches 100 tracks at a time, hence\ # the loop to keep retrieving until we reach the end of the playlist while offset != pl_length: # Get the next batch of tracks pl_tracks = sp.playlist_tracks(pl_uri, offset=offset, fields="items.track") # Get the list with the info about the artists of each track from the\ # latest batch and append it to the running list [ artists_info.append(pl_item["track"]["artists"]) for pl_item in pl_tracks["items"] ] # Update the offset offset += len(pl_tracks["items"]) return artists_info
def get_pl_length(sp: spotipy.client.Spotify, pl_uri: str) -> int: return sp.playlist_tracks(pl_uri, offset=0, fields="total")["total"]