使用Spotipy获取当前正在播放的歌曲会产生错误:“'Spotify'对象没有属性'currently_playing'”

时间:2019-03-26 14:20:11

标签: python spotify spotipy

我正在编写一个脚本,该脚本从Spotify返回您当前正在播放的歌曲。

我阅读了Spotify API的文档,并且一切正常,但是尝试实现某些模块时遇到了一些问题。

例如,这里https://spotipy.readthedocs.io/en/latest/说有一个名为currently_playing()的模块,但出现此错误:

Traceback (most recent call last):
  File "spotify_test.py", line 21, in <module>
    current_song = sp.currently_playing()
AttributeError: 'Spotify' object has no attribute 'currently_playing'

到目前为止,这是我的代码,当我更改范围和模块以获取用户的播放列表时,它可以很好地工作。因此令牌不是问题。

import sys
import spotipy
import spotipy.util as util

if len(sys.argv) > 1:
    username = sys.argv[1]
else:
    print('Usage %s Username ' % (sys.argv[0],))
    sys.exit()

scope = 'user-read-currently-playing'

token = spotipy.util.prompt_for_user_token(
    username, scope, redirect_uri='http://127.0.0.1/callback')

if token:
    sp = spotipy.Spotify(auth=token)
    current_song = sp.currently_playing()

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


print(current_song)

1 个答案:

答案 0 :(得分:1)

即使PyPi声称Spotipy的版本是最新的2.4.4,实际上也不是。我注意到,使用pip安装Spotipy之后,其源代码与GitHub上master分支的头部不同。而且PyPi版本没有currently_playing方法。

对我有用的是通过运行pip uninstall spotipy卸载Spotipy,然后直接从GitHub重新安装:

pip install git+https://github.com/plamere/spotipy.git@master