使用Spotipy在Spotify中将曲目添加到播放列表

时间:2016-08-25 07:57:07

标签: python spotify spotipy

我使用Spotify API的Python绑定列出艺术家的顶级曲目并将它们添加到播放列表中,但每次都失败,就像它排除了不同类型的输入一样。

New_Track_List是一个字符串,其中包含顶部曲目的输出查找:

1:URIs:

示例:" spotify:track:1pAyyxlkPuGnENdj4g7Y4f,spotify:track:7D2xaUXQ4DGY5JJAdM5mGP,spotify:track:74mG2xIxEUJwHckS0Co6jF,spotify:track:2rjqDPbLlbQRlcj8DVM9kn,"

使用URI我从函数

返回
sp.user_playlist_add_tracks(username, playlist_id=playlist, tracks=New_Track_List)

追溯:

spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/users/smokieprofile/playlists/40aijTeKoxo5u1VSS9E3UQ/tracks: You can add a maximum of 100 tracks per request.

字符串中只有20首曲目。

第二次尝试:跟踪ID:

示例:" 1pAyyxlkPuGnENdj4g7Y4f,7D2xaUXQ4DGY5JJAdM5mGP,74mG2xIxEUJwHckS0Co6jF,2rjqDPbLlbQRlcj8DVM9kn"

相同的追溯输出。

使用单个曲目ID

sp.user_playlist_add_tracks(username, playlist_id=playlist, tracks="spotify:track:74mG2xIxEUJwHckS0Co6jF")

尝试只添加一首曲目,给我这条消息:

spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/users/smokieprofile/playlists/40aijTeKoxo5u1VSS9E3UQ/tracks: Invalid track uri: spotify:track:s

仅使用Track ID的相同消息,就像它只检查传递的字符串的第一个字母一样。

    sp.user_playlist_add_tracks(username, playlist_id=playlist, tracks="7D2xaUXQ4DGY5JJAdM5mGP")

错误追溯:

spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/users/smokieprofile/playlists/40aijTeKoxo5u1VSS9E3UQ/tracks: Invalid track uri: spotify:track:7

1 个答案:

答案 0 :(得分:3)

这看起来像是python中的duck type和api中的错误消息。我想api要求你发送一个列表而不是一个字符串,但实际上并没有检查。问题是字符串也是可迭代的。

>>> tracks = "spotify:track:1pAyyxlkPuGnENdj4g7Y4f, spotify:track:7D2xaUXQ4DGY5JJAdM5mGP, spotify:track:74mG2xIxEUJwHckS0Co6jF, spotify:track:2rjqDPbLlbQRlcj8DVM9kn"
>>> len(tracks)
150

并且api实际上是一个轨道id列表(不是一串逗号分隔的轨道uris)和prepends' spotify:track:'所有ids面前:

>>> tracks = "spotify:track:74mG2xIxEUJwHckS0Co6jF"
>>> ["spotify:track:" + track for track in tracks][0]
'spotify:track:s'

因此,如果您给api一个轨道ID列表,它可能会起作用:

>>> tracks = ["1pAyyxlkPuGnENdj4g7Y4f", "7D2xaUXQ4DGY5JJAdM5mGP"]
>>> ["spotify:track:" + track for track in tracks][0]
'spotify:track:1pAyyxlkPuGnENdj4g7Y4f'