Getting currently playing song with Spotipy gives error: "'Spotify' object has no attribute 'currently_playing'"

Getting currently playing song with Spotipy gives error: "'Spotify' object has no attribute 'currently_playing'"

我正在编写一个脚本,returns 您当前正在播放的来自 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)

即使PyPi claims that the version of Spotipy is the latest there, 2.4.4, in fact, it is not. I noticed that after installing Spotipy with pip, its source code is different from the head of the master branch on the GitHub。而且 PyPi 版本没有 currently_playing 方法。

对我有用的是 运行 pip uninstall spotipy 卸载 Spotipy 并直接从 GitHub:

重新安装它
pip install git+https://github.com/plamere/spotipy.git@master