Get current play track

EDIT: Let's try to clarify all this.

I am writing a python script and I want him to tell me the song that Spotify plays.

I tried looking for libraries that could help me, but did not find any that are still supported and working. I also looked at the Spotify web API, but it provides no way to get this information.

The only possible solution I found was to capture the title bar of my Spotify window (desktop application). But so far I have not done this.

So basically, I ask if anyone knows:

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

    OR

  • Any other way to extract this information from the Spotify desktop application or web client.


Original post:

I was busy with the idea of ​​a python status bar for a linux environment, nothing out of the ordinary, just a script considering my own use. What I'm trying to do right now is to show the current playable track from spotify (namely, artist and title).

There is nothing like that in their official web interface. I also did not find a third-party library that would do this. Most of the libraries found are either outdated because spotify released their current API, or they are based on the mentioned API, which does not do what I want.

I also read a bunch of similar questions, most of which had no answers or an outdated solution.

I thought about grabbing the window title as it fills in the information I need. But not only does this seem really confusing, I also have difficulties getting this to happen. I tried to get it by doing a combination of the linux xdotools and xprop commands inside my script.

It is worth mentioning that since I already use psutil lib to get other information, I already have access to determine the PID.

Any idea how I could do this?

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

Your help will be appreciated.

+5
source share
2 answers

The Linux Spotify client implements the D-Bus interface, called the MPRIS interface specification - Media Player Remote Interfacing.

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

You can access the name (and other metadata) from python as follows:

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'] 
+15
source

For windows:

The library is on github: https://github.com/XanderMJ/spotilib . Keep in mind that this is still ongoing.

Just copy the file and put it in your Python / Lib directory.

 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 () returns only the first artist. I started working on another spotimeta.py library to solve this problem. However, this does not work 100% yet.

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

If an error occurs, spotimeta.artists () will only return the first artist (found using spotilib.artist ())

+2
source

Source: https://habr.com/ru/post/1236658/


All Articles