-
Notifications
You must be signed in to change notification settings - Fork 5
/
icy-info.py
executable file
·93 lines (70 loc) · 2.15 KB
/
icy-info.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
#!/usr/bin/python2
#
# This script parses the mplayer standard output and
# extracts ICY info for the mot-encoder.
#
# Usage:
# mplayer <blablabla> | icy-info.py file.dls file-with-default.dls
#
# the file-with-default.dls contains DLS text to be sent when there
# is no ICY info
import re
import select
import sys
import time
re_icy = re.compile(r"""ICY Info: StreamTitle='([^']*)'.*""")
if len(sys.argv) < 3:
print("Please specify dls output file, and file containing default text")
sys.exit(1)
dls_file = sys.argv[1]
default_textfile = sys.argv[2]
def new_dlstext(text):
if text.strip() == "":
try:
fd = open(default_textfile, "r")
text = fd.read().strip()
fd.close()
except Exception as e:
print("Could not read default text from {}: {}".format(default_textfile, e))
print("New Text: {}".format(text))
fd = open(dls_file, "w")
fd.write(text)
fd.close()
wait_timeout = 5
nodls_timeout = 0
while True:
# readline is blocking, therefore we cannot send a default text
# after some timeout
new_data = sys.stdin.readline()
if not new_data:
break
match = re_icy.match(new_data)
if match:
artist_title = match.groups()[0]
new_dlstext(artist_title)
else:
print("{}".format(new_data.strip()))
if False:
# The select call creates a one ICY delay, and it's not clear why...
while True:
rfds, wfds, efds = select.select( [sys.stdin], [], [], wait_timeout)
if rfds:
# new data available on stdin
print("SELECT !")
new_data = sys.stdin.readline()
print("DATA ! {}".format(new_data))
if not new_data:
break
match = re_icy.match(new_data)
if match:
artist_title = match.groups()[0]
new_dlstext(artist_title)
else:
print("{}".format(new_data.strip()))
else:
# timeout reading stdin
nodls_timeout += 1
if nodls_timeout == 100:
new_dlstext("")
nodls_timeout = 0
time.sleep(.1)