Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script to fetch the meta data of the currently playing track in Spotify. Tested on Ubuntu.
#!/usr/bin/python
#
# now_playing.py
#
# Python script to fetch the meta data of the currently playing
# track in Spotify. This is tested on Ubuntu.
import dbus
bus = dbus.SessionBus()
try:
spotify = bus.get_object('com.spotify.qt', '/')
iface = dbus.Interface(spotify, 'org.freedesktop.MediaPlayer2')
meta_data = iface.GetMetadata()
artistname = ",".join(meta_data['xesam:artist'])
trackname = meta_data['xesam:title']
albumname = meta_data['xesam:album']
#Other fields are:
# 'xesam:trackNumber', 'xesam:discNumber','mpris:trackid',
# 'mpris:length','mpris:artUrl','xesam:autoRating','xesam:contentCreated','xesam:url'
print str(trackname + " | " + artistname + " | " + albumname + " | Unknown")
except dbus.exceptions.DBusException:
print "Spotify is not running."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment