-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.py
34 lines (26 loc) · 1.1 KB
/
write.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json
from typing import List, Dict
import xml.etree.ElementTree as ET
from xml.dom import minidom
def xspf(data: Dict[str, List[Dict[str, str]]]) -> str:
playlist = ET.Element('playlist', xmlns='http://xspf.org/ns/0/', version='1')
ET.SubElement(playlist, 'title').text = 'Radios'
tracklist = ET.SubElement(playlist, 'trackList')
for broadcaster, stations in data.items():
for station in stations:
if station.get("working"):
track = ET.SubElement(tracklist, 'track')
ET.SubElement(track, 'creator').text = broadcaster
ET.SubElement(track, 'location').text = station['url']
ET.SubElement(track, 'title').text = station['radio']
ET.SubElement(track, 'album').text = station['radio']
xmlstr = minidom.parseString(ET.tostring(playlist)).toprettyxml(indent=' ')
return xmlstr
def main():
with open('radios.json', 'r') as f:
data = json.load(f)
xmlstr = xspf(data)
with open('new.xspf', 'w') as f:
print(xmlstr, file=f)
if __name__ == '__main__':
main()