Skip to content

Instantly share code, notes, and snippets.

@FergusInLondon
Last active September 21, 2020 04:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save FergusInLondon/960ebd9c50d5abe1eafbead9ead7dc42 to your computer and use it in GitHub Desktop.
Save FergusInLondon/960ebd9c50d5abe1eafbead9ead7dc42 to your computer and use it in GitHub Desktop.
Retrieve data from a Media Player in Linux, via dbus. (Uses Python)
import dbus
class MediaPlayer:
"""Recieves state from a MediaPlayer using dbus."""
player_properties = False
def __init__(self, player_name):
# Get an instance of the dbus session bus, and retrieve
# a proxy object for accessing the MediaPlayer
session_bus = dbus.SessionBus()
player_proxy = session_bus.get_object(
'org.mpris.MediaPlayer2.%s' % player_name,
'/org/mpris/MediaPlayer2'
)
# Apply the interface 'org.freedesktop.DBus.Properties to
# the player proxy, allowing us to call .Get() and .GetAll()
self.player_properties = dbus.Interface(player_proxy, 'org.freedesktop.DBus.Properties')
"""
Retrieve the properties from the Player interface, return a
song string.
"""
def song_string(self):
props = self.player_properties.GetAll('org.mpris.MediaPlayer2.Player')
return "%s - %s (%s)" % (
props["Metadata"]["xesam:artist"][0],
props["Metadata"]["xesam:title"],
props["Metadata"]["xesam:album"]
)
"""
Retrieve properties from the MediaPlayer2 interface, return
whether a screen is maximised or not.
"""
def is_fullscreen(self):
props = self.player_properties.GetAll('org.mpris.MediaPlayer2')
return bool(props["Fullscreen"])
player = MediaPlayer('vlc')
print("Status: %s" % ("Do Not Disturb" if player.is_fullscreen() else "Available"))
print("Playing: %s" % (player.song_string()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment