Skip to content

Instantly share code, notes, and snippets.

@nsbgn
Last active March 11, 2018 13:57
Show Gist options
  • Save nsbgn/c8ca2c9ed08dfc6740aa4b165a6ccef6 to your computer and use it in GitHub Desktop.
Save nsbgn/c8ca2c9ed08dfc6740aa4b165a6ccef6 to your computer and use it in GitHub Desktop.
Monitor currently playing song, for statusbars
#!/usr/bin/env python3
# This script continuously outputs the currently playing song and play status.
# For use with i3blocks, polybar, yabar etc; to avoid polling
# Note that there is an MPRIS plugin to control and monitor MPV through
# DBUS, at https://github.com/hoyon/mpv-mpris
# See also:
# https://github.com/mariusor/mpris-ctl
# https://github.com/acrisci/playerctl
import sys
import gi
gi.require_version("Playerctl","1.0")
from gi.repository import Playerctl, GLib
player = Playerctl.Player(player_name="mpv")
def update(player, e=None):
m = dict(**player.props.metadata)
print("{icon} {title} ({artist} - {album})".format(
icon = "" if player.props.status == "Playing" else ""
, artist = " and ".join(m.get("xesam:artist", []))
, album = m.get("xesam:album", "Unknown")
, title = m.get("xesam:title", "Unknown")
)
)
sys.stdout.flush()
player.on("play", update)
player.on("pause", update)
player.on("metadata", update)
main = GLib.MainLoop()
main.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment