jpu
jpu

Reputation: 13

Does Spotipy break after a certain number of requests? Hitting API too much?

I had a script that ran for about ~1hr.

df is a data frame created by rows I queried from my own Postgres database. I'm iterating over that to get the artist_name value and make the API call.

There are ~47k records in that table.

After an hour or so, the script stopped giving me any responses. There are no errors.

The line that breaks is results = artist_albums(...)

Putting a breakpoint() before works but once that line runs, It stops. No status errors, 429, etc...

Did I hit the Spotipy API too much?

for idx, val in enumerate(df['artist_name']):

    #get albums for each artist  
    results = artist_albums('spotify:artist:'+df['artist_name'], album_type='album')
    albums = results['items']
    while results['next']:
        results = spotify.next(results)
        albums.extend(results['items'])

    sleep(0.5)

    for album in albums:
        print(album['name'])
        try:
            q = (album['name'],
            album['id'],
            album['uri'],
            album['release_date'],
            album['release_date_precision'],
            album['total_tracks'],
            album['artists'][0]['id'],
            album['type'],
            album['external_urls']['spotify']
            )
            )
            cur.execute("""insert into schema.table values (
            %s, %s, %s, %s, %s,
            %s, %s, %s, %s)""", q)
            conn.commit()
    ```

Upvotes: 1

Views: 2302

Answers (1)

rob_med
rob_med

Reputation: 546

You have probably hit Spotiy API's rate limits, which works in a 30-seconds rolling window.

If your app makes a lot of Web API requests in a short period of time then it may receive a 429 error response from Spotify. This indicates that your app has reached our Web API rate limit. The Web API has rate limits in order to keep our API reliable and to help third-party developers use the API in a responsible way.

Spotify’s API rate limit is calculated based on the number of calls that your app makes to Spotify in a rolling 30 second window.

A way to avoid this would be to introduce some waiting time between API calls, for example using time.sleep, i.e.:

import time
time.sleep(10) # sleeps for 10 seconds

Upvotes: 1

Related Questions