diff --git a/.config/i3blocks/config b/.config/i3blocks/config index 7271bf7..c4fc86b 100644 --- a/.config/i3blocks/config +++ b/.config/i3blocks/config @@ -32,6 +32,9 @@ markup=pango [mpc] interval=5 +[spotify] +interval=5 + [mondash] instance=$env_MONDASH_PRIVATE interval=30 diff --git a/.config/i3blocks/spotify b/.config/i3blocks/spotify new file mode 100755 index 0000000..6fae62b --- /dev/null +++ b/.config/i3blocks/spotify @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import dbus + +from block import * + +ICON = "\uf1bc" + + +class Spotify(Block): + def __init__(self, icon=None, icon_color=None): + super().__init__(icon, icon_color) + + self.bus = dbus.SessionBus() + spotify_obj = self.bus.get_object( + "org.mpris.MediaPlayer2.spotify", + "/org/mpris/MediaPlayer2", + ) + self.spotify_props = dbus.Interface( + spotify_obj, + 'org.freedesktop.DBus.Properties', + ) + self.spotify_player = dbus.Interface( + spotify_obj, + 'org.mpris.MediaPlayer2.Player', + ) + + def execute(self): + if self.button_is(BTN_LEFT): + self.spotify_player.PlayPause() + pass + + if not self.is_playing(): + self.set_icon_color(COLOR_SECONDARY) + return "" + + return self.get_artist_title() + + def is_playing(self): + status = self.spotify_props.Get( + 'org.mpris.MediaPlayer2.Player', + 'PlaybackStatus', + ) + + return status == 'Playing' + + def get_artist_title(self): + meta = self.spotify_props.Get( + 'org.mpris.MediaPlayer2.Player', + 'Metadata', + ) + + return "{} - {}".format( + meta.get('xesam:artist')[0], + meta.get('xesam:title'), + ) + + +def main(): + block = Spotify(ICON) + block.render() + + +if __name__ == '__main__': + main()