-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplaylist-export.py
More file actions
executable file
·49 lines (34 loc) · 1.31 KB
/
playlist-export.py
File metadata and controls
executable file
·49 lines (34 loc) · 1.31 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
"""
Export playlists to M3U files.
"""
import os
import sys
import xml.etree.ElementTree as ET
def main(inputFile, outputPath):
collection = ET.parse(inputFile).getroot()
for node in collection.iterfind('PLAYLISTS/NODE/SUBNODES/NODE'):
processNode(node, outputPath)
def processNode(node, outputPath):
nodeType = node.get('TYPE')
nodeName = node.get('NAME')
nodeOutputPath = os.path.join(outputPath, nodeName.replace(os.sep, '-'))
if nodeType == 'FOLDER':
if not os.path.exists(nodeOutputPath):
os.mkdir(nodeOutputPath)
for subnode in node.iterfind('SUBNODES/NODE'):
processNode(subnode, nodeOutputPath)
elif nodeType == 'PLAYLIST':
if nodeName in ['_LOOPS', '_RECORDINGS']:
return
outputFile = nodeOutputPath + '.m3u'
with open(outputFile, 'w') as file:
for track in node.iterfind('PLAYLIST/ENTRY/PRIMARYKEY[@TYPE="TRACK"]'):
path = track.get('KEY').replace('/:', os.sep)
path = path if path[1] == ':' else os.path.join(os.sep, 'Volumes', path)
file.write(path + '\n')
if __name__ == '__main__':
if len(sys.argv) < 3:
print('Usage: playlist-export <input> <output>')
sys.exit(1)
main(sys.argv[1], sys.argv[2])