【发布时间】:2019-12-22 04:26:06
【问题描述】:
我已经阅读了文档中关于 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 搜索中播放您的 Spotify 歌曲!
【问题讨论】: