Spotipy: How to read more than 100 tracks from a playlist

╄→尐↘猪︶ㄣ 提交于 2019-12-03 17:10:05

Many of the spotipy methods return paginated results, so you will have to scroll through them to view more than just the max limit. I've encountered this most often when collecting a playlist's full track listing and consequently created a custom method to handle this:

def get_playlist_tracks(username,playlist_id):
    results = sp.user_playlist_tracks(username,playlist_id)
    tracks = results['items']
    while results['next']:
        results = sp.next(results)
        tracks.extend(results['items'])
    return tracks

Below is the user_playlist_tracks module used in spotipy. (notice it defaults to 100 limit).

Try setting the limit to 200.

def user_playlist_tracks(self, user, playlist_id = None,   fields=None,
    limit=100, offset=0):
    ''' Get full details of the tracks of a playlist owned by a user.

        Parameters:
            - user - the id of the user
            - playlist_id - the id of the playlist
            - fields - which fields to return
            - limit - the maximum number of tracks to return
            - offset - the index of the first track to return
    '''
    plid = self._get_id('playlist', playlist_id)
    return self._get("users/%s/playlists/%s/tracks" % (user, plid),
                limit=limit, offset=offset, fields=fields)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!