mirror of
https://github.com/fergalmoran/dss.radio.git
synced 2025-12-22 09:48:21 +00:00
Allow post track
This commit is contained in:
73
ice_relay.py
73
ice_relay.py
@@ -24,26 +24,35 @@ class IceRelay(Thread):
|
|||||||
self.channelIsOpen = False
|
self.channelIsOpen = False
|
||||||
|
|
||||||
self.options = options
|
self.options = options
|
||||||
|
self.mountpoint = mountpoint
|
||||||
self.channel = shout.Shout()
|
self.title = title
|
||||||
self.channel.mount = '/' + mountpoint
|
|
||||||
|
|
||||||
self.api_host = options['api_host']
|
self.api_host = options['api_host']
|
||||||
self.channel.url = 'http://deepsouthsounds.com/'
|
self.api_callback_url = options['api_callback_url']
|
||||||
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.twitter_consumer_key = options['twitter_consumer_key']
|
self.twitter_consumer_key = options['twitter_consumer_key']
|
||||||
self.twitter_consumer_secret = options['twitter_consumer_secret']
|
self.twitter_consumer_secret = options['twitter_consumer_secret']
|
||||||
self.twitter_access_token = options['twitter_access_token']
|
self.twitter_access_token = options['twitter_access_token']
|
||||||
self.twitter_access_token_secret = options['twitter_access_token_secret']
|
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
|
self.channel.public = 1
|
||||||
if self.channel.format == 'mp3':
|
if self.channel.format == 'mp3':
|
||||||
self.channel.audio_info = {
|
self.channel.audio_info = {
|
||||||
@@ -52,9 +61,6 @@ class IceRelay(Thread):
|
|||||||
'channels': str(2),
|
'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):
|
def channel_open(self):
|
||||||
if self.channelIsOpen:
|
if self.channelIsOpen:
|
||||||
return True
|
return True
|
||||||
@@ -98,26 +104,19 @@ class IceRelay(Thread):
|
|||||||
while not found:
|
while not found:
|
||||||
r = requests.get('http://{}/_radio?rmix=z'.format(self.api_host))
|
r = requests.get('http://{}/_radio?rmix=z'.format(self.api_host))
|
||||||
v = r.json()
|
v = r.json()
|
||||||
audio = v['url']
|
|
||||||
title = v['title']
|
|
||||||
slug = v['slug']
|
|
||||||
import urllib2
|
import urllib2
|
||||||
try:
|
try:
|
||||||
ret = urllib2.urlopen(audio)
|
ret = urllib2.urlopen(v['url'])
|
||||||
if ret.code == 200:
|
if ret.code == 200:
|
||||||
found = True
|
found = True
|
||||||
ret = [{
|
ret = [v]
|
||||||
'url': audio,
|
|
||||||
'description': title,
|
|
||||||
'slug': slug
|
|
||||||
}]
|
|
||||||
except urllib2.HTTPError as ex:
|
except urllib2.HTTPError as ex:
|
||||||
pass
|
pass
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
ret = [{
|
ret = [{
|
||||||
'url': 'https://dsscdn.blob.core.windows.net/mixes/52df41af-5f81-4f00-a9a8-9ffb5dc3185f.mp3',
|
'url': 'https://dsscdn.blob.core.windows.net/mixes/52df41af-5f81-4f00-a9a8-9ffb5dc3185f.mp3',
|
||||||
'description': 'Default song',
|
'title': 'Default song',
|
||||||
'slug': '/'
|
'slug': '/'
|
||||||
}]
|
}]
|
||||||
|
|
||||||
@@ -145,18 +144,21 @@ class IceRelay(Thread):
|
|||||||
else:
|
else:
|
||||||
item = self.default_queue()[0]
|
item = self.default_queue()[0]
|
||||||
|
|
||||||
self.channel.set_metadata({'song': str(item['description']), 'charset': 'utf-8'})
|
self.channel.set_metadata({'song': str(item['title']), 'charset': 'utf-8'})
|
||||||
logging.debug("Playing: {}".format(item['description']))
|
logging.debug("Playing: {}".format(item['title']))
|
||||||
self.stream = self.file_read_remote(item['url'])
|
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 \
|
if self.twitter_access_token and self.twitter_access_token_secret and \
|
||||||
self.twitter_consumer_secret and self.twitter_consumer_key:
|
self.twitter_consumer_secret and self.twitter_consumer_key:
|
||||||
try:
|
try:
|
||||||
tw = Twitter(key=self.twitter_consumer_key, secret=self.twitter_consumer_secret,
|
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)
|
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:
|
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
|
self._ended = False
|
||||||
return True
|
return True
|
||||||
@@ -175,6 +177,13 @@ class IceRelay(Thread):
|
|||||||
try:
|
try:
|
||||||
self.channel.send(self.chunk)
|
self.channel.send(self.chunk)
|
||||||
self.channel.sync()
|
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:
|
except Exception as ex:
|
||||||
logging.error("Error sending chunk: {0}".format(ex))
|
logging.error("Error sending chunk: {0}".format(ex))
|
||||||
self.channel_close()
|
self.channel_close()
|
||||||
@@ -196,3 +205,9 @@ class IceRelay(Thread):
|
|||||||
break
|
break
|
||||||
yield __main_chunk
|
yield __main_chunk
|
||||||
m.close()
|
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)
|
||||||
|
|||||||
19
server.py
19
server.py
@@ -25,13 +25,20 @@ class ShuffleAudioHandler(tornado.web.RequestHandler):
|
|||||||
except Exception, ex:
|
except Exception, ex:
|
||||||
raise tornado.web.HTTPError(500, ex.message)
|
raise tornado.web.HTTPError(500, ex.message)
|
||||||
|
|
||||||
|
|
||||||
class PlayAudioHandler(tornado.web.RequestHandler):
|
class PlayAudioHandler(tornado.web.RequestHandler):
|
||||||
def post(self, *args, **kwargs):
|
def post(self, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
data = tornado.escape.json_decode(self.request.body)
|
""" check the arguments we've got until we're satisfied they are correct """
|
||||||
in_file = data.get('audio_file')
|
args = self.request.body_arguments
|
||||||
if in_file is not None:
|
if 'slug' in args and 'title' in args and 'url' in args:
|
||||||
relay.set_audio_queue([in_file])
|
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:
|
except Exception, ex:
|
||||||
raise tornado.web.HTTPError(500, ex.message)
|
raise tornado.web.HTTPError(500, ex.message)
|
||||||
|
|
||||||
@@ -59,6 +66,7 @@ def try_exit():
|
|||||||
tornado.ioloop.IOLoop.instance().stop()
|
tornado.ioloop.IOLoop.instance().stop()
|
||||||
logging.info('exit success')
|
logging.info('exit success')
|
||||||
|
|
||||||
|
|
||||||
define("port", default=8888, help="run on the given port", type=int)
|
define("port", default=8888, help="run on the given port", type=int)
|
||||||
define("debug", default=True, help="run in debug mode")
|
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_format", default='mp3', help="Format of the icecast server (mp3, vorbis, flac)")
|
||||||
define("ice_protocol", default='http', help="Protocol (currently only http)")
|
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_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_key", default='', help="Key for posting to twitter")
|
||||||
define("twitter_consumer_secret", default='', help="Secret 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", default='', help="Key for posting to twitter")
|
||||||
define("twitter_access_token_secret", default='', help="Secret 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")
|
tornado.options.parse_config_file("dss.radio.conf")
|
||||||
relay = IceRelay(options=options)
|
relay = IceRelay(options=options)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user