Skip to content

Instantly share code, notes, and snippets.

@shermozle
Created April 2, 2020 22:59
Show Gist options
  • Save shermozle/6455e04e3b75afd9663993b618fa76cc to your computer and use it in GitHub Desktop.
Save shermozle/6455e04e3b75afd9663993b618fa76cc to your computer and use it in GitHub Desktop.
Script to grab 6music "what's playing" info and update an Icecast stream's metadata
#!/usr/bin/python
import urllib, json, re, time
count = 0
sleep_timer = 10
while count <=5:
try:
clients = urllib.urlopen('http://admin:PASSWORD@192.168.1.128:8000/admin/listclients?mount=/6music')
except Exception as e:
print("Error connecting to icecast: " + str(e))
pattern = re.compile("<Listeners>0</Listeners>")
searchResult = pattern.search(clients.read())
if searchResult is None:
getUrl = 'https://polling.bbc.co.uk/radio/nhppolling/bbc_6music?&callback=nhprealtimepolling'
try:
response = urllib.urlopen(getUrl)
except Exception as e:
print("Get url error: " + str(e))
try:
data = response.read()
data = data[ data.index("(") + 1 : data.rindex(")")]
data = json.loads(data)
metadata = (data['packages']['richtracks'][0]['artist'] + ' - ' + data['packages']['richtracks'][0]['title']).encode('utf-8')
print(metadata)
except Exception as e:
print("Error:" + str(e))
# print(json.dumps(data))
try:
urllib.urlopen('http://admin:PASSWORD@192.168.1.128:8000/admin/metadata.xsl?song=' + str(urllib.quote_plus(metadata)) + '&mount=%2F6music&mode=updinfo&charset=UTF-8').read()
except Exception as e:
print("Well that didn't work eh: " + str(e))
else:
print("Nobody listening")
sleep_timer = 5
time.sleep(sleep_timer)
count = count + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment