Skip to content

Instantly share code, notes, and snippets.

@GeordieP
Last active August 29, 2015 14:04
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 GeordieP/9ddf991ded3c2a858170 to your computer and use it in GitHub Desktop.
Save GeordieP/9ddf991ded3c2a858170 to your computer and use it in GitHub Desktop.
Reads AAC stream .pls files of all the Di.fm channels, and puts their title and URL into an .XML file that MusicBee's radio station import can read
#!/usr/bin/python
import json
import urllib.request
class Main:
def __init__(self):
# XML Formatting Strings
xmlPre = '<?xml version="1.0" encoding="UTF-8"?><opml version="2.0"><head><title>MusicBee Radio Station List</title><dateCreated>Tue, 29 Jul 2014 13:37:00 GMT</dateCreated></head><body>\n'
xmlPost = '</body></opml>'
outputString = xmlPre
# Open main link, get a JSON object of retrieved data
data = urllib.request.urlopen('http://listen.di.fm/public1').read()
jsonData = json.loads(data.decode("utf8"))
# Loop through each channel
for obj in jsonData:
# Parse an object out of each channel's JSON data
data = urllib.request.urlopen(obj["playlist"]).read().decode("utf8").strip().splitlines()
# Add the URL and stream Title from the channel's object to the final output string
# Put genre, bitrate, and format into category. Use the title (Without the Di.fm branding) for genre
outputString += '<outline text="'+ data[3].split('Title1=')[1].replace("&", "and") +'" description="" category="'+ data[3].split('Title1=')[1].split('-')[1].strip().replace("&", "and") +'/AAC/64k/" type="link" url="'+ data[2].split('File1=')[1] +'" />\n'
# Add the rest of the necessary XML to the output string
outputString += xmlPost
outputFile = open("difm.xml", "w+")
outputFile.write(outputString)
outputFile.close()
main = Main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment