【发布时间】:2021-07-06 17:06:57
【问题描述】:
使用 discord.py 构建一个不和谐机器人,我集成了 Spotify API 以打印播放列表中的歌曲列表。
我现在的代码一次打印每首歌 1 个句子(总共 50 个),这非常低效并且意味着很多 ping。关于如何将其全部打印成 1 个大块或小于 50 个的任何想法?
@client.command(name="ukmusic")
async def playlist(ctx):
#Authortization for API usage
headers = {
"Authorization": "Bearer {}".format(oauth)
}
#These create the link that the program fetches (The playlist)
endpoint = "https://api.spotify.com/v1/playlists/37i9dQZEVXbLnolsZ8PSNw"
data = urlencode({"market": "GB"})
lookup_url = f"{endpoint}?{data}"
#This prints what the link looks like and the status code (200 if it works correctly)
print (lookup_url)
r = requests.get(lookup_url, headers = headers)
print (r.status_code)
#This prints out the playlist
await ctx.send("Here is what the good people of Britiania are listening to on Spotify")
em = discord.Embed(title = "Song - Artist - Album\n")
for item in r.json()['tracks']['items']:
await ctx.send(
item['track']['name'] + ' - ' +
item['track']['artists'][0]['name'] + ' - ' +
item['track']['album']['name']
)
【问题讨论】:
-
如果您有一个需要令牌的请求,最好包含该数据的样本。
标签: python discord.py spotipy