Skip to content

Instantly share code, notes, and snippets.

@wdog
Created October 9, 2018 09:34
Show Gist options
  • Save wdog/08e1d7c9c82e835cd4d6326734c41514 to your computer and use it in GitHub Desktop.
Save wdog/08e1d7c9c82e835cd4d6326734c41514 to your computer and use it in GitHub Desktop.
i3block for spotify
#!/usr/bin/python3
import dbus
import os
import sys
STATUS_TO_ICON = {
'Playing': '',
'Paused': '',
'Stopped': '',
}
BUS_NAME = 'org.mpris.MediaPlayer2.spotify'
OBJECT_PATH = '/org/mpris/MediaPlayer2'
PLAYER_INTERFACE = 'org.mpris.MediaPlayer2.Player'
PROPERTIES_INTERFACE = 'org.freedesktop.DBus.Properties'
try:
bus = dbus.SessionBus()
spotify = bus.get_object(BUS_NAME, OBJECT_PATH)
if os.environ.get('BLOCK_BUTTON'):
control_iface = dbus.Interface(spotify, PLAYER_INTERFACE)
if (os.environ['BLOCK_BUTTON'] == '1'):
control_iface.Previous()
elif (os.environ['BLOCK_BUTTON'] == '2'):
control_iface.PlayPause()
elif (os.environ['BLOCK_BUTTON'] == '3'):
control_iface.Next()
spotify_iface = dbus.Interface(spotify, PROPERTIES_INTERFACE)
props = spotify_iface.Get(PLAYER_INTERFACE, 'Metadata')
status = spotify_iface.Get( PLAYER_INTERFACE, 'PlaybackStatus')
status_icon = STATUS_TO_ICON.get(status, '?')
if (sys.version_info > (3, 0)):
print("[ " + status_icon + " ] " + str(props['xesam:artist'][0]) + " - " + str(props['xesam:title']) )
else:
print(props['xesam:artist'][0] + " - " + props['xesam:title']).encode('utf-8')
exit
#except dbus.exceptions.DBusException:
except dbus.DBusException as ex:
print (ex)
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment