diff options
| author | euer <root@euer.krebsco.de> | 2012-12-20 03:19:28 +0100 | 
|---|---|---|
| committer | euer <root@euer.krebsco.de> | 2012-12-20 03:19:28 +0100 | 
| commit | 38dbb8ee3867060fddd427d1bb4e57ee0300c8bb (patch) | |
| tree | abb7b2ab988934d6ddfbdd0448517b3a957645bf /god/streams/bin/mpdstreams | |
| parent | 7c1b02b086a8377ad76a46c277a224150c5b85d6 (diff) | |
| parent | 09dc57b9d5f564d13f80707eefadd845a4aa9aec (diff) | |
Merge branch 'master' of github.com:krebscode/painload
Diffstat (limited to 'god/streams/bin/mpdstreams')
| -rwxr-xr-x | god/streams/bin/mpdstreams | 113 | 
1 files changed, 113 insertions, 0 deletions
| diff --git a/god/streams/bin/mpdstreams b/god/streams/bin/mpdstreams new file mode 100755 index 00000000..a2a5f77c --- /dev/null +++ b/god/streams/bin/mpdstreams @@ -0,0 +1,113 @@ +#!/usr/bin/python2 + +# 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" +host="lounge.mpd.shack" +url_file = os.environ.get("STREAM_DB", "../db/stream.db") +urls = [] +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","--host",host,"crossfade","5"], +            stdout=pipe_silent,stderr=pipe_silent) +    Popen(["mpc","--host",host,"repeat","yes"], +            stdout=pipe_silent,stderr=pipe_silent) +    Popen(["mpc","--host",host,"clear"], +            stdout=pipe_silent,stderr=pipe_silent) +    Popen(["mpc","--host",host,"add",stream_url], +            stdout=pipe_silent,stderr=pipe_silent).wait() +    Popen(["mpc","--host",host,"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","--host",host,"stop"], +            stdout=pipe_silent,stderr=pipe_silent) +         + +def running(): +    try: +        (out,err) = Popen(["mpc","--host",host,"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 """[32;1m get all available streams with [31;1;4m'/%(fil)s list'[m +    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) | 
