forked from rc/aircox
		
	fix some issues, make the liquidsoap monitor working
This commit is contained in:
		@ -1,10 +1,7 @@
 | 
			
		||||
"""
 | 
			
		||||
Control Liquidsoap
 | 
			
		||||
"""
 | 
			
		||||
import os
 | 
			
		||||
import re
 | 
			
		||||
import datetime
 | 
			
		||||
import collections
 | 
			
		||||
import time
 | 
			
		||||
from argparse import RawTextHelpFormatter
 | 
			
		||||
 | 
			
		||||
from django.core.management.base import BaseCommand, CommandError
 | 
			
		||||
@ -14,74 +11,6 @@ import aircox_liquidsoap.settings as settings
 | 
			
		||||
import aircox_liquidsoap.utils as utils
 | 
			
		||||
import aircox_programs.models as models
 | 
			
		||||
 | 
			
		||||
class DiffusionInfo:
 | 
			
		||||
    date = None
 | 
			
		||||
    original = None
 | 
			
		||||
    sounds = None
 | 
			
		||||
    duration = 0
 | 
			
		||||
 | 
			
		||||
    def __init__ (self, diffusion):
 | 
			
		||||
        episode = diffusion.episode
 | 
			
		||||
        self.original = diffusion
 | 
			
		||||
        self.sounds = [ sound for sound in episode.sounds
 | 
			
		||||
                            if sound.type = models.Sound.Type['archive'] ]
 | 
			
		||||
        self.sounds.sort(key = 'path')
 | 
			
		||||
        self.date = diffusion.date
 | 
			
		||||
        self.duration = episode.get_duration()
 | 
			
		||||
        self.end = self.date + tz.datetime.timedelta(seconds = self.duration)
 | 
			
		||||
 | 
			
		||||
    def __eq___ (self, info):
 | 
			
		||||
        return self.original.id == info.original.id
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ControllerMonitor:
 | 
			
		||||
    current = None
 | 
			
		||||
    queue = None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def get_next (self, controller):
 | 
			
		||||
        upcoming = models.Diffusion.get_next(
 | 
			
		||||
            station = controller.station,
 | 
			
		||||
            # diffusion__episode__not blank
 | 
			
		||||
            # diffusion__episode__sounds not blank
 | 
			
		||||
        )
 | 
			
		||||
        return Monitor.Info(upcoming[0]) if upcoming else None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def playlist (self, controller):
 | 
			
		||||
        dealer = controller.dealer
 | 
			
		||||
        on_air = dealer.current_sound
 | 
			
		||||
        playlist = dealer.playlist
 | 
			
		||||
 | 
			
		||||
        next = self.queue[0]
 | 
			
		||||
 | 
			
		||||
        # last track: time to reload playlist
 | 
			
		||||
        if on_air == playlist[-1] or on_air not in playlist:
 | 
			
		||||
            dealer.playlist = [sound.path for sound in next.sounds]
 | 
			
		||||
            dealer.on = False
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def current (self, controller):
 | 
			
		||||
        # time to switch...
 | 
			
		||||
        if on_air not in self.current.sounds:
 | 
			
		||||
            self.current = self.queue.popleft()
 | 
			
		||||
 | 
			
		||||
        if self.current.date <= tz.datetime.now() and not dealer.on:
 | 
			
		||||
            dealer.on = True
 | 
			
		||||
            print('start ', self.current.original)
 | 
			
		||||
 | 
			
		||||
        # HERE
 | 
			
		||||
 | 
			
		||||
        upcoming = self.get_next(controller)
 | 
			
		||||
 | 
			
		||||
        if upcoming.date <= tz.datetime.now() and not self.current:
 | 
			
		||||
            self.current = upcoming
 | 
			
		||||
 | 
			
		||||
        if not self.upcoming or upcoming != self.upcoming:
 | 
			
		||||
            dealer.playlist = [sound.path for sound in upcomming.sounds]
 | 
			
		||||
            dealer.on = False
 | 
			
		||||
            self.upcoming = upcoming
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Command (BaseCommand):
 | 
			
		||||
    help= __doc__
 | 
			
		||||
@ -98,24 +27,27 @@ class Command (BaseCommand):
 | 
			
		||||
            help='Runs in monitor mode'
 | 
			
		||||
        )
 | 
			
		||||
        parser.add_argument(
 | 
			
		||||
            '-s', '--sleep', type=int,
 | 
			
		||||
            default=1,
 | 
			
		||||
            help='Time to sleep before update'
 | 
			
		||||
            '-d', '--delay', type=int,
 | 
			
		||||
            default=1000,
 | 
			
		||||
            help='Time to sleep in milliseconds before update on monitor'
 | 
			
		||||
        )
 | 
			
		||||
        # start and run liquidsoap
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def handle (self, *args, **options):
 | 
			
		||||
        connector = utils.Connector()
 | 
			
		||||
        self.monitor = utils.Monitor()
 | 
			
		||||
        self.monitor = utils.Monitor(connector)
 | 
			
		||||
        self.monitor.update()
 | 
			
		||||
 | 
			
		||||
        if options.get('on_air'):
 | 
			
		||||
            for id, controller in self.monitor.controller.items():
 | 
			
		||||
                print(id, controller.master.current_sound())
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        if options.get('monitor'):
 | 
			
		||||
            sleep = 
 | 
			
		||||
            delay = options.get('delay') / 1000
 | 
			
		||||
            while True:
 | 
			
		||||
                for controller in self.monitor.controllers.values():
 | 
			
		||||
                    controller.dealer.monitor()
 | 
			
		||||
                time.sleep(delay)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user