summaryrefslogtreecommitdiffstats
path: root/streams/mpdstreams
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2012-04-23 01:39:36 +0200
committermakefu <github@syntax-fehler.de>2012-04-23 01:39:36 +0200
commita67ab843c176b696d18dbf8512b562dd8c1a90b6 (patch)
treeb8c6b9ee1d28af998992443ed4a4dd2a5a15a0f6 /streams/mpdstreams
parent017bf7c08c3dc09993e5727b1dc631912c02c4c5 (diff)
streams: adding mpd connector with current database
Diffstat (limited to 'streams/mpdstreams')
-rwxr-xr-xstreams/mpdstreams113
1 files changed, 113 insertions, 0 deletions
diff --git a/streams/mpdstreams b/streams/mpdstreams
new file mode 100755
index 00000000..75d2af13
--- /dev/null
+++ b/streams/mpdstreams
@@ -0,0 +1,113 @@
+#!/usr/bin/python
+
+# this version cannot tell if a stream is running or just ordinary music
+import os
+import sys
+from subprocess import Popen, PIPE
+
+os.chdir(os.path.dirname(os.path.realpath(sys.argv[0])))
+pidfile = "/tmp/krebs.stream.pid"
+url_file = os.environ.get("STREAM_DB", "stream.db")
+urls = []
+#urls = [ url,f for (url,f) in open(url_file).readline() ]
+for line in open(url_file):
+ urls.append(line.split())
+#print urls
+mybin = sys.argv[0]
+cmd = sys.argv[1] if len(sys.argv) > 1 else "you-are-made-of-stupid"
+stream = sys.argv[2] if len(sys.argv) == 3 else "groove"
+pipe_silent = open("/dev/null","w")
+
+def urlForStream(stream):
+ for url, s in urls:
+ if s == stream:
+ return url
+
+def streamForUrl(url):
+ for u, s in urls:
+ if u == url:
+ return stream
+
+def startStream(stream_url):
+ Popen(["mpc","crossfade","5"],
+ stdout=pipe_silent,stderr=pipe_silent)
+ Popen(["mpc","repeat","yes"],
+ stdout=pipe_silent,stderr=pipe_silent)
+ Popen(["mpc","clear"],
+ stdout=pipe_silent,stderr=pipe_silent)
+ Popen(["mpc","load",stream_url],
+ stdout=pipe_silent,stderr=pipe_silent).wait()
+ Popen(["mpc","play"],
+ stdout=pipe_silent,stderr=pipe_silent)
+
+def start(stream):
+ ret = running()
+ if ret:
+ print "!! Stream `%s` already running !" % \
+ (ret)
+ else:
+ startStream(urlForStream(stream))
+ print "** Starting `%s`."% stream
+
+
+def stop():
+ ret = running()
+ if not ret:
+ print "!! No Stream running!"
+ else:
+ print "** Stopping `%s`" % ret
+ Popen(["mpc","stop"],
+ stdout=pipe_silent,stderr=pipe_silent)
+
+
+def running():
+ try:
+ (out,err) = Popen(["mpc","current"],stdout=PIPE,stderr=PIPE).communicate()
+ out = out.rstrip()
+ return out
+ except Exception as e:
+ return ""
+
+
+def slist():
+ for url, name in urls:
+ print "%s : %s" % (name, url)
+
+
+def shorthelp():
+ print "start|stop|restart|status|list [audio stream]"
+
+
+def longhelp():
+ print "Usage: %s" % mybin,
+ shorthelp
+ print """ get all available streams with '/%(fil)s list'
+ Examples:
+ %(fil)s list
+ %(fil)s start groove
+ %(fil)s switch deepmix
+ %(fil)s status
+ %(fil)s stop""" % {'fil': mybin}
+
+if cmd == "start":
+ start(stream)
+elif cmd == "stop":
+ stop()
+elif cmd == "switch" or cmd == "restart":
+ stop()
+ start(stream)
+elif cmd == "status":
+ ret = running()
+ if not ret:
+ print "** nothing running" # , e
+ else:
+ print "Now Playing: %s" % ret
+elif cmd == "list":
+ slist()
+elif cmd == "--help":
+ longhelp()
+elif cmd == "-h":
+ shorthelp()
+else:
+ print "unknown command `%s`" % cmd
+ print "try `%s` --help" % os.path.basename(mybin)