Search code examples
pythonapispotify

Speed up the query of audio features (Spotify Web API)


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


Solution

  • The time it takes can be justified meaningfully.

    Firstly, the use of a WEB API not suitable for making an abundant of calls, as in your case you make around 2500 calls to the API. Also, note that each API call has a variable latency time, so there is no fixed time on when the result would be returned. It is also possible for latency to degrade over time. Factors such as this attribute to the time it takes. According to my calculation, it should be taking you around 11 minutes for all the tracks meta-data to be completed.

    The solution is to use the Library. This is simply a wrapper for the WEB API. The benefit of this is that it has all the in-built functions to handle your query.

    Method to use audio_features(tracks=[]). See from the docs here