获取Spotify当前正在播放的曲目 [英] Get spotify currently playing track

查看:153
本文介绍了获取Spotify当前正在播放的曲目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们尝试澄清所有这些.

我正在编写python脚本,希望它告诉我Spotify当前正在播放的歌曲.

I'm writing a python script, and I want it to tell me the song that Spotify is currently playing.

我尝试寻找可以帮助我但找不到任何仍在维护和正常工作的库. 我也浏览了Spotify的网络API,但是它没有提供任何获取信息的方法.

I've tried looking for libraries that could help me but didn't find any that are still maintained and working. I've also looked through Spotify's web API, but it does not provide any way to get that information.

我发现的唯一可能的解决方案是获取Spotify(桌面应用程序)窗口的标题.但是到目前为止,我还没有做到这一点.

The only potential solution I found would be to grab the title of my Spotify (desktop app) window. But I didn't manage to do that so far.

所以基本上,我要问的是是否有人知道:

So basically, what I'm asking is whether anyone knows :

  • 如何应用我已经尝试使用的方法(从程序中获取窗口的标题),可以是纯python方式,也可以是使用中间shell脚本.

  • How to apply the method I'm already trying to use (get the window's title from a program), either in pure python or using an intermediary shell script.

OR

从Spotify的桌面应用程序或Web客户端提取信息的任何其他方式.

Any other way to extract that information from Spotify's desktop app or web client.

原始帖子:

我对Linux环境下的python状态栏这个想法不满意,没有幻想,只是针对我自己的用法量身定制的脚本.我现在想做的是显示Spotify当前正在播放的曲目(即艺术家和标题).

I'm fiddling with the idea of a python status bar for a linux environment, nothing fancy, just a script tailored to my own usage. What I'm trying to do right now is to display the currently playing track from spotify (namely, the artist and title).

在他们的官方Web API中似乎没有类似的东西.我还没有找到可以做到这一点的任何第三方库.我发现的大多数库都因为Spotify发布了当前的API而已被弃用,或者它们基于所述的API不能满足我的要求.

There does not seem to be anything like that in their official web API. I haven't found any third party library that would do that either. Most libraries I found are either deprecated since spotify released their current API, or they are based on said API which does not do what I want.

我在这里也读了很多类似的问题,其中大多数都没有答案,或者不建议使用的解决方案.

I've also read a bunch of similar question in here, most of which had no answers, or a deprecated solution.

我考虑过要获取窗口标题,因为它确实显示了我所需的信息.但是,这似乎不仅令人费解,而且我也很难做到这一点.我试图通过在脚本中运行linux命令xdotools和xprop的组合来获得它.

I thought about grabbing the window title, since it does diplay the information I need. But not only does that seem really convoluted, I also have difficulties making this happen. I was trying to get it by running a combination of the linux commands xdotools and xprop inside my script.

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

It's worth mentionning that since I'm already using the psutil lib for other informations, I already have access to spotify's PID.

有什么想法我该怎么做?

Any idea how I could do that ?

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

And in case my method was the only one you can think of, any idea how to actually make it work ?

您的帮助将不胜感激.

推荐答案

Linux上的Spotify客户端实现了一个称为MPRIS的D-Bus接口-Media Player远程接口规范.

The Spotify client on Linux implements a D-Bus interface called MPRIS - Media Player Remote Interfacing Specification.

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

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

You could access the title (and other metadata) from python like this:



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']

这篇关于获取Spotify当前正在播放的曲目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆