i have a code that creates a dataframe of the songs from a playlist. I tried to change it so it will ignore the 100 limit and i will recieve all the songs from the playlist, but it doesnt work.. can you help me please?
i tried this code: import pandas as pd import spotipy from spotipy.oauth2 import SpotifyClientCredentials
def analyze_playlist100(creator, playlist_id): # Initialize Spotify client client_credentials_manager = SpotifyClientCredentials(client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET") sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
# Create empty dataframe
playlist_features_list = ["artist", "album", "track_name", "track_id", "release_date", "popularity",
"danceability", "energy", "key", "loudness",
"mode", "speechiness", "instrumentalness", "liveness",
"valence", "tempo", "duration_ms", "time_signature"]
playlist_df = pd.DataFrame(columns=playlist_features_list)
# Initialize offset and limit
offset = 0
limit = 100
while True:
# Make API call with current offset
playlist = sp.user_playlist_tracks(creator, playlist_id, offset=offset, limit=limit)["items"]
if len(playlist) == 0:
# No more tracks, break the loop
break
for track in playlist:
# Create empty dict
playlist_features = {}
# Get metadata
playlist_features["artist"] = track["track"]["album"]["artists"][0]["name"]
playlist_features["album"] = track["track"]["album"]["name"]
playlist_features["track_name"] = track["track"]["name"]
playlist_features["track_id"] = track["track"]["id"]
playlist_features["release_date"] = track["track"]["album"]["release_date"]
playlist_features["popularity"] = track["track"]["popularity"]
# Get audio features
audio_features = sp.audio_features(playlist_features["track_id"])[0]
for feature in playlist_features_list[6:]:
playlist_features[feature] = audio_features[feature]
# Concatenate the dfs
track_df = pd.DataFrame(playlist_features, index=[0])
playlist_df = pd.concat([playlist_df, track_df], ignore_index=True)
# Increment offset by the limit for the next API call
offset += limit
return playlist_df