Created
February 4, 2020 15:30
-
-
Save Melanpan/e344cd9162025871e71e0558928744c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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