spotipy

How to retrieve more than 50 records using Spotipy API


I'm using the Spotipy API to retrieve song data from Spotify. Here's my code:

import pandas as pd
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id='<my_client_id>',
                                                           client_secret='<my_client_secret'))

results = sp.search(q="artist:guns n' roses", limit=50)

d = []
for idx, track in enumerate(results['tracks']['items']):
    d.append (
        {
            'Track' : track['name'],
            'Album' : track['album']['name'],
            'Artist' : track['artists'][0]['name'],
            'Release Date' : track['album']['release_date'],            
            'Track Number' : track['track_number'],
            'Popularity' : track['popularity'],
            'Track Number' : track['track_number'],
            'Explicit' : track['explicit'],
            'Duration' : track['duration_ms'],
            'Audio Preview URL' : track['preview_url'],
            'Album URL' : track['album']['external_urls']['spotify']
        }
    )

pd.DataFrame(d)

Per the docs, it appears that Spotify has a limit of 50 records.

Is it possible to retrieve all records for a given string search? (e.g. by chunking requests, etc.)

Thanks!


Solution

  • The Spotify Web API can return a maximum of 1000 items. (In this example, it found 390 tracks, so it got all of them.)
    Here is the code to get them:

    import pandas as pd
    import spotipy
    from spotipy.oauth2 import SpotifyClientCredentials
    
    sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id='<my_client_id>',
                                                               client_secret='<my_client_secret>'))
    
    d = []
    
    total = 1 # temporary variable
    offset = 0
    
    while offset < total:
        results = sp.search(q="artist:guns n' roses",  type='track', offset=offset, limit=50)
        total = results['tracks']['total']
        offset += 50 # increase the offset
        for idx, track in enumerate(results['tracks']['items']):
            d.append (
                {
                    'Track' : track['name'],
                    'Album' : track['album']['name'],
                    'Artist' : track['artists'][0]['name'],
                    'Release Date' : track['album']['release_date'],            
                    'Track Number' : track['track_number'],
                    'Popularity' : track['popularity'],
                    'Track Number' : track['track_number'],
                    'Explicit' : track['explicit'],
                    'Duration' : track['duration_ms'],
                    'Audio Preview URL' : track['preview_url'],
                    'Album URL' : track['album']['external_urls']['spotify']
                }
            )
    
    pd.DataFrame(d)