Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Grabs your current track metadata from your MPRIS-enabled media player on Linux and zephyrs it in sipbmp3 format.
#!/usr/bin/env python3
import dbus
import sys
from zwrite import zwrite
# You can find zwrite.py at https://gist.github.com/vulpicastor/809952a8a65c808a882f
import platform, getpass
BUS_PREFIX = "org.mpris.MediaPlayer2"
OBJECT_PATH = "/org/mpris/MediaPlayer2"
PLAYER_INTERFACE = BUS_PREFIX + ".Player"
DBUS_PROPERTIES = "org.freedesktop.DBus.Properties"
PLAYERS = {"spotify", "vlc", "clementine"}
ZWRITE_DEFAULTS = {"classname": "abcdefgh", # PLEASE SET YOUR CLASS NAME
"instance": "music",
"opcode": "auto",
"unauth": True}
ZWRITE_TEMPLATE = """\
{user} is playing:
@b{{{xesamtitle}}}
{xesamartist}
@i{{{xesamalbum}}}"""
USER = "@".join((getpass.getuser(), platform.node()))
def get_player_proxy(player_name, session_bus = dbus.SessionBus()):
return session_bus.get_object(".".join((BUS_PREFIX, player_name)), OBJECT_PATH)
def get_properties_interface(player_proxy):
return dbus.Interface(player_proxy, dbus_interface=DBUS_PROPERTIES)
def get_metadata(properties_interface):
return properties_interface.Get(PLAYER_INTERFACE, "Metadata")
def strip_key_colon(dictionary):
return {k.replace(":", ""): v for k, v in dictionary.items()}
def zwrite_metadata(metadata):
return zwrite(ZWRITE_TEMPLATE.format(user=USER, **metadata), zsig=metadata["xesamurl"], **ZWRITE_DEFAULTS)
def main(interface):
metadata = get_metadata(interface)
metadata["xesam:artist"] = ", ".join(metadata["xesam:artist"])
if metadata["mpris:trackid"]:
zwrite_metadata(strip_key_colon(metadata))
if __name__ == "__main__":
if len(sys.argv) > 1:
if sys.argv[1] in PLAYERS:
player = sys.argv[1]
else:
print("Player not currently supported.")
sys.exit(1)
else:
player = "spotify"
proxy = get_player_proxy(player)
interface = get_properties_interface(proxy)
main(interface)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment