diff --git a/aircox_liquidsoap/management/commands/liquidsoap_files.py b/aircox_liquidsoap/management/commands/liquidsoap_files.py index 2bc1f7f..3fba17e 100644 --- a/aircox_liquidsoap/management/commands/liquidsoap_files.py +++ b/aircox_liquidsoap/management/commands/liquidsoap_files.py @@ -90,30 +90,27 @@ class Command (BaseCommand): file.write(data) @staticmethod - def __render_stream_in_radio (stream): - if stream.time_start and stream.time_end: - data = '({{{}-{}}}, {})'.format( - stream.time_start.strftime('%Hh%M'), - stream.time_end.strftime('%Hh%M'), - stream.program.get_slug_name() - ) - else: - data = stream.program.get_slug_name() + def liquid_stream (stream): + def to_seconds (time): + return 3600 * time.hour + 60 * time.minute + time.second - if stream.delay: - data = 'delay({}., {})'.format( - 3600*stream.delay.hour+60*stream.delay.minute+stream.delay.second, - data - ) - return data + return { + 'name': stream.program.get_slug_name(), + 'begin': stream.begin.strftime('%Hh%M') if stream.begin else None, + 'end': stream.end.strftime('%Hh%M') if stream.end else None, + 'delay': to_seconds(stream.delay) if stream.delay else None, + 'file': '{}/stream_{}.m3u'.format( + settings.AIRCOX_LIQUIDSOAP_MEDIA, + stream.pk, + ) + } def get_config (self, output = None): - streams = models.Stream.objects.filter(program__active = True) - for stream in streams: - stream.render_in_radio = self.__render_stream_in_radio(stream) - context = { - 'streams': streams, + 'streams': [ + self.liquid_stream(stream) + for stream in models.Stream.objects.filter(program__active = True) + ], 'settings': settings, } diff --git a/aircox_liquidsoap/settings.py b/aircox_liquidsoap/settings.py index 180c4fc..bfb881c 100644 --- a/aircox_liquidsoap/settings.py +++ b/aircox_liquidsoap/settings.py @@ -5,11 +5,11 @@ def ensure (key, default): globals()[key] = getattr(settings, key, default) +ensure('AIRCOX_LIQUIDSOAP_SOCKET', '/tmp/liquidsoap.sock') + # dict of values to set (do not forget to escape chars) ensure('AIRCOX_LIQUIDSOAP_SET', { 'log.file.path': '"/tmp/liquidsoap.log"', - 'server.socket': 'true', - 'server.socket.path': '"/tmp/liquidsoap.sock"' }) # security source: used when no source are available diff --git a/aircox_liquidsoap/utils.py b/aircox_liquidsoap/utils.py new file mode 100644 index 0000000..cd75ad8 --- /dev/null +++ b/aircox_liquidsoap/utils.py @@ -0,0 +1,34 @@ +import socket +import aircox_liquidsoap.settings as settings + +class Controller: + socket = None + available = False + + def __init__ (self): + self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + + def open (self): + if self.available: + return + + address = settings.AIRCOX_LIQUIDSOAP_SOCKET + try: + self.socket.connect(address) + self.available = True + except: + print('can not connect to liquidsoap socket {}'.format(address)) + self.available = False + return -1 + + def send (self, data): + if self.open(): + return -1 + data = bytes(data + '\n', encoding='utf-8') + self.socket.sendall(data) + return self.socket.recv(10240).decode('utf-8') + + def get (self, stream = None): + print(self.send('station.get')) + + diff --git a/aircox_programs/admin.py b/aircox_programs/admin.py index 0ae3d86..e4b15c1 100755 --- a/aircox_programs/admin.py +++ b/aircox_programs/admin.py @@ -24,7 +24,7 @@ class ScheduleInline (admin.TabularInline): extra = 1 class StreamInline (admin.TabularInline): - fields = ['delay', 'time_start', 'time_end'] + fields = ['delay', 'begin', 'end'] model = Stream extra = 1 @@ -64,7 +64,7 @@ class SoundAdmin (NameableAdmin): @admin.register(Stream) class StreamAdmin (admin.ModelAdmin): - list_display = ('id', 'program', 'delay', 'time_start', 'time_end') + list_display = ('id', 'program', 'delay', 'begin', 'end') @admin.register(Program) diff --git a/aircox_programs/models.py b/aircox_programs/models.py index 7bdd727..b033fe6 100755 --- a/aircox_programs/models.py +++ b/aircox_programs/models.py @@ -201,13 +201,13 @@ class Stream (models.Model): blank = True, null = True, help_text = _('plays this playlist at least every delay') ) - time_start = models.TimeField( - _('start'), + begin = models.TimeField( + _('begin'), blank = True, null = True, help_text = _('used to define a time range this stream is' 'played') ) - time_end = models.TimeField( + end = models.TimeField( _('end'), blank = True, null = True, help_text = _('used to define a time range this stream is'