-
Notifications
You must be signed in to change notification settings - Fork 11
/
shairport-metadata.py
106 lines (97 loc) · 3.21 KB
/
shairport-metadata.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import re, sys
import base64
import json
def start_item(line):
regex = r"<item><type>(([A-Fa-f0-9]{2}){4})</type><code>(([A-Fa-f0-9]{2}){4})</code><length>(\d*)</length>"
matches = re.findall(regex, line)
typ = matches[0][0].decode('hex')
code = matches[0][2].decode('hex')
length = int(matches[0][4])
return (typ, code, length)
def start_data(line):
try:
assert line == '<data encoding="base64">\n'
except AssertionError:
if line.startswith("<data"):
return 0
return -1
return 0
def read_data(line, length):
b64size = 4*((length+2)/3);
try:
data = base64.b64decode(line[:b64size])
except TypeError:
data = ""
pass
return data
def guessImageMime(magic):
if magic.startswith('\xff\xd8'):
return 'image/jpeg'
elif magic.startswith('\x89PNG\r\n\x1a\r'):
return 'image/png'
else:
return "image/jpg"
if __name__ == "__main__":
metadata = {}
fi = sys.stdin
while True:
line = sys.stdin.readline()
if not line: #EOF
break
sys.stdout.flush()
if not line.startswith("<item>"):
continue
typ, code, length = start_item(line)
data = ""
if (length > 0):
r = start_data(sys.stdin.readline())
if (r == -1):
continue
data = read_data(sys.stdin.readline(), length)
# Everything read
if (typ == "core"):
if (code == "asal"):
metadata['Album Name'] = data
elif (code == "asar"):
metadata['Artist'] = data
#elif (code == "ascm"):
# metadata['Comment'] = data
#elif (code == "asgn"):
# metadata['Genre'] = data
elif (code == "minm"):
metadata['Title'] = data
#elif (code == "ascp"):
# metadata['Composer'] = data
#elif (code == "asdt"):
# metadata['File Kind'] = data
#elif (code == "assn"):
# metadata['Sort as'] = data
#elif (code == "clip"):
# metadata['IP'] = data
if (typ == "ssnc" and code == "snam"):
metadata['snam'] = data
if (typ == "ssnc" and code == "prgr"):
metadata['prgr'] = data
if (typ == "ssnc" and code == "pfls"):
metadata = {}
print json.dumps({})
sys.stdout.flush()
if (typ == "ssnc" and code == "pend"):
metadata = {}
print json.dumps({})
sys.stdout.flush()
if (typ == "ssnc" and code == "prsm"):
metadata['pause'] = False
if (typ == "ssnc" and code == "pbeg"):
metadata['pause'] = False
if (typ == "ssnc" and code == "PICT"):
if (len(data) == 0):
print json.dumps({"image": ""})
else:
mime = guessImageMime(data)
print json.dumps({"image": "data:" + mime + ";base64," + base64.b64encode(data)})
sys.stdout.flush()
if (typ == "ssnc" and code == "mden"):
print json.dumps(metadata)
sys.stdout.flush()
metadata = {}