diff --git a/ice_relay.py b/ice_relay.py index 8a5bb65..9299724 100644 --- a/ice_relay.py +++ b/ice_relay.py @@ -24,26 +24,35 @@ class IceRelay(Thread): self.channelIsOpen = False self.options = options - - self.channel = shout.Shout() - self.channel.mount = '/' + mountpoint + self.mountpoint = mountpoint + self.title = title self.api_host = options['api_host'] - self.channel.url = 'http://deepsouthsounds.com/' - self.channel.name = title - self.channel.genre = 'Deep House Music' - self.channel.description = 'Deep sounds from the deep south' - self.channel.format = options['ice_format'] - self.channel.host = options['ice_host'] - self.channel.port = int(options['ice_port']) - self.channel.user = options['ice_user'] - self.channel.password = options['ice_password'] + self.api_callback_url = options['api_callback_url'] self.twitter_consumer_key = options['twitter_consumer_key'] self.twitter_consumer_secret = options['twitter_consumer_secret'] self.twitter_access_token = options['twitter_access_token'] self.twitter_access_token_secret = options['twitter_access_token_secret'] + self._channel_mutex = False + + self._open_channel() + self.server_url = 'http://' + self.channel.host + ':' + str(self.channel.port) + self.channel.mount + print(self.server_url) + + def _open_channel(self): + self.channel = shout.Shout() + self.channel.mount = '/' + self.mountpoint + self.channel.url = 'http://deepsouthsounds.com/' + self.channel.name = self.title + self.channel.genre = 'Deep House Music' + self.channel.description = 'Deep sounds from the deep south' + self.channel.format = self.options['ice_format'] + self.channel.host = self.options['ice_host'] + self.channel.port = int(self.options['ice_port']) + self.channel.user = self.options['ice_user'] + self.channel.password = self.options['ice_password'] self.channel.public = 1 if self.channel.format == 'mp3': self.channel.audio_info = { @@ -52,9 +61,6 @@ class IceRelay(Thread): 'channels': str(2), } - self.server_url = 'http://' + self.channel.host + ':' + str(self.channel.port) + self.channel.mount - print(self.server_url) - def channel_open(self): if self.channelIsOpen: return True @@ -98,26 +104,19 @@ class IceRelay(Thread): while not found: r = requests.get('http://{}/_radio?rmix=z'.format(self.api_host)) v = r.json() - audio = v['url'] - title = v['title'] - slug = v['slug'] import urllib2 try: - ret = urllib2.urlopen(audio) + ret = urllib2.urlopen(v['url']) if ret.code == 200: found = True - ret = [{ - 'url': audio, - 'description': title, - 'slug': slug - }] + ret = [v] except urllib2.HTTPError as ex: pass except Exception as ex: logging.error(ex) ret = [{ 'url': 'https://dsscdn.blob.core.windows.net/mixes/52df41af-5f81-4f00-a9a8-9ffb5dc3185f.mp3', - 'description': 'Default song', + 'title': 'Default song', 'slug': '/' }] @@ -145,18 +144,21 @@ class IceRelay(Thread): else: item = self.default_queue()[0] - self.channel.set_metadata({'song': str(item['description']), 'charset': 'utf-8'}) - logging.debug("Playing: {}".format(item['description'])) + self.channel.set_metadata({'song': str(item['title']), 'charset': 'utf-8'}) + logging.debug("Playing: {}".format(item['title'])) self.stream = self.file_read_remote(item['url']) + + self.post_callback(item['title'], item['slug']) if self.twitter_access_token and self.twitter_access_token_secret and \ self.twitter_consumer_secret and self.twitter_consumer_key: try: tw = Twitter(key=self.twitter_consumer_key, secret=self.twitter_consumer_secret, access_key=self.twitter_access_token, access_secret=self.twitter_access_token_secret) - tw.post("Now playing on DSS Radio - {}\nhttp://deepsouthsounds.com/".format(item['description'])) + tw.post("Now playing on DSS Radio - {}\nhttp://deepsouthsounds.com/".format( + item['title'][0:90])) except Exception as ex: - logging.error("Unable to post to twitter: {}".format(ex)) + logging.debug("Unable to post to twitter: {}".format(ex)) self._ended = False return True @@ -175,6 +177,13 @@ class IceRelay(Thread): try: self.channel.send(self.chunk) self.channel.sync() + except shout.ShoutException as sex: #(snigger) + if not self._channel_mutex: + self.channelIsOpen = False + self._channel_mutex = True + self.channel_open() + else: + self._channel_mutex = False except Exception as ex: logging.error("Error sending chunk: {0}".format(ex)) self.channel_close() @@ -196,3 +205,9 @@ class IceRelay(Thread): break yield __main_chunk m.close() + + def post_callback(self, title, slug): + if self.api_callback_url: + url = "http://{}{}".format(self.api_host, self.api_callback_url.format(title, slug)) + r = requests.post(url) + print (r) diff --git a/server.py b/server.py index 19ae517..3406a0b 100755 --- a/server.py +++ b/server.py @@ -25,13 +25,20 @@ class ShuffleAudioHandler(tornado.web.RequestHandler): except Exception, ex: raise tornado.web.HTTPError(500, ex.message) + class PlayAudioHandler(tornado.web.RequestHandler): def post(self, *args, **kwargs): try: - data = tornado.escape.json_decode(self.request.body) - in_file = data.get('audio_file') - if in_file is not None: - relay.set_audio_queue([in_file]) + """ check the arguments we've got until we're satisfied they are correct """ + args = self.request.body_arguments + if 'slug' in args and 'title' in args and 'url' in args: + relay.set_audio_queue([{ + 'slug': self.get_body_argument('slug'), + 'title': self.get_body_argument('title'), + 'url': self.get_body_argument('url') + }]) + else: + raise tornado.web.HTTPError(401, "Invalid audio item") except Exception, ex: raise tornado.web.HTTPError(500, ex.message) @@ -59,6 +66,7 @@ def try_exit(): tornado.ioloop.IOLoop.instance().stop() logging.info('exit success') + define("port", default=8888, help="run on the given port", type=int) define("debug", default=True, help="run in debug mode") @@ -70,13 +78,14 @@ define("ice_mount", default='/mp3', help="Default icecast mount point") define("ice_format", default='mp3', help="Format of the icecast server (mp3, vorbis, flac)") define("ice_protocol", default='http', help="Protocol (currently only http)") define("api_host", default='api.deepsouthsounds.com', help="API Host for serving audio") +define("api_callback_url", default='/_radio', help="Callback url for notifying host of songs and ting!") define("twitter_consumer_key", default='', help="Key for posting to twitter") define("twitter_consumer_secret", default='', help="Secret for posting to twitter") define("twitter_access_token", default='', help="Key for posting to twitter") define("twitter_access_token_secret", default='', help="Secret for posting to twitter") -#tornado.options.parse_command_line() +# tornado.options.parse_command_line() tornado.options.parse_config_file("dss.radio.conf") relay = IceRelay(options=options)