Port mpd status from mpc CLI to dbus using mpd-mpris
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
efe929ead7
commit
463609a978
2 changed files with 1 additions and 72 deletions
|
@ -29,7 +29,7 @@ separator_block_width=15
|
|||
markup=pango
|
||||
|
||||
|
||||
[mpc]
|
||||
[mpd]
|
||||
interval=5
|
||||
|
||||
[spotify]
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import dbus
|
||||
|
||||
from block import *
|
||||
|
||||
ICON = "\uf001"
|
||||
|
||||
|
||||
class MPD(Block):
|
||||
def __init__(self, icon=None, icon_color=None):
|
||||
super().__init__(icon, icon_color)
|
||||
|
||||
self.bus = dbus.SessionBus()
|
||||
mpd_obj = self.bus.get_object(
|
||||
"org.mpris.MediaPlayer2.mpd",
|
||||
"/org/mpris/MediaPlayer2",
|
||||
)
|
||||
self.mpd_props = dbus.Interface(
|
||||
mpd_obj,
|
||||
'org.freedesktop.DBus.Properties',
|
||||
)
|
||||
self.mpd_player = dbus.Interface(
|
||||
mpd_obj,
|
||||
'org.mpris.MediaPlayer2.Player',
|
||||
)
|
||||
|
||||
def execute(self):
|
||||
if self.button_is(BTN_LEFT):
|
||||
self.mpd_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.mpd_props.Get(
|
||||
'org.mpris.MediaPlayer2.Player',
|
||||
'PlaybackStatus',
|
||||
)
|
||||
|
||||
return status == 'Playing'
|
||||
|
||||
def get_artist_title(self):
|
||||
meta = self.mpd_props.Get(
|
||||
'org.mpris.MediaPlayer2.Player',
|
||||
'Metadata',
|
||||
)
|
||||
|
||||
if meta.get('xesam:artist')[0] == 'unknown artist' and ' - ' in meta.get('xesam:title'):
|
||||
return self.safe_text(meta.get('xesam:title'))
|
||||
|
||||
return self.safe_text("{} - {}".format(
|
||||
meta.get('xesam:artist')[0],
|
||||
meta.get('xesam:title'),
|
||||
))
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
block = MPD(ICON)
|
||||
block.render()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in a new issue