Skip to content

Instantly share code, notes, and snippets.

@nsbgn
Last active March 11, 2018 13:57
Show Gist options
  • Select an option

  • Save nsbgn/c8ca2c9ed08dfc6740aa4b165a6ccef6 to your computer and use it in GitHub Desktop.

Select an option

Save nsbgn/c8ca2c9ed08dfc6740aa4b165a6ccef6 to your computer and use it in GitHub Desktop.

Revisions

  1. slakkenhuis revised this gist Mar 11, 2018. No changes.
  2. slakkenhuis created this gist Mar 11, 2018.
    35 changes: 35 additions & 0 deletions mpris.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/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()