-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkinsky2m3u.py
48 lines (33 loc) · 1.24 KB
/
kinsky2m3u.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
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
# use python3
import re
import sys
from io import StringIO
from lxml import etree
from urllib.parse import unquote
if len(sys.argv) != 2:
print("please provide a filename of a Kinsky dpl/xml playlist")
print("on a Mac these can be found in ~/Library/Kinsky/Playlists/")
sys,exit(1)
fd = open(sys.argv[1],"r")
content = fd.read()
# replace item with linn:item to make lxml recognize it
content = content.replace("<item ","<linn:item ")
content = content.replace("</item>","</linn:item>")
# remove DIDL-Lite lines for lxml
content = re.sub(r'\s*</?DIDL-Lite.*?>', "", content)
namespaces={'l': 'urn:linn-co-uk/playlist', 'd': 'urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/', 'dc': 'http://purl.org/dc/elements/1.1/', 'upnp': 'urn:schemas-upnp-org:metadata-1-0/upnp/'}
tree = etree.parse(StringIO(content))
r = tree.xpath('/l:Playlist/l:Track/l:item/res', namespaces=namespaces)
# print(r[2].text)
for i in range(len(r)):
fname = r[i].text
fname = fname.replace("*","%")
fname = unquote(fname)
matchobj = re.match(r'.*DLNA-PNMP3-OP01-FLAGS.*', fname, re.I)
if matchobj:
continue
fname = re.sub(r'^http.*(Musicshare/|Syn3/|Musik/|minimserver/%/)', "", fname)
print(fname)
# print(str(i) + " :: " + fname)
# print(content)