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

Posted

技术标签:

【中文标题】使用 Spotipy 获取当前正在播放的歌曲会出现错误:“\'Spotify\' 对象没有属性 \'currently_playing\'”【英文标题】:Getting currently playing song with Spotipy gives error: "'Spotify' object has no attribute 'currently_playing'"使用 Spotipy 获取当前正在播放的歌曲会出现错误:“'Spotify' 对象没有属性 'currently_playing'” 【发布时间】:2019-08-16 23:16:54 【问题描述】:

我正在编写一个脚本,从 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)

【问题讨论】:

你试过current_user_playing_track()吗? 是的,很遗憾我收到了同样的错误:AttributeError: 'Spotify' object has no attribute 'current_user_playing_track'。我再次卸载并安装了spotipy,并检查了当前版本,即2.4.4 【参考方案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

【讨论】:

非常感谢!如您所说,直接从 github 卸载并安装后,它工作得很好!

以上是关于使用 Spotipy 获取当前正在播放的歌曲会出现错误:“'Spotify' 对象没有属性 'currently_playing'”的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 SpotifyAPI-NET 获取当前正在播放的歌曲

Google Cast:从音频设备获取当前正在播放的歌曲

如何使用 Python 在 Rhythmbox 中获取当前正在播放的歌曲

如何使用 Spotify Web API 获取当前播放/最近播放的歌曲

Apple Music API 获取当前播放的歌曲

持续实时获取当前在 Spotify 上播放的歌曲