aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-x[-rw-r--r--]icy-info.py62
1 files changed, 48 insertions, 14 deletions
diff --git a/icy-info.py b/icy-info.py
index 2929ed0..f2db039 100644..100755
--- a/icy-info.py
+++ b/icy-info.py
@@ -1,37 +1,71 @@
#!/usr/bin/python2
#
-# Pipe mplayer into this script
+# 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
+
+# quite long, because it's only a fallback, and you don't
+# want to overwrite real ICY info
+wait_timeout=600
re_icy = re.compile(r"""ICY Info: StreamTitle='([^']*)'.*""")
if len(sys.argv) < 3:
- print("Please specify dls output file, and default text")
+ print("Please specify dls output file, and file containing default text")
sys.exit(1)
dls_file = sys.argv[1]
-default_text = sys.argv[2]
+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()
+
while True:
- new_data = sys.stdin.readline()
+ rfds, wfds, efds = select.select( [sys.stdin], [], [], wait_timeout)
- match = re_icy.match(new_data)
+ if rfds:
+ # new data available on stdin
+ new_data = sys.stdin.readline()
- if match:
- artist_title = match.groups()[0]
+ if not new_data:
+ break
- if artist_title.strip() == "":
- artist_title = default_text
+ match = re_icy.match(new_data)
- print("New artist: {}".format(artist_title))
+ if match:
+ artist_title = match.groups()[0]
+ new_dlstext(artist_title)
+ else:
+ print("{}".format(new_data.strip()))
- fd = open(dls_file, "w")
- fd.write(artist_title)
- fd.close()
else:
- print("{}".format(new_data.strip()))
+ # timeout reading stdin
+ new_dlstext("")
+
+ time.sleep(.1)