Skip to content

Instantly share code, notes, and snippets.

@bonelifer
Forked from Melanpan/mpd_icecast_update.py
Created March 16, 2024 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bonelifer/9c052b65991e150154525a4f7600f589 to your computer and use it in GitHub Desktop.
Save bonelifer/9c052b65991e150154525a4f7600f589 to your computer and use it in GitHub Desktop.
"""
Small script that syncs the current now playing (MPD) with Icecast
By Melan
"""
import time
import os
import requests
from mpd import MPDClient
UPDATE_RATE = 5 # Update rate in seconds
MPD_HOST = "localhost"
MPD_PORT = 6600
ICECAST_HOST = "localhost"
ICECAST_PORT = 8050
ICECAST_MNT = "/nurds"
ICECAST_AUTH_USR = "admin"
ICECAST_AUTH_PWD = "hackme"
prev_meta = ""
mpd = MPDClient()
mpd.connect(MPD_HOST, MPD_PORT)
def update_metadata(metadata, prev_meta):
if metadata == prev_meta:
return
url = "http://%s:%s/admin/metadata" % (ICECAST_HOST, ICECAST_PORT)
data_params = {"mount": ICECAST_MNT, "mode": "updinfo", "song": metadata} # mount=/mystream&mode=updinfo&song=ACDC+Back+In+Black
r = requests.get(url, params=data_params, auth=(ICECAST_AUTH_USR, ICECAST_AUTH_PWD))
while True:
metadata = None
mpd_status = mpd.status()
# When we are playing music
if mpd_status['state'] == "play":
current_song = mpd.currentsong()
# When the metadata contains both artist and title
if "artist" in current_song and "title" in current_song:
metadata = "%s - %s" % (current_song['artist'], current_song['title'])
# When the metadata only contains the title
elif "title" in current_song and not "artist" in current_song:
metadata = current_song['title']
# When the metadata only contains the artist
elif "artist" in current_song and not "title" in current_song:
metadata = current_song['artist']
# When it contains neither, fall back to the file name
else:
metadata = os.path.splitext(os.path.basename(current_song['file']))[:-1]
# If not display MPD status
else:
metadata = "MPD state: %s" % (mpd_status['state'])
if metadata:
update_metadata(metadata, prev_meta)
prev_meta = metadata
time.sleep(UPDATE_RATE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment