获取 spotify 当前正在播放的曲目

Get spotify currently playing track

编辑:让我们试着澄清这一切。

我正在写一个 python 脚本,我想让它告诉我 Spotify 当前正在播放的歌曲。

我尝试寻找可以帮助我的库,但没有找到任何仍在维护和工作的库。 我还查看了 Spotify 的网站 API,但它没有提供任何获取该信息的方法。

我找到的唯一可能的解决方案是获取我的 Spotify(桌面应用程序)的标题 window。但到目前为止我没能做到。

基本上,我想问的是是否有人知道:


原文post:

我正在摆弄 linux 环境的 python 状态栏的想法,没什么特别的,只是一个适合我自己使用的脚本。我现在想做的是显示 spotify 当前正在播放的曲目(即艺术家和标题)。

他们的官方网站上似乎没有类似的东西 API。我还没有找到任何可以做到这一点的第三方图书馆。我发现的大多数库要么自 spotify 发布其当前 API 以来已被弃用,要么它们基于所说的 API 而不是我想要的。

我在这里也看到了一堆类似的问题,其中大部分没有答案,或者是不推荐使用的解决方案。

我考虑过获取 window 标题,因为它确实显示了我需要的信息。但这不仅看起来真的很复杂,而且我也很难做到这一点。我试图通过 运行 脚本中 linux 命令 xdotools 和 xprop 的组合来获取它。

值得一提的是,由于我已经在使用 psutil 库获取其他信息,因此我已经可以访问 spotify 的 PID。

知道我该怎么做吗?

如果我的方法是您能想到的唯一方法,知道如何让它真正起作用吗?

我们将不胜感激。

Linux 上的 Spotify 客户端实现了一个名为 MPRIS - 媒体播放器远程接口规范的 D-Bus 接口。

http://specifications.freedesktop.org/mpris-spec/latest/index.html

您可以像这样从 python 访问标题(和其他元数据):

import dbus
session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object("org.mpris.MediaPlayer2.spotify",
                                     "/org/mpris/MediaPlayer2")
spotify_properties = dbus.Interface(spotify_bus,
                                    "org.freedesktop.DBus.Properties")
metadata = spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")

# The property Metadata behaves like a python dict
for key, value in metadata.items():
    print(key, value)

# To just print the title
print(metadata['xesam:title'])

对于windows:

可以在 github: https://github.com/XanderMJ/spotilib 找到该库。请记住,这仍在进行中。

只需复制文件并将其放在您的 Python/Lib 目录中。

import spotilib
spotilib.artist() #returns the artist of the current playing song
spotilib.song() #returns the song title of the current playing song

spotilib.artist() return只是第一位艺术家。我开始研究另一个库 spotimeta.py 来解决这个问题。但是,这还没有达到 100%。

import spotimeta
spotimeta.artists() #returns a list of all the collaborating artists of the track

如果发生错误,spotimeta.artists() 将 return 仅第一个艺术家(通过 spotilib.artist 找到())