Sunday, February 5, 2012

Spotify: Changing and displaying song information using dbus interface

I used d-feet to get the information about the methods available to me.





spotify_getinfo.py

#!/usr/bin/python2

import dbus
bus = dbus.SessionBus()
player = bus.get_object('com.spotify.qt', '/')
iface = dbus.Interface(player, 'org.freedesktop.MediaPlayer2')
info = iface.GetMetadata()
# OUT: [dbus.String(u'xesam:album'), dbus.String(u'xesam:title'), dbus.String(u'xesam:trackNumber'), dbus.String(u'xesam:artist'), dbus.String(u'xesam:discNumber'), dbus.String(u'mpris:trackid'), dbus.String(u'mpris:length'), dbus.String(u'mpris:artUrl'), dbus.String(u'xesam:autoRating'), dbus.String(u'xesam:contentCreated'), dbus.String(u'xesam:url')]
print str(info['xesam:artist'][0]) + " - " + str(info['xesam:title'])

spotify_next.sh
#!/bin/bash

dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Next

spotify_prev.sh
#!/bin/bash

dbus-send --print-reply --session --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Previous

You can add global shortcuts. In openbox, I did:


    <keybind key="A-w">
      <action name="execute">
        
        <execute>/home/shadyabhi/spotify_next.sh</execute>
      </action>
    </keybind>
    <keybind key="A-S-w">
      <action name="execute">
        
        <execute>/home/shadyabhi/spotify_prev.sh</execute>

      </action>
</keybind>

You can also add the current song notification to your conky.

Spotify: ${color FFFFFF}${execi 1 ~/spotify_getinfo.py}

No comments:

Post a Comment