Python Spotipy-如何从给定索引开始的播放列表中获取歌曲?

Python Spotipy-如何从给定索引开始的播放列表中获取歌曲?,python,pycharm,spotipy,Python,Pycharm,Spotipy,我在文档中读过关于偏移量参数的内容;然而,我不知道如何使用它。这是到目前为止我的代码。不幸的是,仅从播放列表中检索到前100首歌曲。如何更改索引以便从播放列表中检索更多歌曲 import os, re, shutil import spotipy import spotipy.util as util import time # Parameters username = 'REDACTED' client_id = 'REDACTED' client_secret = '

我在文档中读过关于偏移量参数的内容;然而,我不知道如何使用它。这是到目前为止我的代码。不幸的是,仅从播放列表中检索到前100首歌曲。如何更改索引以便从播放列表中检索更多歌曲

import os, re, shutil

import spotipy
import spotipy.util as util
import time

# Parameters
username      = 'REDACTED'
client_id     = 'REDACTED'
client_secret = 'REDACTED'
redirect_uri  = 'http://localhost/'
scope         = 'user-library-read'
playlist      = '17gneMykp6L6O5R70wm0gE'


def show_tracks(tracks):
    for i, item in enumerate(tracks['items']):
        track = item['track']
        myName = re.sub('[^A-Za-z0-9\ ]+', '', track['name'])
        dirName = "/Users/pschorn/Songs/" + myName + ".app"
        if os.path.exists(dirName):
            continue
            #shutil.rmtree(dirName)
        os.mkdir(dirName)
        os.mkdir(dirName + "/Contents")
        with open(dirName + "/Contents/PkgInfo", "w+") as f:
            f.write("APPL????")
        os.mkdir(dirName + "/Contents/MacOS")
        with open(dirName + "/Contents/MacOS/" + myName, "w+") as f:
            f.write("#!/bin/bash\n")
            f.write("osascript -e \'tell application \"Spotify\" to play track \"{}\"\'".format(track['uri']))
        os.lchmod(dirName + "/Contents/MacOS/" + myName, 0o777)

        myName = re.sub('\ ', '\\ ', myName)
        # I've installed a third-party command-line utility that
        # allows me to set the icon for applications.
        # If there's a way to do this from python, let me know.
        os.system(
            '/usr/local/bin/fileicon set /Users/pschorn/Songs/' + myName + '.app /Users/pschorn/Code/PyCharmSupport/Icon.icns')





token = util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)

if token:
    sp = spotipy.Spotify(auth=token)
    results = sp.user_playlist(username, playlist, fields="tracks,next")
    tracks = results['tracks', offset=100]
    show_tracks(tracks)

else:
    print("Can't get token for", username)


编辑:我已经知道如何从给定索引开始返回歌曲,甚至更多。你可以查看我的代码!它检索用户所有播放列表中的所有歌曲,并为每首歌曲创建一个可打开播放歌曲的应用程序。这样做的目的是让您可以直接从spotlight search播放Spotify歌曲

我编写的这个扩展Spotipy库提供的功能的自定义类有一个处理偏移的包装器函数

def user_playlist_tracks_full(spotify, user, playlist_id=None, fields=None, market=None):
    """ Get full details of the tracks of a playlist owned by a user.
        Parameters:
            - spotify - spotipy instance
            - user - the id of the user
            - playlist_id - the id of the playlist
            - fields - which fields to return
            - market - an ISO 3166-1 alpha-2 country code.
    """

    # first run through also retrieves total no of songs in library
    response = spotify.user_playlist_tracks(user, playlist_id, fields=fields, limit=100, market=market)
    results = response["items"]

    # subsequently runs until it hits the user-defined limit or has read all songs in the library
    while len(results) < response["total"]:
        response = spotify.user_playlist_tracks(
            user, playlist_id, fields=fields, limit=100, offset=len(results), market=market
        )
        results.extend(response["items"])

    return results
def user_playlist_tracks_full(spotify,user,playlist_id=None,fields=None,market=None):
“”“获取用户拥有的播放列表曲目的完整详细信息。
参数:
-spotify-spotipy实例
-用户-用户的id
-playlist\u id-播放列表的id
-字段-要返回的字段
-市场-ISO 3166-1 alpha-2国家代码。
"""
#首次运行还检索库中歌曲的总数
response=spotify.user\u playlist\u tracks(用户,playlist\u id,fields=fields,limit=100,market=market)
结果=响应[“项目”]
#随后运行,直到达到用户定义的限制或已读取库中的所有歌曲
而len(results)
这段代码可能足以说明您必须做什么,每次循环并更改偏移量

完整的类是,在本例中,我刚刚将
self
替换为
spotify