Hello,
I'm currently writing a small Python program to analyze the audio features of saved songs with the Spotify Web API. Unfortunately, querying the audio features for each song takes a long time. Approx. 3-4 songs are analyzed per second. With 2500 stored songs the function takes a long time. As a beginner, I haven't found a way to speed up the function. Here is the source code of the function:
def avg_features(token, tracklist):
track_counter: int = 0
danceability = 0
energy = 0
loudness = 0
speechiness = 0
acousticness = 0
instrumentalness = 0
liveness = 0
valence = 0
tempo = 0
for track in tracklist:
query = f"https://api.spotify.com/v1/audio-features/{track}"
response=requests.get(query, headers={"Authorization": f"Bearer {token}"})
response=response.json()
danceability += response['danceability']
energy += response['energy']
loudness += response['loudness']
speechiness += response['speechiness']
acousticness += response['acousticness']
instrumentalness += response['instrumentalness']
liveness += response['liveness']
valence += response['valence']
tempo += response['tempo']
track_counter += 1
print(track_counter)
danceability /= track_counter
energy /= track_counter
loudness /= track_counter
speechiness /= track_counter
acousticness /= track_counter
instrumentalness /= track_counter
liveness /= track_counter
valence /= track_counter
tempo /= track_counter
feature_list: Dict[str, int] = {'danceability': danceability, 'energy': energy, 'loudness': loudness, 'speechiness': speechiness, 'acousticness': acousticness', 'instrumentalness': instrumentalness, 'liveness': liveness, 'valence': valence, 'tempo': tempo}
return feature_list
Does anyone have an idea how I can speed up the function or whether the query of audio features always takes a long time?
I would appreciate an answer very much.
Thanks in advance
Eric