How do I retrieve the current song name and time left from pianobar using python?

1.4k views Asked by At

I'm creating an alarm clock with a gui using TKinter and python on my raspberry pi. When the alarm goes off I want it to start playing pianobar. I can do this easily enough, but I also want to display the name of the song on the GUI, and I cannot figure out how to get the current song being played. I have tried using pipes "|", and redirecting with ">" but I have gotten nowhere. Any advice will help.

1

There are 1 answers

0
Scott Hansen On BEST ANSWER

You need to use the event command interface. From the man page:

An example script can be found in the contrib/ directory of pianobar's source distribution.

~/.config/pianobar:

user = <username>
password = <password> (although I'd suggest password_command)
event_command = ~/.config/pianobar/event_command.py

~/config/event_command.py

#!/usr/bin/env python

import os
import sys
from os.path import expanduser, join

path = os.environ.get('XDG_CONFIG_HOME')
if not path:
    path = expanduser("~/.config")
else:
    path = expanduser(path)
fn = join(path, 'pianobar', 'nowplaying')

info = sys.stdin.readlines()
cmd = sys.argv[1]

if cmd == 'songstart':
    with open(fn, 'w') as f:
        f.write("".join(info))

This will write the song information to ~/.config/pianobar/nowplaying when a new song starts (there are other events available in the manpage). You can then parse that using your choice of tools to acquire the song title.