2012-05-14 22:09:49 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2011-03-03 06:22:28 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import logging.config
|
|
|
|
import json
|
2011-03-04 02:13:55 +01:00
|
|
|
import telnetlib
|
2012-03-01 23:58:44 +01:00
|
|
|
import copy
|
2012-08-20 18:11:03 +02:00
|
|
|
import subprocess
|
2013-03-22 17:16:17 +01:00
|
|
|
import signal
|
2013-03-15 17:50:23 +01:00
|
|
|
from datetime import datetime
|
2013-04-05 23:31:55 +02:00
|
|
|
import traceback
|
2013-05-16 18:25:21 +02:00
|
|
|
import pure
|
2011-03-23 06:09:27 +01:00
|
|
|
|
2012-06-28 18:12:22 +02:00
|
|
|
from Queue import Empty
|
2015-05-25 21:37:45 +02:00
|
|
|
from threading import Thread, Timer
|
2013-03-14 21:50:55 +01:00
|
|
|
from subprocess import Popen, PIPE
|
2012-06-28 18:12:22 +02:00
|
|
|
|
2011-03-03 06:22:28 +01:00
|
|
|
from api_clients import api_client
|
2012-04-21 00:32:10 +02:00
|
|
|
from std_err_override import LogWriter
|
2013-06-11 21:55:17 +02:00
|
|
|
from timeout import ls_timeout
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2013-05-16 18:25:21 +02:00
|
|
|
|
2011-03-21 00:34:43 +01:00
|
|
|
# configure logging
|
2013-01-31 18:33:14 +01:00
|
|
|
logging_cfg = os.path.join(os.path.dirname(__file__), "logging.cfg")
|
|
|
|
logging.config.fileConfig(logging_cfg)
|
2012-04-21 00:32:10 +02:00
|
|
|
logger = logging.getLogger()
|
|
|
|
LogWriter.override_std_err(logger)
|
|
|
|
|
2013-03-22 17:16:17 +01:00
|
|
|
def keyboardInterruptHandler(signum, frame):
|
|
|
|
logger = logging.getLogger()
|
|
|
|
logger.info('\nKeyboard Interrupt\n')
|
|
|
|
sys.exit(0)
|
|
|
|
signal.signal(signal.SIGINT, keyboardInterruptHandler)
|
|
|
|
|
2012-04-21 00:32:10 +02:00
|
|
|
#need to wait for Python 2.7 for this..
|
|
|
|
#logging.captureWarnings(True)
|
2011-03-21 00:34:43 +01:00
|
|
|
|
2015-02-24 18:03:35 +01:00
|
|
|
POLL_INTERVAL = 480
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2011-03-21 00:34:43 +01:00
|
|
|
class PypoFetch(Thread):
|
2013-06-11 23:26:48 +02:00
|
|
|
|
2013-04-26 04:20:03 +02:00
|
|
|
def __init__(self, pypoFetch_q, pypoPush_q, media_q, telnet_lock, pypo_liquidsoap, config):
|
2011-03-21 00:34:43 +01:00
|
|
|
Thread.__init__(self)
|
2013-05-02 23:59:03 +02:00
|
|
|
|
2013-06-11 23:26:48 +02:00
|
|
|
#Hacky...
|
|
|
|
PypoFetch.ref = self
|
|
|
|
|
2012-07-16 21:33:44 +02:00
|
|
|
self.api_client = api_client.AirtimeApiClient()
|
2012-02-28 20:44:39 +01:00
|
|
|
self.fetch_queue = pypoFetch_q
|
|
|
|
self.push_queue = pypoPush_q
|
2012-03-01 23:58:44 +01:00
|
|
|
self.media_prepare_queue = media_q
|
2012-06-08 20:57:59 +02:00
|
|
|
self.last_update_schedule_timestamp = time.time()
|
2013-04-26 04:11:26 +02:00
|
|
|
self.config = config
|
2012-06-27 20:37:16 +02:00
|
|
|
self.listener_timeout = POLL_INTERVAL
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-16 04:14:19 +01:00
|
|
|
self.telnet_lock = telnet_lock
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-04-22 23:33:56 +02:00
|
|
|
self.logger = logging.getLogger()
|
|
|
|
|
|
|
|
self.pypo_liquidsoap = pypo_liquidsoap
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-02-23 02:41:24 +01:00
|
|
|
self.cache_dir = os.path.join(config["cache_dir"], "scheduler")
|
2012-02-29 04:33:19 +01:00
|
|
|
self.logger.debug("Cache dir %s", self.cache_dir)
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2012-02-23 02:41:24 +01:00
|
|
|
try:
|
2012-02-28 21:32:18 +01:00
|
|
|
if not os.path.isdir(dir):
|
|
|
|
"""
|
|
|
|
We get here if path does not exist, or path does exist but
|
2012-08-17 21:38:40 +02:00
|
|
|
is a file. We are not handling the second case, but don't
|
2012-02-28 21:32:18 +01:00
|
|
|
think we actually care about handling it.
|
|
|
|
"""
|
2012-02-29 04:33:19 +01:00
|
|
|
self.logger.debug("Cache dir does not exist. Creating...")
|
2012-02-23 02:41:24 +01:00
|
|
|
os.makedirs(dir)
|
|
|
|
except Exception, e:
|
2012-02-28 21:32:18 +01:00
|
|
|
pass
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-08-15 22:10:46 +02:00
|
|
|
self.schedule_data = []
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.info("PypoFetch: init complete")
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-08-15 22:10:46 +02:00
|
|
|
"""
|
|
|
|
Handle a message from RabbitMQ, put it into our yucky global var.
|
|
|
|
Hopefully there is a better way to do this.
|
|
|
|
"""
|
2012-02-12 05:53:43 +01:00
|
|
|
def handle_message(self, message):
|
2012-06-27 04:41:11 +02:00
|
|
|
try:
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.info("Received event from Pypo Message Handler: %s" % message)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
|
|
|
m = json.loads(message)
|
2011-09-13 20:56:24 +02:00
|
|
|
command = m['event_type']
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.info("Handling command: " + command)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-09-13 20:56:24 +02:00
|
|
|
if command == 'update_schedule':
|
2012-06-27 04:41:11 +02:00
|
|
|
self.schedule_data = m['schedule']
|
2012-03-17 18:55:56 +01:00
|
|
|
self.process_schedule(self.schedule_data)
|
2012-09-15 00:20:46 +02:00
|
|
|
elif command == 'reset_liquidsoap_bootstrap':
|
|
|
|
self.set_bootstrap_variables()
|
2011-09-13 20:56:24 +02:00
|
|
|
elif command == 'update_stream_setting':
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.info("Updating stream setting...")
|
2012-08-19 03:52:06 +02:00
|
|
|
self.regenerate_liquidsoap_conf(m['setting'])
|
2012-02-11 00:43:40 +01:00
|
|
|
elif command == 'update_stream_format':
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.info("Updating stream format...")
|
2012-02-11 00:43:40 +01:00
|
|
|
self.update_liquidsoap_stream_format(m['stream_format'])
|
|
|
|
elif command == 'update_station_name':
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.info("Updating station name...")
|
2012-02-11 00:43:40 +01:00
|
|
|
self.update_liquidsoap_station_name(m['station_name'])
|
2012-03-21 03:16:17 +01:00
|
|
|
elif command == 'update_transition_fade':
|
|
|
|
self.logger.info("Updating transition_fade...")
|
|
|
|
self.update_liquidsoap_transition_fade(m['transition_fade'])
|
2012-03-08 23:42:38 +01:00
|
|
|
elif command == 'switch_source':
|
|
|
|
self.logger.info("switch_on_source show command received...")
|
2013-05-03 00:46:16 +02:00
|
|
|
self.pypo_liquidsoap.\
|
|
|
|
get_telnet_dispatcher().\
|
|
|
|
switch_source(m['sourcename'], m['status'])
|
2012-03-14 16:09:59 +01:00
|
|
|
elif command == 'disconnect_source':
|
|
|
|
self.logger.info("disconnect_on_source show command received...")
|
2013-05-03 00:46:16 +02:00
|
|
|
self.pypo_liquidsoap.get_telnet_dispatcher().\
|
|
|
|
disconnect_source(m['sourcename'])
|
2012-09-15 01:01:21 +02:00
|
|
|
else:
|
|
|
|
self.logger.info("Unknown command: %s" % command)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-06-08 20:57:59 +02:00
|
|
|
# update timeout value
|
|
|
|
if command == 'update_schedule':
|
2012-06-27 20:37:16 +02:00
|
|
|
self.listener_timeout = POLL_INTERVAL
|
2012-06-08 20:57:59 +02:00
|
|
|
else:
|
2012-06-27 20:37:16 +02:00
|
|
|
self.listener_timeout = self.last_update_schedule_timestamp - time.time() + POLL_INTERVAL
|
2012-06-08 20:57:59 +02:00
|
|
|
if self.listener_timeout < 0:
|
|
|
|
self.listener_timeout = 0
|
|
|
|
self.logger.info("New timeout: %s" % self.listener_timeout)
|
2011-09-13 20:56:24 +02:00
|
|
|
except Exception, e:
|
2012-02-28 20:44:39 +01:00
|
|
|
top = traceback.format_exc()
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.error('Exception: %s', e)
|
|
|
|
self.logger.error("traceback: %s", top)
|
|
|
|
self.logger.error("Exception in handling Message Handler message: %s", e)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
|
|
|
|
2013-01-29 18:17:05 +01:00
|
|
|
def switch_source_temp(self, sourcename, status):
|
|
|
|
self.logger.debug('Switching source: %s to "%s" status', sourcename, status)
|
|
|
|
command = "streams."
|
|
|
|
if sourcename == "master_dj":
|
|
|
|
command += "master_dj_"
|
|
|
|
elif sourcename == "live_dj":
|
|
|
|
command += "live_dj_"
|
|
|
|
elif sourcename == "scheduled_play":
|
|
|
|
command += "scheduled_play_"
|
|
|
|
|
|
|
|
if status == "on":
|
|
|
|
command += "start\n"
|
|
|
|
else:
|
|
|
|
command += "stop\n"
|
|
|
|
|
|
|
|
return command
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-14 15:22:41 +01:00
|
|
|
"""
|
2013-04-26 04:32:27 +02:00
|
|
|
Initialize Liquidsoap environment
|
2012-03-14 15:22:41 +01:00
|
|
|
"""
|
2012-03-20 21:41:15 +01:00
|
|
|
def set_bootstrap_variables(self):
|
|
|
|
self.logger.debug('Getting information needed on bootstrap from Airtime')
|
2013-02-04 22:05:58 +01:00
|
|
|
try:
|
|
|
|
info = self.api_client.get_bootstrap_info()
|
|
|
|
except Exception, e:
|
2012-06-27 22:29:33 +02:00
|
|
|
self.logger.error('Unable to get bootstrap info.. Exiting pypo...')
|
2013-02-04 22:05:58 +01:00
|
|
|
self.logger.error(str(e))
|
|
|
|
|
|
|
|
self.logger.debug('info:%s', info)
|
|
|
|
commands = []
|
|
|
|
for k, v in info['switch_status'].iteritems():
|
|
|
|
commands.append(self.switch_source_temp(k, v))
|
|
|
|
|
|
|
|
stream_format = info['stream_label']
|
|
|
|
station_name = info['station_name']
|
|
|
|
fade = info['transition_fade']
|
|
|
|
|
|
|
|
commands.append(('vars.stream_metadata_type %s\n' % stream_format).encode('utf-8'))
|
|
|
|
commands.append(('vars.station_name %s\n' % station_name).encode('utf-8'))
|
|
|
|
commands.append(('vars.default_dj_fade %s\n' % fade).encode('utf-8'))
|
2013-05-03 00:46:16 +02:00
|
|
|
self.pypo_liquidsoap.get_telnet_dispatcher().telnet_send(commands)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-06-13 20:06:07 +02:00
|
|
|
self.pypo_liquidsoap.clear_all_queues()
|
2013-05-06 22:39:17 +02:00
|
|
|
self.pypo_liquidsoap.clear_queue_tracker()
|
|
|
|
|
2012-08-19 06:41:25 +02:00
|
|
|
def restart_liquidsoap(self):
|
2012-08-19 03:52:06 +02:00
|
|
|
try:
|
2013-06-11 21:55:17 +02:00
|
|
|
"""do not block - if we receive the lock then good - no other thread
|
|
|
|
will try communicating with Liquidsoap. If we don't receive, it may
|
|
|
|
mean some thread blocked and is still holding the lock. Restarting
|
|
|
|
Liquidsoap will cause that thread to release the lock as an Exception
|
|
|
|
will be thrown."""
|
|
|
|
self.telnet_lock.acquire(False)
|
|
|
|
|
|
|
|
|
2012-08-20 18:11:03 +02:00
|
|
|
self.logger.info("Restarting Liquidsoap")
|
2014-10-30 22:41:00 +01:00
|
|
|
subprocess.call('/etc/init.d/airtime-liquidsoap restart', shell=True, close_fds=True)
|
2012-08-20 18:11:03 +02:00
|
|
|
|
2012-08-20 20:41:34 +02:00
|
|
|
#Wait here and poll Liquidsoap until it has started up
|
2012-08-20 18:11:03 +02:00
|
|
|
self.logger.info("Waiting for Liquidsoap to start")
|
|
|
|
while True:
|
|
|
|
try:
|
2013-04-26 04:11:26 +02:00
|
|
|
tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port'])
|
2012-08-20 18:11:03 +02:00
|
|
|
tn.write("exit\n")
|
|
|
|
tn.read_all()
|
|
|
|
self.logger.info("Liquidsoap is up and running")
|
|
|
|
break
|
|
|
|
except Exception, e:
|
|
|
|
#sleep 0.5 seconds and try again
|
|
|
|
time.sleep(0.5)
|
|
|
|
|
2012-08-19 03:52:06 +02:00
|
|
|
except Exception, e:
|
2012-08-19 06:41:25 +02:00
|
|
|
self.logger.error(e)
|
2012-08-20 18:11:03 +02:00
|
|
|
finally:
|
2013-07-17 21:29:21 +02:00
|
|
|
if self.telnet_lock.locked():
|
|
|
|
self.telnet_lock.release()
|
2012-08-20 18:11:03 +02:00
|
|
|
|
2013-04-26 04:32:27 +02:00
|
|
|
"""
|
|
|
|
TODO: This function needs to be way shorter, and refactored :/ - MK
|
|
|
|
"""
|
2012-08-19 03:52:06 +02:00
|
|
|
def regenerate_liquidsoap_conf(self, setting):
|
2015-03-11 00:15:38 +01:00
|
|
|
self.restart_liquidsoap()
|
|
|
|
self.update_liquidsoap_connection_status()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-02-29 04:33:19 +01:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
@ls_timeout
|
2012-02-28 19:58:10 +01:00
|
|
|
def update_liquidsoap_connection_status(self):
|
|
|
|
"""
|
2012-08-20 18:11:03 +02:00
|
|
|
updates the status of Liquidsoap connection to the streaming server
|
|
|
|
This function updates the bootup time variable in Liquidsoap script
|
2012-03-14 15:22:41 +01:00
|
|
|
"""
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-16 04:14:19 +01:00
|
|
|
try:
|
2013-01-25 20:16:53 +01:00
|
|
|
self.telnet_lock.acquire()
|
2013-04-26 04:11:26 +02:00
|
|
|
tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port'])
|
2012-08-20 18:11:03 +02:00
|
|
|
# update the boot up time of Liquidsoap. Since Liquidsoap is not restarting,
|
2012-03-16 04:14:19 +01:00
|
|
|
# we are manually adjusting the bootup time variable so the status msg will get
|
|
|
|
# updated.
|
|
|
|
current_time = time.time()
|
2012-06-27 04:41:11 +02:00
|
|
|
boot_up_time_command = "vars.bootup_time " + str(current_time) + "\n"
|
2013-01-28 23:00:16 +01:00
|
|
|
self.logger.info(boot_up_time_command)
|
2012-03-16 04:14:19 +01:00
|
|
|
tn.write(boot_up_time_command)
|
2013-01-28 23:00:16 +01:00
|
|
|
|
|
|
|
connection_status = "streams.connection_status\n"
|
|
|
|
self.logger.info(connection_status)
|
|
|
|
tn.write(connection_status)
|
|
|
|
|
2012-03-16 04:14:19 +01:00
|
|
|
tn.write('exit\n')
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-16 04:14:19 +01:00
|
|
|
output = tn.read_all()
|
|
|
|
except Exception, e:
|
|
|
|
self.logger.error(str(e))
|
|
|
|
finally:
|
|
|
|
self.telnet_lock.release()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-12-24 16:59:09 +01:00
|
|
|
output_list = output.split("\r\n")
|
|
|
|
stream_info = output_list[2]
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-12-24 16:59:09 +01:00
|
|
|
# streamin info is in the form of:
|
|
|
|
# eg. s1:true,2:true,3:false
|
|
|
|
streams = stream_info.split(",")
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.info(streams)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-12-24 16:59:09 +01:00
|
|
|
fake_time = current_time + 1
|
|
|
|
for s in streams:
|
|
|
|
info = s.split(':')
|
|
|
|
stream_id = info[0]
|
|
|
|
status = info[1]
|
|
|
|
if(status == "true"):
|
|
|
|
self.api_client.notify_liquidsoap_status("OK", stream_id, str(fake_time))
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-01-27 02:05:31 +01:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
@ls_timeout
|
2012-02-11 00:43:40 +01:00
|
|
|
def update_liquidsoap_stream_format(self, stream_format):
|
|
|
|
# Push stream metadata to liquidsoap
|
|
|
|
# TODO: THIS LIQUIDSOAP STUFF NEEDS TO BE MOVED TO PYPO-PUSH!!!
|
|
|
|
try:
|
2012-03-16 04:14:19 +01:00
|
|
|
self.telnet_lock.acquire()
|
2013-04-26 04:11:26 +02:00
|
|
|
tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port'])
|
2012-03-17 01:47:46 +01:00
|
|
|
command = ('vars.stream_metadata_type %s\n' % stream_format).encode('utf-8')
|
|
|
|
self.logger.info(command)
|
|
|
|
tn.write(command)
|
|
|
|
tn.write('exit\n')
|
|
|
|
tn.read_all()
|
2012-02-11 00:43:40 +01:00
|
|
|
except Exception, e:
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.error("Exception %s", e)
|
2012-03-17 01:47:46 +01:00
|
|
|
finally:
|
|
|
|
self.telnet_lock.release()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
@ls_timeout
|
2012-03-21 03:16:17 +01:00
|
|
|
def update_liquidsoap_transition_fade(self, fade):
|
|
|
|
# Push stream metadata to liquidsoap
|
|
|
|
# TODO: THIS LIQUIDSOAP STUFF NEEDS TO BE MOVED TO PYPO-PUSH!!!
|
|
|
|
try:
|
|
|
|
self.telnet_lock.acquire()
|
2013-04-26 04:11:26 +02:00
|
|
|
tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port'])
|
2012-03-21 03:16:17 +01:00
|
|
|
command = ('vars.default_dj_fade %s\n' % fade).encode('utf-8')
|
|
|
|
self.logger.info(command)
|
|
|
|
tn.write(command)
|
|
|
|
tn.write('exit\n')
|
|
|
|
tn.read_all()
|
|
|
|
except Exception, e:
|
|
|
|
self.logger.error("Exception %s", e)
|
|
|
|
finally:
|
|
|
|
self.telnet_lock.release()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
@ls_timeout
|
2012-02-11 00:43:40 +01:00
|
|
|
def update_liquidsoap_station_name(self, station_name):
|
2011-03-23 06:09:27 +01:00
|
|
|
# Push stream metadata to liquidsoap
|
|
|
|
# TODO: THIS LIQUIDSOAP STUFF NEEDS TO BE MOVED TO PYPO-PUSH!!!
|
2011-03-03 06:22:28 +01:00
|
|
|
try:
|
2012-03-16 04:14:19 +01:00
|
|
|
try:
|
2013-01-25 20:16:53 +01:00
|
|
|
self.telnet_lock.acquire()
|
2013-04-26 04:11:26 +02:00
|
|
|
tn = telnetlib.Telnet(self.config['ls_host'], self.config['ls_port'])
|
2012-03-16 04:14:19 +01:00
|
|
|
command = ('vars.station_name %s\n' % station_name).encode('utf-8')
|
|
|
|
self.logger.info(command)
|
|
|
|
tn.write(command)
|
|
|
|
tn.write('exit\n')
|
2012-06-27 04:41:11 +02:00
|
|
|
tn.read_all()
|
2012-03-16 04:14:19 +01:00
|
|
|
except Exception, e:
|
|
|
|
self.logger.error(str(e))
|
|
|
|
finally:
|
|
|
|
self.telnet_lock.release()
|
2011-03-23 06:09:27 +01:00
|
|
|
except Exception, e:
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.error("Exception %s", e)
|
2012-02-11 00:43:40 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
Process the schedule
|
|
|
|
- Reads the scheduled entries of a given range (actual time +/- "prepare_ahead" / "cache_for")
|
|
|
|
- Saves a serialized file of the schedule
|
|
|
|
- playlists are prepared. (brought to liquidsoap format) and, if not mounted via nsf, files are copied
|
|
|
|
to the cache dir (Folder-structure: cache/YYYY-MM-DD-hh-mm-ss)
|
|
|
|
- runs the cleanup routine, to get rid of unused cached files
|
|
|
|
"""
|
2012-06-27 04:41:11 +02:00
|
|
|
def process_schedule(self, schedule_data):
|
2012-06-18 05:24:15 +02:00
|
|
|
self.last_update_schedule_timestamp = time.time()
|
2012-02-28 21:32:18 +01:00
|
|
|
self.logger.debug(schedule_data)
|
2012-02-27 19:52:35 +01:00
|
|
|
media = schedule_data["media"]
|
2012-03-28 21:12:01 +02:00
|
|
|
media_filtered = {}
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2011-06-15 21:49:42 +02:00
|
|
|
# Download all the media and put playlists in liquidsoap "annotate" format
|
2011-03-21 00:34:43 +01:00
|
|
|
try:
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
"""
|
|
|
|
Make sure cache_dir exists
|
|
|
|
"""
|
|
|
|
download_dir = self.cache_dir
|
|
|
|
try:
|
|
|
|
os.makedirs(download_dir)
|
|
|
|
except Exception, e:
|
|
|
|
pass
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-03-14 21:50:55 +01:00
|
|
|
media_copy = {}
|
2012-03-01 23:58:44 +01:00
|
|
|
for key in media:
|
|
|
|
media_item = media[key]
|
2012-08-17 21:38:24 +02:00
|
|
|
if (media_item['type'] == 'file'):
|
2013-05-07 21:05:14 +02:00
|
|
|
self.sanity_check_media_item(media_item)
|
2012-03-28 21:12:01 +02:00
|
|
|
fileExt = os.path.splitext(media_item['uri'])[1]
|
2012-07-26 20:41:09 +02:00
|
|
|
dst = os.path.join(download_dir, unicode(media_item['id']) + fileExt)
|
2012-03-28 21:12:01 +02:00
|
|
|
media_item['dst'] = dst
|
2012-07-03 23:06:35 +02:00
|
|
|
media_item['file_ready'] = False
|
2012-03-28 21:12:01 +02:00
|
|
|
media_filtered[key] = media_item
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-05-30 20:02:05 +02:00
|
|
|
media_item['start'] = datetime.strptime(media_item['start'],
|
|
|
|
"%Y-%m-%d-%H-%M-%S")
|
|
|
|
media_item['end'] = datetime.strptime(media_item['end'],
|
|
|
|
"%Y-%m-%d-%H-%M-%S")
|
|
|
|
media_copy[key] = media_item
|
2013-03-14 21:50:55 +01:00
|
|
|
|
|
|
|
|
2012-03-28 21:12:01 +02:00
|
|
|
self.media_prepare_queue.put(copy.copy(media_filtered))
|
2012-02-27 19:52:35 +01:00
|
|
|
except Exception, e: self.logger.error("%s", e)
|
2011-03-21 00:34:43 +01:00
|
|
|
|
2011-03-23 06:09:27 +01:00
|
|
|
# Send the data to pypo-push
|
2012-03-01 23:58:44 +01:00
|
|
|
self.logger.debug("Pushing to pypo-push")
|
2013-03-14 21:50:55 +01:00
|
|
|
self.push_queue.put(media_copy)
|
2012-02-27 19:52:35 +01:00
|
|
|
|
2012-03-06 01:02:46 +01:00
|
|
|
|
2011-03-03 06:22:28 +01:00
|
|
|
# cleanup
|
2012-03-17 18:55:56 +01:00
|
|
|
try: self.cache_cleanup(media)
|
2012-02-27 19:52:35 +01:00
|
|
|
except Exception, e: self.logger.error("%s", e)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-05-07 21:05:14 +02:00
|
|
|
#do basic validation of file parameters. Useful for debugging
|
|
|
|
#purposes
|
|
|
|
def sanity_check_media_item(self, media_item):
|
|
|
|
start = datetime.strptime(media_item['start'], "%Y-%m-%d-%H-%M-%S")
|
|
|
|
end = datetime.strptime(media_item['end'], "%Y-%m-%d-%H-%M-%S")
|
|
|
|
|
2013-05-16 18:25:21 +02:00
|
|
|
length1 = pure.date_interval_to_seconds(end - start)
|
2013-05-07 21:05:14 +02:00
|
|
|
length2 = media_item['cue_out'] - media_item['cue_in']
|
|
|
|
|
|
|
|
if abs(length2 - length1) > 1:
|
|
|
|
self.logger.error("end - start length: %s", length1)
|
|
|
|
self.logger.error("cue_out - cue_in length: %s", length2)
|
|
|
|
self.logger.error("Two lengths are not equal!!!")
|
|
|
|
|
2013-03-01 22:15:28 +01:00
|
|
|
def is_file_opened(self, path):
|
|
|
|
#Capture stderr to avoid polluting py-interpreter.log
|
|
|
|
proc = Popen(["lsof", path], stdout=PIPE, stderr=PIPE)
|
|
|
|
out = proc.communicate()[0].strip()
|
|
|
|
return bool(out)
|
|
|
|
|
2012-03-17 18:55:56 +01:00
|
|
|
def cache_cleanup(self, media):
|
2012-03-06 01:02:46 +01:00
|
|
|
"""
|
|
|
|
Get list of all files in the cache dir and remove them if they aren't being used anymore.
|
|
|
|
Input dict() media, lists all files that are scheduled or currently playing. Not being in this
|
2012-08-17 21:38:40 +02:00
|
|
|
dict() means the file is safe to remove.
|
2012-03-06 01:02:46 +01:00
|
|
|
"""
|
|
|
|
cached_file_set = set(os.listdir(self.cache_dir))
|
|
|
|
scheduled_file_set = set()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-06 01:02:46 +01:00
|
|
|
for mkey in media:
|
|
|
|
media_item = media[mkey]
|
2012-07-25 22:56:33 +02:00
|
|
|
if media_item['type'] == 'file':
|
|
|
|
fileExt = os.path.splitext(media_item['uri'])[1]
|
2012-07-26 20:41:09 +02:00
|
|
|
scheduled_file_set.add(unicode(media_item["id"]) + fileExt)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-07-25 22:56:33 +02:00
|
|
|
expired_files = cached_file_set - scheduled_file_set
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-07-25 22:56:33 +02:00
|
|
|
self.logger.debug("Files to remove " + str(expired_files))
|
|
|
|
for f in expired_files:
|
|
|
|
try:
|
2013-03-01 22:15:28 +01:00
|
|
|
path = os.path.join(self.cache_dir, f)
|
|
|
|
self.logger.debug("Removing %s" % path)
|
|
|
|
|
|
|
|
#check if this file is opened (sometimes Liquidsoap is still
|
|
|
|
#playing the file due to our knowledge of the track length
|
|
|
|
#being incorrect!)
|
2013-03-15 20:07:55 +01:00
|
|
|
if not self.is_file_opened(path):
|
2013-03-01 22:15:28 +01:00
|
|
|
os.remove(path)
|
2013-04-05 23:31:55 +02:00
|
|
|
self.logger.info("File '%s' removed" % path)
|
|
|
|
else:
|
|
|
|
self.logger.info("File '%s' not removed. Still busy!" % path)
|
2012-07-25 22:56:33 +02:00
|
|
|
except Exception, e:
|
2013-04-05 23:31:55 +02:00
|
|
|
self.logger.error("Problem removing file '%s'" % f)
|
|
|
|
self.logger.error(traceback.format_exc())
|
2011-03-23 06:09:27 +01:00
|
|
|
|
2012-08-20 18:11:03 +02:00
|
|
|
def manual_schedule_fetch(self):
|
|
|
|
success, self.schedule_data = self.api_client.get_schedule()
|
|
|
|
if success:
|
|
|
|
self.process_schedule(self.schedule_data)
|
|
|
|
return success
|
|
|
|
|
2013-01-25 18:11:50 +01:00
|
|
|
def persistent_manual_schedule_fetch(self, max_attempts=1):
|
|
|
|
success = False
|
|
|
|
num_attempts = 0
|
|
|
|
while not success and num_attempts < max_attempts:
|
|
|
|
success = self.manual_schedule_fetch()
|
|
|
|
num_attempts += 1
|
|
|
|
|
|
|
|
return success
|
|
|
|
|
2015-05-25 21:37:45 +02:00
|
|
|
# This function makes a request to Airtime to see if we need to
|
|
|
|
# push metadata to TuneIn. We have to do this because TuneIn turns
|
|
|
|
# off metadata if it does not receive a request every 5 minutes.
|
|
|
|
def update_metadata_on_tunein(self):
|
|
|
|
self.api_client.update_metadata_on_tunein()
|
|
|
|
Timer(120, self.update_metadata_on_tunein).start()
|
2013-01-25 18:11:50 +01:00
|
|
|
|
2011-09-08 18:17:42 +02:00
|
|
|
def main(self):
|
2013-04-22 23:33:56 +02:00
|
|
|
#Make sure all Liquidsoap queues are empty. This is important in the
|
|
|
|
#case where we've just restarted the pypo scheduler, but Liquidsoap still
|
|
|
|
#is playing tracks. In this case let's just restart everything from scratch
|
|
|
|
#so that we can repopulate our dictionary that keeps track of what
|
|
|
|
#Liquidsoap is playing much more easily.
|
|
|
|
self.pypo_liquidsoap.clear_all_queues()
|
|
|
|
|
2013-05-15 23:18:15 +02:00
|
|
|
self.set_bootstrap_variables()
|
|
|
|
|
2015-05-25 21:37:45 +02:00
|
|
|
self.update_metadata_on_tunein()
|
|
|
|
|
2013-05-13 23:52:22 +02:00
|
|
|
# Bootstrap: since we are just starting up, we need to grab the
|
2015-05-25 21:37:45 +02:00
|
|
|
# most recent schedule. After that we fetch the schedule every 8
|
2014-08-14 18:29:52 +02:00
|
|
|
# minutes or wait for schedule updates to get pushed.
|
2013-05-13 23:52:22 +02:00
|
|
|
success = self.persistent_manual_schedule_fetch(max_attempts=5)
|
|
|
|
|
2012-02-27 19:52:35 +01:00
|
|
|
if success:
|
|
|
|
self.logger.info("Bootstrap schedule received: %s", self.schedule_data)
|
2011-09-20 19:25:29 +02:00
|
|
|
|
2012-06-27 04:41:11 +02:00
|
|
|
loops = 1
|
2011-03-21 00:34:43 +01:00
|
|
|
while True:
|
2012-02-27 19:52:35 +01:00
|
|
|
self.logger.info("Loop #%s", loops)
|
2012-06-27 04:41:11 +02:00
|
|
|
try:
|
2011-09-08 18:17:42 +02:00
|
|
|
"""
|
2012-03-12 22:52:17 +01:00
|
|
|
our simple_queue.get() requires a timeout, in which case we
|
|
|
|
fetch the Airtime schedule manually. It is important to fetch
|
2012-08-17 21:38:40 +02:00
|
|
|
the schedule periodically because if we didn't, we would only
|
|
|
|
get schedule updates via RabbitMq if the user was constantly
|
|
|
|
using the Airtime interface.
|
|
|
|
|
2012-03-12 22:52:17 +01:00
|
|
|
If the user is not using the interface, RabbitMq messages are not
|
2012-08-17 21:38:40 +02:00
|
|
|
sent, and we will have very stale (or non-existent!) data about the
|
2012-03-12 22:52:17 +01:00
|
|
|
schedule.
|
2012-08-17 21:38:40 +02:00
|
|
|
|
2012-06-27 20:37:16 +02:00
|
|
|
Currently we are checking every POLL_INTERVAL seconds
|
2011-09-08 18:17:42 +02:00
|
|
|
"""
|
2012-06-27 04:41:11 +02:00
|
|
|
|
|
|
|
|
2012-06-08 20:57:59 +02:00
|
|
|
message = self.fetch_queue.get(block=True, timeout=self.listener_timeout)
|
2012-02-27 19:52:35 +01:00
|
|
|
self.handle_message(message)
|
2012-06-28 18:12:22 +02:00
|
|
|
except Empty, e:
|
|
|
|
self.logger.info("Queue timeout. Fetching schedule manually")
|
2013-01-25 18:11:50 +01:00
|
|
|
self.persistent_manual_schedule_fetch(max_attempts=5)
|
2011-09-08 18:17:42 +02:00
|
|
|
except Exception, e:
|
2012-03-29 22:57:28 +02:00
|
|
|
top = traceback.format_exc()
|
|
|
|
self.logger.error('Exception: %s', e)
|
|
|
|
self.logger.error("traceback: %s", top)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-02-24 19:12:50 +01:00
|
|
|
loops += 1
|
2011-09-08 18:17:42 +02:00
|
|
|
|
|
|
|
def run(self):
|
2012-02-28 21:32:18 +01:00
|
|
|
"""
|
|
|
|
Entry point of the thread
|
|
|
|
"""
|
|
|
|
self.main()
|