1
0
mirror of https://github.com/dcarrillo/dotfiles.git synced 2024-11-14 21:41:13 +00:00
dotfiles/.config/polybar/scripts/spotify.py

36 lines
986 B
Python
Raw Normal View History

2019-07-18 16:45:46 +00:00
#!/usr/bin/env python3
import dbus
bus_name = 'org.mpris.MediaPlayer2.spotify'
def get_spotify_song():
output = ''
try:
session_bus = dbus.SessionBus()
spotify_bus = session_bus.get_object(bus_name, '/org/mpris/MediaPlayer2')
spotify_properties = dbus.Interface(spotify_bus, 'org.freedesktop.DBus.Properties')
metadata = spotify_properties.Get("org.mpris.MediaPlayer2.Player", "Metadata")
artist = metadata['xesam:artist'][0]
title = metadata['xesam:title']
window_title = artist + ' - ' + title
2022-03-05 12:00:21 +00:00
output = "%{A1:$WM_CONTROL '" + window_title + "' &:}" + window_title + "%{A}"
2019-07-18 16:45:46 +00:00
except dbus.DBusException as e:
if (e.get_dbus_message() == f'The name {bus_name} was not provided by any .service files'):
pass
else:
output = e.get_dbus_message()
except Exception as e:
output = str(e)
print(output)
if __name__ == "__main__":
get_spotify_song()