#!/usr/bin/env python3 from pydbus import SessionBus import sys import urllib.parse import os.path bus = SessionBus() all_names = bus.get("org.freedesktop.DBus", "/org/freedesktop/DBus").ListNames() def writepart(s): sys.stdout.write(s.replace("&", "&").replace("<", "<").replace(">", ">") .replace("|", "|")) for name in all_names: if name.startswith("org.mpris.MediaPlayer2."): mpris = bus.get(name, "/org/mpris/MediaPlayer2") status = mpris.Get("org.mpris.MediaPlayer2.Player", "PlaybackStatus") metadata = mpris.Get("org.mpris.MediaPlayer2.Player", "Metadata") if status == "Playing": title = metadata.get("xesam:title", None) if title is None: url = metadata.get("xesam:url", None) if url is None: continue url = urllib.parse.urlparse(url) path = url.path title = os.path.basename(path) title = os.path.splitext(title)[0] artist = metadata.get("xesam:artist", None) if artist is None: artist = metadata.get("xesam:albumArtist", None) if artist is not None: artist = ", ".join(artist) album = metadata.get("xesam:album", None) if len(artist.strip()) == 0: artist = None if len(title.strip()) == 0: title = None if album is not None and len(album.strip()) == 0: album = None if artist is not None: writepart(artist) if title is not None: if artist is not None: sys.stdout.write(" - ") writepart(title) if album is not None: if artist is not None or title is not None: sys.stdout.write(" / ") writepart(album) if artist is not None or album is not None or title is not None: sys.stdout.write(" | iconName=multimedia-audio-player-symbolic\n") break sys.stdout.write("---\n")