MusicPlayerDaemon / MPD

Music Player Daemon
https://www.musicpd.org/
GNU General Public License v2.0
2.19k stars 350 forks source link

Can changes in the radio stream title be logged? #859

Closed kowalcj0 closed 4 years ago

kowalcj0 commented 4 years ago

Question

Is it possible to configure MPD so it logs changes in the radio stream title?

I'm aware that I can record radio streams using recorder plugin but that would simply gobble up the free space on my disk and also unnecessarily record (and transcode) locally played files.

Thanks

MaxKellermann commented 4 years ago

No, but you can write a small Python script which acts as a MPD client and logs all new tags. I suggest moving "special" features like this to a small and dedicated client.

kowalcj0 commented 4 years ago

Thanks @MaxKellermann

Here's an example python script that logs to system journal and to /home/mpd/.mpd/play.log.

pip install python-musicpd systemd-python
#!/usr/bin/env python3
# coding: utf-8
from datetime import datetime

from systemd import journal
import musicpd
from musicpd import ConnectionError

def append_to_mpd_play_log(entry: str):
    with open("/home/mpd/.mpd/play.log", "a+") as log:
        log.write(entry)

def main():
    cli = musicpd.MPDClient()
    try:
        cli.connect()
        if cli.status()["state"] == "play":
            song = cli.currentsong()
            entry = f"{str(datetime.utcnow())}-MPD logger: {song['name']} -> {song['title']}\n"
            journal.send(entry)
            append_to_mpd_play_log(entry)
        cli.disconnect()
    except ConnectionError:
        pass

if __name__ == '__main__':
    main()

I run it every minute with simple cron job:

crontab -e
* * * * * /home/mpd/scripts/mpdlogger.py