more DRY code. also use "with" instead of try/except
This commit is contained in:
parent
bd0becf277
commit
9165ddc0ec
1 changed files with 35 additions and 123 deletions
|
@ -36,14 +36,13 @@ class TelnetLiquidsoap:
|
||||||
self.current_prebuffering_stream_id = None
|
self.current_prebuffering_stream_id = None
|
||||||
|
|
||||||
def __connect(self):
|
def __connect(self):
|
||||||
return telnetlib.Telnet(self.ls_host, self.ls_port)
|
return self.__connect()
|
||||||
|
|
||||||
def __is_empty(self, tn, queue_id):
|
def __is_empty(self, tn, queue_id):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def queue_clear_all(self):
|
def queue_clear_all(self):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
|
||||||
tn = self.__connect()
|
tn = self.__connect()
|
||||||
|
|
||||||
for i in self.queues:
|
for i in self.queues:
|
||||||
|
@ -53,14 +52,9 @@ class TelnetLiquidsoap:
|
||||||
|
|
||||||
tn.write("exit\n")
|
tn.write("exit\n")
|
||||||
self.logger.debug(tn.read_all())
|
self.logger.debug(tn.read_all())
|
||||||
except Exception:
|
|
||||||
raise
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
def queue_remove(self, queue_id):
|
def queue_remove(self, queue_id):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
|
||||||
tn = self.__connect()
|
tn = self.__connect()
|
||||||
|
|
||||||
msg = 'queues.%s_skip\n' % queue_id
|
msg = 'queues.%s_skip\n' % queue_id
|
||||||
|
@ -69,15 +63,9 @@ class TelnetLiquidsoap:
|
||||||
|
|
||||||
tn.write("exit\n")
|
tn.write("exit\n")
|
||||||
self.logger.debug(tn.read_all())
|
self.logger.debug(tn.read_all())
|
||||||
except Exception:
|
|
||||||
raise
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
|
|
||||||
def queue_push(self, queue_id, media_item):
|
def queue_push(self, queue_id, media_item):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
|
||||||
tn = self.__connect()
|
tn = self.__connect()
|
||||||
|
|
||||||
if not self.__is_empty(tn, queue_id):
|
if not self.__is_empty(tn, queue_id):
|
||||||
|
@ -95,17 +83,10 @@ class TelnetLiquidsoap:
|
||||||
|
|
||||||
tn.write("exit\n")
|
tn.write("exit\n")
|
||||||
self.logger.debug(tn.read_all())
|
self.logger.debug(tn.read_all())
|
||||||
except Exception:
|
|
||||||
raise
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
|
|
||||||
def stop_web_stream_buffer(self):
|
def stop_web_stream_buffer(self):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.ls_host, self.ls_port)
|
|
||||||
#dynamic_source.stop http://87.230.101.24:80/top100station.mp3
|
|
||||||
|
|
||||||
msg = 'http.stop\n'
|
msg = 'http.stop\n'
|
||||||
self.logger.debug(msg)
|
self.logger.debug(msg)
|
||||||
|
@ -118,16 +99,9 @@ class TelnetLiquidsoap:
|
||||||
tn.write("exit\n")
|
tn.write("exit\n")
|
||||||
self.logger.debug(tn.read_all())
|
self.logger.debug(tn.read_all())
|
||||||
|
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(str(e))
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
def stop_web_stream_output(self):
|
def stop_web_stream_output(self):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.ls_host, self.ls_port)
|
|
||||||
#dynamic_source.stop http://87.230.101.24:80/top100station.mp3
|
|
||||||
|
|
||||||
msg = 'dynamic_source.output_stop\n'
|
msg = 'dynamic_source.output_stop\n'
|
||||||
self.logger.debug(msg)
|
self.logger.debug(msg)
|
||||||
|
@ -136,15 +110,9 @@ class TelnetLiquidsoap:
|
||||||
tn.write("exit\n")
|
tn.write("exit\n")
|
||||||
self.logger.debug(tn.read_all())
|
self.logger.debug(tn.read_all())
|
||||||
|
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(str(e))
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
def start_web_stream(self, media_item):
|
def start_web_stream(self, media_item):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.ls_host, self.ls_port)
|
|
||||||
|
|
||||||
#TODO: DO we need this?
|
#TODO: DO we need this?
|
||||||
msg = 'streams.scheduled_play_start\n'
|
msg = 'streams.scheduled_play_start\n'
|
||||||
|
@ -158,15 +126,10 @@ class TelnetLiquidsoap:
|
||||||
self.logger.debug(tn.read_all())
|
self.logger.debug(tn.read_all())
|
||||||
|
|
||||||
self.current_prebuffering_stream_id = None
|
self.current_prebuffering_stream_id = None
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(str(e))
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
def start_web_stream_buffer(self, media_item):
|
def start_web_stream_buffer(self, media_item):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.ls_host, self.ls_port)
|
|
||||||
|
|
||||||
msg = 'dynamic_source.id %s\n' % media_item['row_id']
|
msg = 'dynamic_source.id %s\n' % media_item['row_id']
|
||||||
self.logger.debug(msg)
|
self.logger.debug(msg)
|
||||||
|
@ -180,15 +143,10 @@ class TelnetLiquidsoap:
|
||||||
self.logger.debug(tn.read_all())
|
self.logger.debug(tn.read_all())
|
||||||
|
|
||||||
self.current_prebuffering_stream_id = media_item['row_id']
|
self.current_prebuffering_stream_id = media_item['row_id']
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(str(e))
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
def get_current_stream_id(self):
|
def get_current_stream_id(self):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.ls_host, self.ls_port)
|
|
||||||
|
|
||||||
msg = 'dynamic_source.get_id\n'
|
msg = 'dynamic_source.get_id\n'
|
||||||
self.logger.debug(msg)
|
self.logger.debug(msg)
|
||||||
|
@ -199,10 +157,6 @@ class TelnetLiquidsoap:
|
||||||
self.logger.debug("stream_id: %s" % stream_id)
|
self.logger.debug("stream_id: %s" % stream_id)
|
||||||
|
|
||||||
return stream_id
|
return stream_id
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(str(e))
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
def disconnect_source(self, sourcename):
|
def disconnect_source(self, sourcename):
|
||||||
self.logger.debug('Disconnecting source: %s', sourcename)
|
self.logger.debug('Disconnecting source: %s', sourcename)
|
||||||
|
@ -212,34 +166,22 @@ class TelnetLiquidsoap:
|
||||||
elif(sourcename == "live_dj"):
|
elif(sourcename == "live_dj"):
|
||||||
command += "live_dj_harbor.kick\n"
|
command += "live_dj_harbor.kick\n"
|
||||||
|
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.ls_host, self.ls_port)
|
|
||||||
self.logger.info(command)
|
self.logger.info(command)
|
||||||
tn.write(command)
|
tn.write(command)
|
||||||
tn.write('exit\n')
|
tn.write('exit\n')
|
||||||
tn.read_all()
|
tn.read_all()
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(traceback.format_exc())
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
def telnet_send(self, commands):
|
def telnet_send(self, commands):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
|
|
||||||
tn = telnetlib.Telnet(self.ls_host, self.ls_port)
|
|
||||||
for i in commands:
|
for i in commands:
|
||||||
self.logger.info(i)
|
self.logger.info(i)
|
||||||
tn.write(i)
|
tn.write(i)
|
||||||
|
|
||||||
tn.write('exit\n')
|
tn.write('exit\n')
|
||||||
tn.read_all()
|
tn.read_all()
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(str(e))
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
|
|
||||||
def switch_source(self, sourcename, status):
|
def switch_source(self, sourcename, status):
|
||||||
self.logger.debug('Switching source: %s to "%s" status', sourcename,
|
self.logger.debug('Switching source: %s to "%s" status', sourcename,
|
||||||
|
@ -262,44 +204,29 @@ class TelnetLiquidsoap:
|
||||||
def liquidsoap_get_info(self):
|
def liquidsoap_get_info(self):
|
||||||
self.logger.debug("Checking to see if Liquidsoap is running")
|
self.logger.debug("Checking to see if Liquidsoap is running")
|
||||||
response = ""
|
response = ""
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.host, self.port)
|
|
||||||
msg = "version\n"
|
msg = "version\n"
|
||||||
tn.write(msg)
|
tn.write(msg)
|
||||||
tn.write("exit\n")
|
tn.write("exit\n")
|
||||||
response = tn.read_all()
|
response = tn.read_all()
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(str(e))
|
|
||||||
return None
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def update_liquidsoap_station_name(self, station_name):
|
def update_liquidsoap_station_name(self, station_name):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
try:
|
tn = self.__connect()
|
||||||
self.telnet_lock.acquire()
|
command = ('vars.station_name %s\n' %
|
||||||
tn = telnetlib.Telnet(self.host, self.port)
|
station_name).encode('utf-8')
|
||||||
command = ('vars.station_name %s\n' %
|
self.logger.info(command)
|
||||||
station_name).encode('utf-8')
|
tn.write(command)
|
||||||
self.logger.info(command)
|
tn.write('exit\n')
|
||||||
tn.write(command)
|
tn.read_all()
|
||||||
tn.write('exit\n')
|
|
||||||
tn.read_all()
|
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(str(e))
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
except Exception, e:
|
|
||||||
self.logger.error("Exception %s", e)
|
|
||||||
|
|
||||||
def get_liquidsoap_connection_status(self, current_time):
|
def get_liquidsoap_connection_status(self, current_time):
|
||||||
output = None
|
output = None
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.host, self.port)
|
|
||||||
# update the boot up time of Liquidsoap. Since Liquidsoap is not
|
# update the boot up time of Liquidsoap. Since Liquidsoap is not
|
||||||
# restarting, we are manually adjusting the bootup time variable
|
# restarting, we are manually adjusting the bootup time variable
|
||||||
# so the status msg will get updated.
|
# so the status msg will get updated.
|
||||||
|
@ -312,43 +239,28 @@ class TelnetLiquidsoap:
|
||||||
tn.write(connection_status)
|
tn.write(connection_status)
|
||||||
|
|
||||||
tn.write('exit\n')
|
tn.write('exit\n')
|
||||||
|
|
||||||
output = tn.read_all()
|
output = tn.read_all()
|
||||||
except Exception, e:
|
|
||||||
self.logger.error(str(e))
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
return None
|
return output
|
||||||
|
|
||||||
def update_liquidsoap_stream_format(self, stream_format):
|
def update_liquidsoap_stream_format(self, stream_format):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.host, self.port)
|
|
||||||
command = ('vars.stream_metadata_type %s\n' %
|
command = ('vars.stream_metadata_type %s\n' %
|
||||||
stream_format).encode('utf-8')
|
stream_format).encode('utf-8')
|
||||||
self.logger.info(command)
|
self.logger.info(command)
|
||||||
tn.write(command)
|
tn.write(command)
|
||||||
tn.write('exit\n')
|
tn.write('exit\n')
|
||||||
tn.read_all()
|
tn.read_all()
|
||||||
except Exception, e:
|
|
||||||
self.logger.error("Exception %s", e)
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
def update_liquidsoap_transition_fade(self, fade):
|
def update_liquidsoap_transition_fade(self, fade):
|
||||||
try:
|
with self.telnet_lock:
|
||||||
self.telnet_lock.acquire()
|
tn = self.__connect()
|
||||||
tn = telnetlib.Telnet(self.host, self.port)
|
|
||||||
command = ('vars.default_dj_fade %s\n' % fade).encode('utf-8')
|
command = ('vars.default_dj_fade %s\n' % fade).encode('utf-8')
|
||||||
self.logger.info(command)
|
self.logger.info(command)
|
||||||
tn.write(command)
|
tn.write(command)
|
||||||
tn.write('exit\n')
|
tn.write('exit\n')
|
||||||
tn.read_all()
|
tn.read_all()
|
||||||
except Exception, e:
|
|
||||||
self.logger.error("Exception %s", e)
|
|
||||||
finally:
|
|
||||||
self.telnet_lock.release()
|
|
||||||
|
|
||||||
class DummyTelnetLiquidsoap:
|
class DummyTelnetLiquidsoap:
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue