我在文档中读过关于offset参数的内容;但是,我不知道如何使用它。这是我目前的密码。不幸的是,只有前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)
编辑:我已经知道如何从给定的索引开始返回歌曲,甚至更多。你可以查看我的代码here!它检索用户所有播放列表中的所有歌曲,并为每个歌曲创建一个应用程序,可以打开该应用程序来播放歌曲。这样做的目的是让您可以直接从spotlight search播放Spotify歌曲!你知道吗
我编写的这个扩展Spotipy库提供的功能的自定义类有一个处理偏移量的包装器函数。你知道吗
这段代码可能足以说明您必须做什么,每次循环并更改偏移量。你知道吗
完整的类是in a standalone gist that should work,在这个示例中,我刚刚用
spotify
替换了self
。你知道吗相关问题 更多 >
编程相关推荐