From 5c8e9386f879394f81aa24460de7a820563657dc Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 20 Dec 2012 10:46:35 +0700 Subject: //claws -> //god/claws claws provides interaction with the real world --- god/claws/.gitignore | 1 + god/claws/Makefile | 18 ++ god/claws/README.md | 12 ++ god/claws/communication.h | 25 +++ god/claws/rcontrol.c | 479 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 535 insertions(+) create mode 100644 god/claws/.gitignore create mode 100644 god/claws/Makefile create mode 100644 god/claws/README.md create mode 100644 god/claws/communication.h create mode 100644 god/claws/rcontrol.c (limited to 'god') diff --git a/god/claws/.gitignore b/god/claws/.gitignore new file mode 100644 index 00000000..5da5924f --- /dev/null +++ b/god/claws/.gitignore @@ -0,0 +1 @@ +rcontrol diff --git a/god/claws/Makefile b/god/claws/Makefile new file mode 100644 index 00000000..abfae5eb --- /dev/null +++ b/god/claws/Makefile @@ -0,0 +1,18 @@ +# Makefile for the rcontrol program +# We set a setuid bit as this tools needs root privilleges to open the FT232 device. + +BINARY=./rcontrol + +install: all + ln -snf $$PWD/$(BINARY) ../bin/ + +all:$(BINARY) + chown root.root $(BINARY) + chmod +s $(BINARY) + +$(BINARY): rcontrol.c + #punani install libftdi1 libftdi-dev + gcc -Wall -lusb -lftdi rcontrol.c -o $(BINARY) + +clean: + rm -f $(BINARY) diff --git a/god/claws/README.md b/god/claws/README.md new file mode 100644 index 00000000..00c90a85 --- /dev/null +++ b/god/claws/README.md @@ -0,0 +1,12 @@ +# CLAWS + +Claws is a tool to manage some obscure Hardware Relay Board currently attached +to the krebs infrastructure + +# Installation + + make install + +# Usage + + rcontrol --help diff --git a/god/claws/communication.h b/god/claws/communication.h new file mode 100644 index 00000000..db4b2c36 --- /dev/null +++ b/god/claws/communication.h @@ -0,0 +1,25 @@ +/* These are the command codes of the relay card. If you change something here you must + * recompile the firmware and the control tool. */ +#ifndef _COMMUNICATION_H +#define _COMMUNICATION_H + +#define COMMAND_RELAY_ON 0x01 +#define COMMAND_RELAY_OFF 0x02 +#define COMMAND_RELAY_TOGGLE 0x04 +#define COMMAND_RELAY_SET 0x08 +#define COMMAND_RELAY_TIME_ON 0x10 +#define COMMAND_RELAY_TIME_OFF 0x20 +#define COMMAND_RELAY_TIME_CYCLIC 0x40 +#define COMMAND_GET_STATUS 0x80 +#define COMMAND_DEL_TIMERS 0x81 +#define COMMAND_SETUP_REMOTE 0x82 + + +#define RESPONSE_OK 0xff +#define RESPONSE_INVALID_COMMAND 0xfe +#define RESPONSE_INVALID_ARGUMENT 0xfd +#define RESPONSE_TRANSMISSION_ERROR 0xfc + +#define COMMANDO_LENGTH 4 + +#endif diff --git a/god/claws/rcontrol.c b/god/claws/rcontrol.c new file mode 100644 index 00000000..972c97d0 --- /dev/null +++ b/god/claws/rcontrol.c @@ -0,0 +1,479 @@ +/* +----------------------------------------------------------------------+ + * | relay control program | + * | by mgr, 2007 | + * | last change: 2009-01-05 | + * | | + * | This program is used to control the relay card version 1.0. For more | + * | information have a look at the project homepage. | + * | You will need libftdi in order to compile this tool. | + * | | + * | NOTE: For some reason the -l option causes a program crash if I | + * | compile this program with -O2. On top of that this code seems to | + * | be quite optimal, the program size gets a little larger with -O2, | + * | so I suggest you just compile it without optimization. | + * +----------------------------------------------------------------------+ + */ + +#include +#include +#include +#include +#include +#include +#include +#include // libftdi + +/* Notice that if you experiment with the baud rate, you will have to adapt + * the firmware, too. Also I do not recommend it, as 9600 Bauds are completely + * sufficient for this application. */ +#define BAUD 9600 + +/* If you are using more than one FT232-based pieces of hardware at once, + * we need a way to uniquely address any given one. This is done by the + * serial of the specific device which you can pass to this tool or specify + * here. If no serial is specified (NULL), the first found device is opened. */ +#define DEFAULT_FT_SERIAL "A6TMRSS6" + +#define VERSION "1.0" + +#define OPTION_ADDRESS 0x01 +#define OPTION_INTERVAL 0x04 +#define OPTION_CYCLIC 0x08 +#define OPTION_ON 0x10 +#define OPTION_OFF 0x20 +#define OPTION_TOGGLE 0x40 +#define OPTION_DEL_TIMERS 0x80 +#define OPTION_LIST_DEVICES 0x200 + +#define EXIT_CODE_OK 0 +#define EXIT_CODE_FAILURE 1 + +#include "communication.h" + +/* function prototypes */ +void usage(char* name); +void version(void); +const char* card_strerror(int error); +int valid_argument(const char* str); +void exit_gracefully(struct ftdi_context* ftdic, char exit_code); + + +int main(int argc, char **argv) +{ + int ret=0, int_argument=0, option_flags=0, long_index=0, i=0, num_ops=0; + char c=0; + unsigned char buf[COMMANDO_LENGTH], char_argument=0, operation=0; + const char* ft_serial=DEFAULT_FT_SERIAL; + double double_argument; + char buf0[64], buf1[64], buf2[64]; + time_t start_time; + + //struct ftdi_eeprom eeprom; + + struct ftdi_context ftdic; + struct ftdi_device_list *devlist=NULL, *curdev=NULL; + + static struct option long_options[] = + { + {"help", 0, 0, '?'}, + {"version", 0, 0, 'V'}, + {"on", 1, 0, 'o'}, + {"off", 1, 0, 'f'}, + {"toggle", 1, 0, 't'}, + {"set", 1, 0, 's'}, + {"status", 0, 0, 'S'}, + {"interval", 1, 0, 'v'}, + {"cyclic", 0, 0, 'c'}, + {"address", 1, 0, 'a'}, + {"delete-timers", 0, 0, 'd'}, + {"list-devices", 0, 0, 'l'}, + {0, 0, 0, 0 } + }; + + /* fetch the command line options */ + while ((c = getopt_long_only(argc, argv, "?Vo:f:t:s:Sv:ca:dl", long_options, + &long_index)) != -1) + { + switch (c) + { + case 'o': case 'f': case 't': + int_argument = atoi(optarg); + + if (int_argument > 6 || int_argument < 1 || !valid_argument(optarg)) + { + fprintf(stderr, "%s: -s: invalid value `%s' (1-6 is valid)\n", + *argv, optarg); + return EXIT_CODE_FAILURE; + } + + char_argument = (unsigned char)int_argument; + + if (c == 't') + { + operation = COMMAND_RELAY_TOGGLE; + option_flags |= OPTION_TOGGLE; + } else if (c == 'o') + { + operation = COMMAND_RELAY_ON; + option_flags |= OPTION_ON; + } else + { + operation = COMMAND_RELAY_OFF; + option_flags |= OPTION_OFF; + } + + num_ops++; + + break; + + case 's': + int_argument = atoi(optarg); + + if (int_argument > (1 << 6)-1 || int_argument < 0 || !valid_argument(optarg)) + { + fprintf(stderr, "%s: -s: invalid value `%s'\n", *argv, optarg); + return EXIT_CODE_FAILURE; + } + + char_argument = (unsigned char)int_argument; + operation = COMMAND_RELAY_SET; + num_ops++; + break; + + case 'S': + operation = COMMAND_GET_STATUS; + num_ops++; + break; + + case 'v': + double_argument = atof(optarg); + int_argument = (int)(double_argument*60) /10; + + if (int_argument < 1 || int_argument > (1 << 16)-1) + { + fprintf(stderr, "%s: -i: invalid interval `%s'\n", *argv, optarg); + return EXIT_CODE_FAILURE; + } + + option_flags |= OPTION_INTERVAL; + break; + + case 'c': + option_flags |= OPTION_CYCLIC; + break; + + case 'd': + operation = COMMAND_DEL_TIMERS; + option_flags |= OPTION_DEL_TIMERS; + num_ops++; + break; + + case 'a': + if (strlen(optarg) != 8) + { + fprintf(stderr, "%s: -s: invalid serial number `%s'\n", *argv, optarg); + return EXIT_CODE_FAILURE; + } + + ft_serial = optarg; + option_flags |= OPTION_ADDRESS; + break; + + case 'l': + option_flags |= OPTION_LIST_DEVICES; + break; + + case 'V': + version(); + break; + case '?': default: + usage(*argv); + break; + } + } + + /* check whether the command line options are valid */ + if ((option_flags & OPTION_INTERVAL)) + { + if (option_flags & OPTION_DEL_TIMERS) + { + fprintf(stderr, "%s: -d cannot be mixed with timing options\n", *argv); + usage(*argv); + } + + if (option_flags & OPTION_CYCLIC) + { + operation = COMMAND_RELAY_TIME_CYCLIC; + } else if (option_flags & OPTION_ON) + { + operation = COMMAND_RELAY_TIME_ON; + } else if (option_flags & OPTION_OFF) + { + operation = COMMAND_RELAY_TIME_OFF; + } else + { + fprintf(stderr, "%s: -v: you must also specify an operation (-o or -f)\n", *argv); + usage(*argv); + } + } + + if (!operation && !(option_flags & OPTION_LIST_DEVICES)) + { + usage(*argv); + } + + if (((option_flags & OPTION_DEL_TIMERS) && (operation != COMMAND_DEL_TIMERS))) + { + fprintf(stderr, "%s: invalid mixture of options\n", *argv); + usage(*argv); + } + + if (num_ops > 1) + { + fprintf(stderr, "%s: more than one operation specified\n", *argv); + usage(*argv); + } + + if (ftdi_init(&ftdic) < 0) + { + fprintf(stderr, "%s: unable to initialize FTDI context: %d (%s)\n", *argv, ret, + ftdi_get_error_string(&ftdic)); + return EXIT_CODE_FAILURE; + } + + /* list all found FT232 devices */ + if (option_flags & OPTION_LIST_DEVICES) + { + printf("scanning for FT232 devices...\n" + "you can address the devices using `%s -a '\n", *argv); + + if ((ret = ftdi_usb_find_all(&ftdic, &devlist, 0x0403, 0x6001)) < 0) + { + fprintf(stderr, "%s: unable to scan devices: %d (%s)\n", *argv, ret, + ftdi_get_error_string(&ftdic)); + exit(EXIT_CODE_FAILURE); + } + + if (ret == 0) + { + printf(" no devices found :(\n"); + return EXIT_CODE_OK; + } + + for (i=0, curdev = devlist; curdev != NULL; i++) + { + if (ftdi_usb_get_strings(&ftdic, curdev->dev, buf0, sizeof(buf0)/sizeof(char), + buf1, sizeof(buf1)/sizeof(char), buf2, sizeof(buf2)/sizeof(char)) < 0) + { + fprintf(stderr, "unable to fetch information for device #%i: %s\n", i, + ftdi_get_error_string(&ftdic)); + // continue caused an endless loop in case of an error + break; + } + + printf("\ndevice #%i%s:\n" + " manufacturer: %s\n" + " device: %s\n" + " serial: %s\n", i, (i == 0 ? " (default)" : ""), + (buf0 != NULL ? buf0 : "n/a"), (buf1 != NULL ? buf1 : "n/a"), + (buf2 != NULL ? buf2 : "n/a")); + + curdev = curdev->next; + } + + ftdi_list_free(&devlist); + return EXIT_CODE_OK; + } + + /* Try to open the specified device. If that fails, we take a long shot + * and open the first found FT232 device and assume its the relay card. + * We don't do this if an address was specified with the -a option. */ + if ((ret = ftdi_usb_open_desc(&ftdic, 0x0403, 0x6001, NULL, ft_serial)) < 0) + { + fprintf(stderr, "%s: unable to open ftdi device: %d (%s)\n", *argv, ret, + ftdi_get_error_string(&ftdic)); + exit(EXIT_CODE_FAILURE); + } + + /* get rid of any data still floating around the buffer */ + ftdi_usb_reset(&ftdic); + ftdi_usb_purge_buffers(&ftdic); + + + if ((ret = ftdi_set_baudrate(&ftdic, BAUD)) < 0) + { + fprintf(stderr, "%s: unable to set baudrate: %d (%s)\n", *argv, ret, + ftdi_get_error_string(&ftdic)); + exit_gracefully(&ftdic, EXIT_CODE_FAILURE); + } + + if ((ret = ftdi_set_line_property(&ftdic, 8, 2, NONE)) < 0) + { + fprintf(stderr, "%s: unable to set line property: %d (%s)\n", *argv, ret, + ftdi_get_error_string(&ftdic)); + exit_gracefully(&ftdic, EXIT_CODE_FAILURE); + } + + if ((ret = ftdi_setflowctrl(&ftdic, SIO_RTS_CTS_HS)) < 0) { + fprintf(stderr, "%s: unable to setup flow control: %d (%s)\n", *argv, ret, + ftdi_get_error_string(&ftdic)); + exit_gracefully(&ftdic, EXIT_CODE_FAILURE); + } + + /*if ((ret = ftdi_set_latency_timer(&ftdic, 10)) < 0) + { + fprintf(stderr, "%s: unable to set latency timer: %d (%s)\n", *argv, ret, + ftdi_get_error_string(&ftdic)); + exit_gracefully(&ftdic, EXIT_CODE_FAILURE); + }*/ + + buf[0] = operation; + buf[2] = 0; buf[3] = 0; buf[4] = 0; + + switch (operation) + { + case COMMAND_RELAY_SET: + buf[1] = char_argument; + break; + + case COMMAND_RELAY_ON: case COMMAND_RELAY_OFF: + case COMMAND_RELAY_TOGGLE: + buf[1] = (1 << (char_argument-1)); + break; + + case COMMAND_RELAY_TIME_ON: + case COMMAND_RELAY_TIME_OFF: + case COMMAND_RELAY_TIME_CYCLIC: + buf[1] = char_argument-1; + buf[2] = (int_argument & 0xff); // low byte + buf[3] = (int_argument >> 8); // high byte + break; + + default: + break; + } + + /* These values might not make much sense are vital to the correct + * funtion of this program, so better don't touch them. */ + ftdi_write_data_set_chunksize(&ftdic, 1); + ftdi_read_data_set_chunksize(&ftdic, 4); + + /* send the command */ + if (ftdi_write_data(&ftdic, buf, COMMANDO_LENGTH) != COMMANDO_LENGTH) + { + fprintf(stderr, "%s: unable to send command: %s\n", *argv, + ftdi_get_error_string(&ftdic)); + exit_gracefully(&ftdic, EXIT_CODE_FAILURE); + } + + /* Read the card's response. */ + start_time = time(NULL); + while ((ret = ftdi_read_data(&ftdic, buf, 1)) == 0) { + usleep(500); + if (time(NULL)-start_time >= 2) { + fprintf(stderr, "%s: unable to read card response, the operation might have " + "failed\n", *argv); + exit_gracefully(&ftdic, EXIT_FAILURE); + } + } + + if (operation == COMMAND_GET_STATUS && buf[0] <= ((1 << 7)-1)) + { + printf("relay status: %i (0b%s%s%s%s%s%s)\n", buf[0], + (buf[0] & (1 << 5)) ? "1" : "0", + (buf[0] & (1 << 4)) ? "1" : "0", + (buf[0] & (1 << 3)) ? "1" : "0", + (buf[0] & (1 << 2)) ? "1" : "0", + (buf[0] & (1 << 1)) ? "1" : "0", + (buf[0] & (1 << 0)) ? "1" : "0"); + exit_gracefully(&ftdic, EXIT_CODE_OK); + } + + if (buf[0] != RESPONSE_OK) + { + fprintf(stderr, "%s: relay card returned: %s\n", *argv, card_strerror(buf[0])); + exit_gracefully(&ftdic, EXIT_FAILURE); + } + + /* we can exit now */ + exit_gracefully(&ftdic, EXIT_CODE_OK); + return 0; // to make the compiler happy +} + +void exit_gracefully(struct ftdi_context* ftdic, char exit_code) +{ + ftdi_usb_purge_buffers(ftdic); + ftdi_usb_close(ftdic); + ftdi_deinit(ftdic); + + exit(exit_code); +} + +int valid_argument(const char* str) +{ + int i; + + for (i=0; i: ......... switch relay on\n" + " -f/--off : ........ switch relay off \n" + " -t/--toggle : ..... toggle relay \n" + " -s/--set : ....... set the status of all relays to \n" + " -S/--status: ........... get the current relay status\n" + " -v/--interval .. specify a timing interval (in minutes)\n" + " -c/--cyclic: ........... makes a timing operation (-v) cyclic\n" + " -a/--address .. addresses a specific card if multiple are installed\n" + " -d/--delete-timers ..... deletes all active timers\n" + " -l/--list-devices ...... lists all found FT232 devices\n\n"); + exit(0); +} + +void version(void) +{ + printf("\nThis is the relay control program version %s ($Revision: 26 $)\n" + "----------------------------------------------------------------\n" + " written by Michael Gross, 2007\n" + " binary compiled: %s %s\n\n" + "This program can be redistributed under the terms of the GNU GPL version 2\n" + "or later. For more information about this software and the hardware, visit\n" + "my homepage at http://www.coremelt.net. As usual with free software, there\n" + "is ABSOLUTELY NO WARRANTY. For details, refer to the GPL.\n\n", VERSION, + __DATE__, __TIME__); + + exit(0); +} -- cgit v1.2.3 From 21bb561b81d6c395ce0b004ce3b70a02c00360ef Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 20 Dec 2012 10:50:50 +0700 Subject: //streams -> //god/streams --- god/streams/Makefile | 16 +++++ god/streams/README | 10 ++++ god/streams/mpdstreams | 114 +++++++++++++++++++++++++++++++++++ god/streams/relaxxapi.py | 140 +++++++++++++++++++++++++++++++++++++++++++ god/streams/relaxxplayer.api | 13 ++++ god/streams/relaxxstreams | 107 +++++++++++++++++++++++++++++++++ god/streams/stream-starter | 19 ++++++ god/streams/stream.db | 35 +++++++++++ god/streams/streams | 131 ++++++++++++++++++++++++++++++++++++++++ god/streams/streams.py | 116 +++++++++++++++++++++++++++++++++++ 10 files changed, 701 insertions(+) create mode 100644 god/streams/Makefile create mode 100644 god/streams/README create mode 100755 god/streams/mpdstreams create mode 100755 god/streams/relaxxapi.py create mode 100644 god/streams/relaxxplayer.api create mode 100755 god/streams/relaxxstreams create mode 100755 god/streams/stream-starter create mode 100644 god/streams/stream.db create mode 100755 god/streams/streams create mode 100644 god/streams/streams.py (limited to 'god') diff --git a/god/streams/Makefile b/god/streams/Makefile new file mode 100644 index 00000000..ab5d1429 --- /dev/null +++ b/god/streams/Makefile @@ -0,0 +1,16 @@ +INITD = $(shell test -e /etc/rc.d/ && echo /etc/rc.d/ || echo /etc/init.d/) +streams = $(shell cut -d\ -f2 stream.db) + + +CURRDIR = ${PWD} +.PHONY: all $(streams) +local: ../bin/streams + +../bin/streams: + ln -sf $$PWD/streams ../bin/streams +all: $(streams) + @update-rc.d groove defaults 2>/dev/null || echo "** put groove daemon in DAEMONS in /etc/rc.conf" + +$(streams): local + @test -L $(INITD)$@ || test ! -e $(INITD)$@ && \ + ln -n -s -f $$PWD/stream-starter $(INITD)$@ && echo "writing $@ to $(INITD)" diff --git a/god/streams/README b/god/streams/README new file mode 100644 index 00000000..82300c24 --- /dev/null +++ b/god/streams/README @@ -0,0 +1,10 @@ +streams done right + +deepmix,groovesalad and radiotux are now init.d scrips which can be +started and stopped. + +scripts are dumped into /etc/init.d and groovesalad will be set as +default via update-rc.d + +mplayer will be started in a tmux session either by creating a new +session or starting a new window inside the first existing one diff --git a/god/streams/mpdstreams b/god/streams/mpdstreams new file mode 100755 index 00000000..d9132937 --- /dev/null +++ b/god/streams/mpdstreams @@ -0,0 +1,114 @@ +#!/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", "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","--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 """ 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) diff --git a/god/streams/relaxxapi.py b/god/streams/relaxxapi.py new file mode 100755 index 00000000..f367caef --- /dev/null +++ b/god/streams/relaxxapi.py @@ -0,0 +1,140 @@ +#!/usr/bin/python2 +import json +from urllib import quote +class relaxx: + + def __init__(self,relaxxurl="http://lounge.mpd.shack/"): + self.baseurl=relaxxurl + import requests + ret = requests.get(relaxxurl) # grab cookie + try: + self.r = requests.session(cookies=ret.cookies,headers={"Referer":relaxxurl}) + except: + print ("you are missing the `requests` dependency, please do a `pip install requests`") + def _status(self,value=0,data="json=null"): + """ + value is some weird current playlist value, 0 seems to work + data is url encoded kv-store + """ + # TODO get the current playlist value + url=self.baseurl+"include/controller-ping.php?value=%s"%value + return self.r.post(url,data="json=null").text + + def _playlist(self,action,value="",json="null",method="get"): + """ + This function is the interface to the controller-playlist api + use it if you dare + Possible actions: + clear + addSong url_encoded_path + moveSong 1:2 + getPlaylists + getPlaylistInfo 1 + listPlaylistInfo + as everything seems to be a get request, the method is set to GET as + default + """ + url=self.baseurl+"include/controller-playlist.php?action=%s&value=%s&json=%s"%(action,value,json) + if method== "get": + return self.r.get(url).text + elif method == "post": + return r.post(url).text + else: + raise Exception("unknown method %s") + + def _playback(self,action,value="",json="null",method="get"): + """ + play + continue + stop + setCrossfade + """ + url=self.baseurl+"include/controller-playback.php?action=%s&value=%s&json=%s"%(action,value,json) + # probably obsolete because everything is "get" + if method== "get": + return self.r.get(url).text + elif method == "post": + return r.post(url).text + else: + raise Exception("unknown method %s") + + def _radio(self,playlist=""): + """ + both, post and get the url seem to work here... + """ + url=self.baseurl+"include/controller-netradio.php?playlist=%s"%quote(playlist) + return self.r.get(url).text + + def add_radio(self,playlist=""): + print playlist + print self._radio(playlist) + print json.loads(self._radio(playlist)) #[1:-1])["url"] + resolved_url= json.loads(self._radio(playlist)[1:-1])["url"] + self.add_song(resolved_url) + + def add_song(self,path): + return self._playlist("addSong",path) + + def get_first(self): + return json.loads(self._playlist("getPlaylistInfo","0",""))[0] + + def get_first(self): + return json.loads(self._playlist("getPlaylistInfo","0",""))[-1] + + def clear(self): + return self._playlist("clear") + + def crossfade(self,ident="0"): + """ + default: no crossfade + """ + return self._playback("setCrossfade",ident) + + def repeat(self,ident="1"): + """ + default: do repeat + """ + return self._playback("repeat",ident) + + def play(self,ident): + return self._playback("play",ident) + + def stop(self): + return self._playback("stop") + + def cont(self,ident): + return self._playback("continue",ident) + + def play_first(self): + return self.play(self.get_first()["Id"]) + + def play_last(self): + return self.play(self.get_last()["Id"]) + + def state(self): + return json.loads(self._status()) + + def is_running(self): + return self.state()["status"]["state"] == "play" + + def playing(self): + """ returns "" if not running + """ + state = self.state() + if state["status"]["state"] == "play" : + ident = state["status"]["song"] + current = state["playlist"]["file"][int(ident)] + return current.get("Name",current.get("Artist")) + " - " + current["Title"] + else: + return "" + +if __name__ == "__main__": + r = relaxx() + print r.state() + print r.playing() + print r.add_radio("http://deluxetelevision.com/livestreams/radio/DELUXE_RADIO.pls") + #print r.clear() + #print r.add_radio("http://somafm.com/lush.pls") + #print r.get_first()["Id"] + #print r.play_first() + #print r.add_radio("http://somafm.com/lush.pls") diff --git a/god/streams/relaxxplayer.api b/god/streams/relaxxplayer.api new file mode 100644 index 00000000..6e8af577 --- /dev/null +++ b/god/streams/relaxxplayer.api @@ -0,0 +1,13 @@ +http://lounge.mpd.shack/include/controller-playback.php?action=setCrossfade&value=5&json=null +http://lounge.mpd.shack/include/controller-playback.php?action=repeat&value=1&json=null +http://lounge.mpd.shack/include/controller-playlist.php?action=clear&value=&json=null +http://lounge.mpd.shack/include/controller-playlist.php?action=addSong&value=http%3A%2F%2F212.7.194.133%3A8128&json=null +http://lounge.mpd.shack/include/controller- playlist.php?action=getPlaylistInfo&value=0&json= +[{ + "file": "http:\/\/212.7.194.133:8128", + "Title": "Max & Dima - Sapovnela Studio: Batut", + "Name": "Deep Mix Moscow Radio: deepmix.ru", + "Pos": "0", + "Id": "606" +}] +http://lounge.mpd.shack/include/controller-playlist.php?action=continue&value={Id}&json=null diff --git a/god/streams/relaxxstreams b/god/streams/relaxxstreams new file mode 100755 index 00000000..57cfb05b --- /dev/null +++ b/god/streams/relaxxstreams @@ -0,0 +1,107 @@ +#!/usr/bin/python2 + +# this version cannot tell if a stream is running or just ordinary music +import os +import sys +import json +from urllib import quote +from relaxxapi import relaxx + +try: + import requests +except: + print ("you are missing the `requests` dependency, please do a `pip install requests`") +from subprocess import Popen, PIPE + +os.chdir(os.path.dirname(os.path.realpath(sys.argv[0]))) +pidfile = "/tmp/krebs.stream.pid" +baseurl="http://elab.mpd.shack/" +url=baseurl+"include/controller-playlist.php?action=%s&value=%s&json=%s" +url_file = os.environ.get("STREAM_DB", "direct.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") +api = relaxx(baseurl) + +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): + print api.crossfade("5") + print api.repeat("1") + print api.clear() + print api.add_song(stream_url) + print api.play_first() + +def start(stream): + ret = api.playing() + if ret: + print "!! Stream `%s` already running !" % \ + (ret) + else: + startStream(urlForStream(stream)) + print "** Starting `%s`."% stream + + +def stop(): + ret = api.playing() + if not ret: + print "!! No Stream running!" + else: + print "** Stopping `%s`" % ret + api.stop() + +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 = api.playing() + 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) diff --git a/god/streams/stream-starter b/god/streams/stream-starter new file mode 100755 index 00000000..4d3a406f --- /dev/null +++ b/god/streams/stream-starter @@ -0,0 +1,19 @@ +#! /bin/bash +set -euf +HERE=$(dirname $(readlink -f $0)) +STR=$HERE/streams +NAME=`basename $0` +case "$1" in + start) + $STR start $NAME + ;; + stop) + $STR stop + ;; + restart) + $STR restart $NAME + ;; + *) + echo "aidsballs" + ;; +esac diff --git a/god/streams/stream.db b/god/streams/stream.db new file mode 100644 index 00000000..2e873843 --- /dev/null +++ b/god/streams/stream.db @@ -0,0 +1,35 @@ +http://deepmix.ru/deepmix128.pls deepmix +http://streams.xenim.de/radiotux.ogg radiotux +http://bassdrive.com/v2/streams/BassDrive.pls bassdrive +http://localhost:8000/stream.ogg icecast +http://localhost:8000/shice.ogg shice +http://stream2.jungletrain.net:8000 jungletrain +http://playlist.tormentedradio.com/tormentedradio.pls tormented +http://filebitch.shack:8000 mpd +http://radio.krautchan.net:8000/radio.mp3 radiofreieskrautchan +http://nl1.streamhosting.ch/listen.pls lounge +http://deluxetelevision.com/livestreams/radio/DELUXE_RADIO.pls deluxe +http://livestream.radiodarmstadt.de:8000 darmstadt +http://somafm.com/sxfm.pls southbysoma +http://somafm.com/indiepop.pls indypop +http://somafm.com/poptron.pls poptron +http://somafm.com/480min.pls 480min +http://somafm.com/u80s.pls underground80s +http://somafm.com/secretagent.pls secretagent +http://somafm.com/suburbsofgoa.pls suburbsofgoa +http://somafm.com/beatblender.pls beatblender +http://somafm.com/missioncontrol.pls missioncontrol +http://somafm.com/dronezone.pls dronezone +http://somafm.com/cliqhop.pls cliqhop +http://somafm.com/spacestation.pls spacestationsoma +http://somafm.com/bootliquor.pls bootliquor +http://somafm.com/covers.pls covers +http://somafm.com/illstreet.pls illstreet +http://somafm.com/tags.pls tagstrip +http://somafm.com/groovesalad.pls groove +http://somafm.com/lush.pls lush +http://somafm.com/digitalis.pls digitalis +http://somafm.com/sonicuniverse.pls sonicuniverse +http://somafm.com/doomed.pls doomed +http://somafm.com/brfm.pls blackrockfm +http://de.scenemusic.net/necta192.mp3 demoscene diff --git a/god/streams/streams b/god/streams/streams new file mode 100755 index 00000000..80c654ad --- /dev/null +++ b/god/streams/streams @@ -0,0 +1,131 @@ +#! /bin/bash +set -euf + +stream_file=/tmp/krebs.stream.current + +HERE=$(dirname $(readlink -f $0)) +URLS=`cat $HERE/stream.db` +CURRENT_STREAM="no stream" #will be set when calling `status` +if [ ! `id -u` -eq "0" ]; then + exec sudo "$0" "$@" +fi + +#if [ ! `id -u` -eq "0" ]; then +# echo "we are going sudo..." +# exec sudo "$0" "$@" +#fi + +function start() { + # start the given stream von $1 + REQ=$1 + tmux start-server + if status &>/dev/null ; then + echo "!! Stream already running!" + exit 1 + fi + + if echo "$URLS" | while read URL NAME; do + if [ "$NAME" = "$REQ" ];then + tmux new-session -s streams -n streams -d " + while sleep 1; do + echo $NAME > $stream_file + mplayer $URL + done + " + echo "** $REQ started" + exit 1 + fi + done; then + echo "!! Stream '$REQ' not found!" + exit 1 + fi +} +function stop() +{ + #stops every stream + if status &>/dev/null; then + status | cut -d\ -f2 | xargs printf "** killing %s\n" + tmux kill-session -t streams 2>/dev/null || echo "!! killing session failed" + else + echo "** no stream running"; + return 1 + fi +} + +function status() +{ + #tmux has-session -t streams 2>/dev/null + #RET=$? + + #tmux list-sessions 2>/dev/null + #return $RET + EV="`ps -ef | grep mplayer`" + if echo "$URLS" | while read URL NAME; do + if [ "`echo "$EV" | grep \"$URL\"`" ] ;then + echo "** $NAME running ($URL)" + exit 1 + fi + done; then + echo "** no stream running" + return 1 + else + return 0 + fi +} +function current() +{ + + return 1 +} +function list() +{ + echo "$URLS" | while read URL NAME ; do + echo "$NAME : $URL" + done + +} + +function shorthelp() +{ + echo "start|stop|restart|status|list [audio stream]" +} +function longhelp() +{ + B=`basename $0` + echo -n "Usage: $B " + shorthelp + echo " get all available streams with '/$B list' +Examples: + $B list + $B start groove + $B restart deepmix + $B status + $B stop" +} + + +case "$1" in + start) + start ${2-"`test -f $stream_file && cat $stream_file`"} + ;; + stop) + stop + ;; + (switch|restart) + stop + start $2 + ;; + status) + status + exit $? + ;; + list) + list + ;; + (--help) + shorthelp + ;; + *) + longhelp + ;; +esac diff --git a/god/streams/streams.py b/god/streams/streams.py new file mode 100644 index 00000000..65669b2d --- /dev/null +++ b/god/streams/streams.py @@ -0,0 +1,116 @@ +#!/usr/bin/python +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" + + +def check_pid(pid): + """ Check For the existence of a unix pid. """ + try: + os.kill(pid, 0) + except OSError: + return False + else: + return True + + +def urlForStream(stream): + for url, s in urls: + if s == stream: + return url + + +def start(stream): + ret = running() + if ret: + print "!! Stream `%s` already running with pid `%s` !" % \ + (ret[1], ret[0]) + else: + pipe_silent = open('/dev/null', 'w') + url = urlForStream(stream) + mpl = Popen(["mplayer", url], + stdout=pipe_silent, stderr=pipe_silent).pid + print >> open(pidfile, "w+"), "%d %s" % (mpl, stream) + + +def stop(): + ret = running() + if not ret: + print "!! No Stream running!" + else: + pid, name = ret + print "** Killing `%s` with pid %s" % (name, pid) + os.kill(int(pid), 15) + #if check_pid(int(pid)): + # print "!! trying harder to kill process" + # os.kill(int(pid), 9) + os.remove(pidfile) + + +def running(): + try: + pid, currstream = open(pidfile).read().split() + if check_pid(int(pid)): + return (pid, currstream) + else: + print "!! removing stale pidfile" + os.remove(pidfile) + raise Exception("Pidfile stale") + 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 "** no stream running" # , e + else: + print "%s is running(%s)" % (ret[1], urlForStream(ret[1])) +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) -- cgit v1.2.3 From 5a3126a9036b898de095776ebe18fe55f6312342 Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 20 Dec 2012 10:57:19 +0700 Subject: streams.py is the new streams the old source code was just too ugly --- god/streams/streams | 245 +++++++++++++++++++++++-------------------------- god/streams/streams.py | 116 ----------------------- 2 files changed, 115 insertions(+), 246 deletions(-) mode change 100755 => 100644 god/streams/streams delete mode 100644 god/streams/streams.py (limited to 'god') diff --git a/god/streams/streams b/god/streams/streams old mode 100755 new mode 100644 index 80c654ad..65669b2d --- a/god/streams/streams +++ b/god/streams/streams @@ -1,131 +1,116 @@ -#! /bin/bash -set -euf - -stream_file=/tmp/krebs.stream.current - -HERE=$(dirname $(readlink -f $0)) -URLS=`cat $HERE/stream.db` -CURRENT_STREAM="no stream" #will be set when calling `status` -if [ ! `id -u` -eq "0" ]; then - exec sudo "$0" "$@" -fi - -#if [ ! `id -u` -eq "0" ]; then -# echo "we are going sudo..." -# exec sudo "$0" "$@" -#fi - -function start() { - # start the given stream von $1 - REQ=$1 - tmux start-server - if status &>/dev/null ; then - echo "!! Stream already running!" - exit 1 - fi - - if echo "$URLS" | while read URL NAME; do - if [ "$NAME" = "$REQ" ];then - tmux new-session -s streams -n streams -d " - while sleep 1; do - echo $NAME > $stream_file - mplayer $URL - done - " - echo "** $REQ started" - exit 1 - fi - done; then - echo "!! Stream '$REQ' not found!" - exit 1 - fi -} -function stop() -{ - #stops every stream - if status &>/dev/null; then - status | cut -d\ -f2 | xargs printf "** killing %s\n" - tmux kill-session -t streams 2>/dev/null || echo "!! killing session failed" - else - echo "** no stream running"; - return 1 - fi -} - -function status() -{ - #tmux has-session -t streams 2>/dev/null - #RET=$? - - #tmux list-sessions 2>/dev/null - #return $RET - EV="`ps -ef | grep mplayer`" - if echo "$URLS" | while read URL NAME; do - if [ "`echo "$EV" | grep \"$URL\"`" ] ;then - echo "** $NAME running ($URL)" - exit 1 - fi - done; then - echo "** no stream running" - return 1 - else - return 0 - fi -} -function current() -{ - - return 1 -} -function list() -{ - echo "$URLS" | while read URL NAME ; do - echo "$NAME : $URL" - done - -} - -function shorthelp() -{ - echo "start|stop|restart|status|list [audio stream]" -} -function longhelp() -{ - B=`basename $0` - echo -n "Usage: $B " - shorthelp - echo " get all available streams with '/$B list' -Examples: - $B list - $B start groove - $B restart deepmix - $B status - $B stop" -} - - -case "$1" in - start) - start ${2-"`test -f $stream_file && cat $stream_file`"} - ;; - stop) - stop - ;; - (switch|restart) - stop - start $2 - ;; - status) - status - exit $? - ;; - list) - list - ;; - (--help) +#!/usr/bin/python +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" + + +def check_pid(pid): + """ Check For the existence of a unix pid. """ + try: + os.kill(pid, 0) + except OSError: + return False + else: + return True + + +def urlForStream(stream): + for url, s in urls: + if s == stream: + return url + + +def start(stream): + ret = running() + if ret: + print "!! Stream `%s` already running with pid `%s` !" % \ + (ret[1], ret[0]) + else: + pipe_silent = open('/dev/null', 'w') + url = urlForStream(stream) + mpl = Popen(["mplayer", url], + stdout=pipe_silent, stderr=pipe_silent).pid + print >> open(pidfile, "w+"), "%d %s" % (mpl, stream) + + +def stop(): + ret = running() + if not ret: + print "!! No Stream running!" + else: + pid, name = ret + print "** Killing `%s` with pid %s" % (name, pid) + os.kill(int(pid), 15) + #if check_pid(int(pid)): + # print "!! trying harder to kill process" + # os.kill(int(pid), 9) + os.remove(pidfile) + + +def running(): + try: + pid, currstream = open(pidfile).read().split() + if check_pid(int(pid)): + return (pid, currstream) + else: + print "!! removing stale pidfile" + os.remove(pidfile) + raise Exception("Pidfile stale") + 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 - ;; - *) - longhelp - ;; -esac + 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 "** no stream running" # , e + else: + print "%s is running(%s)" % (ret[1], urlForStream(ret[1])) +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) diff --git a/god/streams/streams.py b/god/streams/streams.py deleted file mode 100644 index 65669b2d..00000000 --- a/god/streams/streams.py +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/python -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" - - -def check_pid(pid): - """ Check For the existence of a unix pid. """ - try: - os.kill(pid, 0) - except OSError: - return False - else: - return True - - -def urlForStream(stream): - for url, s in urls: - if s == stream: - return url - - -def start(stream): - ret = running() - if ret: - print "!! Stream `%s` already running with pid `%s` !" % \ - (ret[1], ret[0]) - else: - pipe_silent = open('/dev/null', 'w') - url = urlForStream(stream) - mpl = Popen(["mplayer", url], - stdout=pipe_silent, stderr=pipe_silent).pid - print >> open(pidfile, "w+"), "%d %s" % (mpl, stream) - - -def stop(): - ret = running() - if not ret: - print "!! No Stream running!" - else: - pid, name = ret - print "** Killing `%s` with pid %s" % (name, pid) - os.kill(int(pid), 15) - #if check_pid(int(pid)): - # print "!! trying harder to kill process" - # os.kill(int(pid), 9) - os.remove(pidfile) - - -def running(): - try: - pid, currstream = open(pidfile).read().split() - if check_pid(int(pid)): - return (pid, currstream) - else: - print "!! removing stale pidfile" - os.remove(pidfile) - raise Exception("Pidfile stale") - 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 "** no stream running" # , e - else: - print "%s is running(%s)" % (ret[1], urlForStream(ret[1])) -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) -- cgit v1.2.3 From e2b9d1434ac87bf2fa160b09924eee84b9b123cb Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 20 Dec 2012 11:15:48 +0700 Subject: //god/streams/ - cleanup --- god/streams/.gitignore | 1 + god/streams/Makefile | 6 +- god/streams/README | 10 --- god/streams/README.md | 38 ++++++++++ god/streams/bin/helper/streamfind | 10 +++ god/streams/bin/helper/streamwrite | 6 ++ god/streams/bin/mpdstreams | 113 ++++++++++++++++++++++++++++++ god/streams/bin/relaxxapi.py | 140 +++++++++++++++++++++++++++++++++++++ god/streams/bin/relaxxstreams | 107 ++++++++++++++++++++++++++++ god/streams/bin/stream-starter | 19 +++++ god/streams/bin/streams | 116 ++++++++++++++++++++++++++++++ god/streams/db/direct.db | 78 +++++++++++++++++++++ god/streams/db/stream.db | 35 ++++++++++ god/streams/doc/relaxxplayer.api | 13 ++++ god/streams/mpdstreams | 114 ------------------------------ god/streams/relaxxapi.py | 140 ------------------------------------- god/streams/relaxxplayer.api | 13 ---- god/streams/relaxxstreams | 107 ---------------------------- god/streams/stream-starter | 19 ----- god/streams/stream.db | 35 ---------- god/streams/streams | 116 ------------------------------ 21 files changed, 679 insertions(+), 557 deletions(-) create mode 100644 god/streams/.gitignore delete mode 100644 god/streams/README create mode 100644 god/streams/README.md create mode 100644 god/streams/bin/helper/streamfind create mode 100644 god/streams/bin/helper/streamwrite create mode 100755 god/streams/bin/mpdstreams create mode 100755 god/streams/bin/relaxxapi.py create mode 100755 god/streams/bin/relaxxstreams create mode 100755 god/streams/bin/stream-starter create mode 100755 god/streams/bin/streams create mode 100644 god/streams/db/direct.db create mode 100644 god/streams/db/stream.db create mode 100644 god/streams/doc/relaxxplayer.api delete mode 100755 god/streams/mpdstreams delete mode 100755 god/streams/relaxxapi.py delete mode 100644 god/streams/relaxxplayer.api delete mode 100755 god/streams/relaxxstreams delete mode 100755 god/streams/stream-starter delete mode 100644 god/streams/stream.db delete mode 100644 god/streams/streams (limited to 'god') diff --git a/god/streams/.gitignore b/god/streams/.gitignore new file mode 100644 index 00000000..0d20b648 --- /dev/null +++ b/god/streams/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/god/streams/Makefile b/god/streams/Makefile index ab5d1429..2d0056f4 100644 --- a/god/streams/Makefile +++ b/god/streams/Makefile @@ -4,10 +4,10 @@ streams = $(shell cut -d\ -f2 stream.db) CURRDIR = ${PWD} .PHONY: all $(streams) -local: ../bin/streams +local: ../../bin/streams -../bin/streams: - ln -sf $$PWD/streams ../bin/streams +../../bin/streams: + ln -sf $$PWD/bin/streams ../../bin/streams all: $(streams) @update-rc.d groove defaults 2>/dev/null || echo "** put groove daemon in DAEMONS in /etc/rc.conf" diff --git a/god/streams/README b/god/streams/README deleted file mode 100644 index 82300c24..00000000 --- a/god/streams/README +++ /dev/null @@ -1,10 +0,0 @@ -streams done right - -deepmix,groovesalad and radiotux are now init.d scrips which can be -started and stopped. - -scripts are dumped into /etc/init.d and groovesalad will be set as -default via update-rc.d - -mplayer will be started in a tmux session either by creating a new -session or starting a new window inside the first existing one diff --git a/god/streams/README.md b/god/streams/README.md new file mode 100644 index 00000000..cc763ede --- /dev/null +++ b/god/streams/README.md @@ -0,0 +1,38 @@ +# streams done right(tm) + +There are numerous ways to start streams ( make your computer or some server +output streamed audio). Currently implemented are: + +# Streams Modules +1. pure streams - will start mpd on your local machine +2. mpdstreams - will use a given mpd server to start a stream +3. relaxxstreams - will contact the relaxxplayer (mpd front-end) if the direct + connection to the mpd is prohibited by firewall rules +# Database +Currently there are a number of possible streams saved in the database files +which contain of a link, a space, and the name of the stream. the database +can be found in db/ . + +Currently there are two kinds of databases: +1. streams.db - contains links to playlists of streams +2. direct.db - contains links directly to the stream, not the playlist + +to generate direct.db from a list of playlists use the helper/* scripts + +# initscripts + +the most convenient way to start streams is to use stream-starter which is +a script which, when symlinked with a name of a stream, invokes the streams +tool with its own name as parameter. + +An example: + + ln -s /krebs/god/streams/bin/stream-starter /etc/init.d/groove + /etc/init.d/groove start + +# Remarks +deepmix,groovesalad and radiotux are now init.d scrips which can be +started and stopped. + +scripts are dumped into /etc/init.d and groovesalad will be set as +default via update-rc.d diff --git a/god/streams/bin/helper/streamfind b/god/streams/bin/helper/streamfind new file mode 100644 index 00000000..32b84a4e --- /dev/null +++ b/god/streams/bin/helper/streamfind @@ -0,0 +1,10 @@ +#!/bin/sh +cat stream.db | while read url name;do + curl $url --max-time 1 2>/dev/null| sed -n 's/[fF]ile[0-9]=\(.*\)/\1/p' > "streamfinder/$name" + if [ "x`cat \"streamfinder/$name\"`" == "x" ];then + rm "streamfinder/$name" + echo "$name empty or not a stream" + else + echo "wrote $name" + fi +done diff --git a/god/streams/bin/helper/streamwrite b/god/streams/bin/helper/streamwrite new file mode 100644 index 00000000..a10fb155 --- /dev/null +++ b/god/streams/bin/helper/streamwrite @@ -0,0 +1,6 @@ +for i in `ls -1 streamfinder/`; +do + for j in `cat "$i"`;do + echo "$j $i" >> direct.db + done +done 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 """ 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) diff --git a/god/streams/bin/relaxxapi.py b/god/streams/bin/relaxxapi.py new file mode 100755 index 00000000..f367caef --- /dev/null +++ b/god/streams/bin/relaxxapi.py @@ -0,0 +1,140 @@ +#!/usr/bin/python2 +import json +from urllib import quote +class relaxx: + + def __init__(self,relaxxurl="http://lounge.mpd.shack/"): + self.baseurl=relaxxurl + import requests + ret = requests.get(relaxxurl) # grab cookie + try: + self.r = requests.session(cookies=ret.cookies,headers={"Referer":relaxxurl}) + except: + print ("you are missing the `requests` dependency, please do a `pip install requests`") + def _status(self,value=0,data="json=null"): + """ + value is some weird current playlist value, 0 seems to work + data is url encoded kv-store + """ + # TODO get the current playlist value + url=self.baseurl+"include/controller-ping.php?value=%s"%value + return self.r.post(url,data="json=null").text + + def _playlist(self,action,value="",json="null",method="get"): + """ + This function is the interface to the controller-playlist api + use it if you dare + Possible actions: + clear + addSong url_encoded_path + moveSong 1:2 + getPlaylists + getPlaylistInfo 1 + listPlaylistInfo + as everything seems to be a get request, the method is set to GET as + default + """ + url=self.baseurl+"include/controller-playlist.php?action=%s&value=%s&json=%s"%(action,value,json) + if method== "get": + return self.r.get(url).text + elif method == "post": + return r.post(url).text + else: + raise Exception("unknown method %s") + + def _playback(self,action,value="",json="null",method="get"): + """ + play + continue + stop + setCrossfade + """ + url=self.baseurl+"include/controller-playback.php?action=%s&value=%s&json=%s"%(action,value,json) + # probably obsolete because everything is "get" + if method== "get": + return self.r.get(url).text + elif method == "post": + return r.post(url).text + else: + raise Exception("unknown method %s") + + def _radio(self,playlist=""): + """ + both, post and get the url seem to work here... + """ + url=self.baseurl+"include/controller-netradio.php?playlist=%s"%quote(playlist) + return self.r.get(url).text + + def add_radio(self,playlist=""): + print playlist + print self._radio(playlist) + print json.loads(self._radio(playlist)) #[1:-1])["url"] + resolved_url= json.loads(self._radio(playlist)[1:-1])["url"] + self.add_song(resolved_url) + + def add_song(self,path): + return self._playlist("addSong",path) + + def get_first(self): + return json.loads(self._playlist("getPlaylistInfo","0",""))[0] + + def get_first(self): + return json.loads(self._playlist("getPlaylistInfo","0",""))[-1] + + def clear(self): + return self._playlist("clear") + + def crossfade(self,ident="0"): + """ + default: no crossfade + """ + return self._playback("setCrossfade",ident) + + def repeat(self,ident="1"): + """ + default: do repeat + """ + return self._playback("repeat",ident) + + def play(self,ident): + return self._playback("play",ident) + + def stop(self): + return self._playback("stop") + + def cont(self,ident): + return self._playback("continue",ident) + + def play_first(self): + return self.play(self.get_first()["Id"]) + + def play_last(self): + return self.play(self.get_last()["Id"]) + + def state(self): + return json.loads(self._status()) + + def is_running(self): + return self.state()["status"]["state"] == "play" + + def playing(self): + """ returns "" if not running + """ + state = self.state() + if state["status"]["state"] == "play" : + ident = state["status"]["song"] + current = state["playlist"]["file"][int(ident)] + return current.get("Name",current.get("Artist")) + " - " + current["Title"] + else: + return "" + +if __name__ == "__main__": + r = relaxx() + print r.state() + print r.playing() + print r.add_radio("http://deluxetelevision.com/livestreams/radio/DELUXE_RADIO.pls") + #print r.clear() + #print r.add_radio("http://somafm.com/lush.pls") + #print r.get_first()["Id"] + #print r.play_first() + #print r.add_radio("http://somafm.com/lush.pls") diff --git a/god/streams/bin/relaxxstreams b/god/streams/bin/relaxxstreams new file mode 100755 index 00000000..0d3813a7 --- /dev/null +++ b/god/streams/bin/relaxxstreams @@ -0,0 +1,107 @@ +#!/usr/bin/python2 + +# this version cannot tell if a stream is running or just ordinary music +import os +import sys +import json +from urllib import quote +from relaxxapi import relaxx + +try: + import requests +except: + print ("you are missing the `requests` dependency, please do a `pip install requests`") +from subprocess import Popen, PIPE + +os.chdir(os.path.dirname(os.path.realpath(sys.argv[0]))) +pidfile = "/tmp/krebs.stream.pid" +baseurl="http://lounge.mpd.shack/" +url=baseurl+"include/controller-playlist.php?action=%s&value=%s&json=%s" +url_file = os.environ.get("STREAM_DB", "../db/direct.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") +api = relaxx(baseurl) + +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): + print api.crossfade("5") + print api.repeat("1") + print api.clear() + print api.add_song(stream_url) + print api.play_first() + +def start(stream): + ret = api.playing() + if ret: + print "!! Stream `%s` already running !" % \ + (ret) + else: + startStream(urlForStream(stream)) + print "** Starting `%s`."% stream + + +def stop(): + ret = api.playing() + if not ret: + print "!! No Stream running!" + else: + print "** Stopping `%s`" % ret + api.stop() + +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(s