forked from rc/aircox
		
	monitor: playlist update and fetch to/from file only if changed; fix bug logs filter in on_air
This commit is contained in:
		@ -180,7 +180,7 @@ class Station(programs.Nameable):
 | 
			
		||||
        # each to put them too there
 | 
			
		||||
        items = []
 | 
			
		||||
        diff_ = None
 | 
			
		||||
        for diff in diffs:
 | 
			
		||||
        for diff in diffs.order_by('-start'):
 | 
			
		||||
            logs_ = \
 | 
			
		||||
                logs.filter(date__gt = diff.end, date__lt = diff_.start) \
 | 
			
		||||
                    if diff_ else \
 | 
			
		||||
@ -194,7 +194,6 @@ class Station(programs.Nameable):
 | 
			
		||||
        if diff_:
 | 
			
		||||
            if count and len(items) >= count:
 | 
			
		||||
                return items[:count]
 | 
			
		||||
 | 
			
		||||
            logs_ = logs.filter(date__lt = diff_.end)
 | 
			
		||||
        else:
 | 
			
		||||
            logs_ = logs.all()
 | 
			
		||||
@ -211,6 +210,7 @@ class Station(programs.Nameable):
 | 
			
		||||
        * date: only for what happened on this date;
 | 
			
		||||
        * count: number of items to retrieve if not zero;
 | 
			
		||||
 | 
			
		||||
        If date is not specified, count MUST be set to a non-zero value.
 | 
			
		||||
        Be careful with what you which for: the result is a plain list.
 | 
			
		||||
 | 
			
		||||
        The list contains:
 | 
			
		||||
@ -220,6 +220,9 @@ class Station(programs.Nameable):
 | 
			
		||||
        # FIXME: as an iterator?
 | 
			
		||||
        # TODO argument to get sound instead of tracks
 | 
			
		||||
        # TODO #Station
 | 
			
		||||
        if not date and not count:
 | 
			
		||||
            raise ValueError('at least one argument must be set')
 | 
			
		||||
 | 
			
		||||
        if date and date > datetime.date.today():
 | 
			
		||||
            return []
 | 
			
		||||
 | 
			
		||||
@ -234,8 +237,7 @@ class Station(programs.Nameable):
 | 
			
		||||
            diffs = programs.Diffusion.objects
 | 
			
		||||
 | 
			
		||||
        diffs = diffs.filter(type = programs.Diffusion.Type.normal) \
 | 
			
		||||
                     .filter(start__lte = tz.now()) \
 | 
			
		||||
                     .order_by('-start')
 | 
			
		||||
                     .filter(start__lte = tz.now())
 | 
			
		||||
        return self.__mix_logs_and_diff(diffs, logs, count)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -345,33 +347,6 @@ class Source(programs.Nameable):
 | 
			
		||||
        if fetch:
 | 
			
		||||
            self.controller.fetch()
 | 
			
		||||
 | 
			
		||||
    def load_playlist(self, diffusion = None, program = None):
 | 
			
		||||
        """
 | 
			
		||||
        Load a playlist to the controller. If diffusion or program is
 | 
			
		||||
        given use it, otherwise, try with self.program if exists, or
 | 
			
		||||
        (if URI, self.value).
 | 
			
		||||
 | 
			
		||||
        A playlist from a program uses all archives available for the
 | 
			
		||||
        program.
 | 
			
		||||
        """
 | 
			
		||||
        if diffusion:
 | 
			
		||||
            self.controller.playlist = diffusion.playlist
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        program = program or self.program
 | 
			
		||||
        if program:
 | 
			
		||||
            self.controller.playlist = [ sound.path for sound in
 | 
			
		||||
                programs.Sound.objects.filter(
 | 
			
		||||
                    type = programs.Sound.Type.archive,
 | 
			
		||||
                    program = program,
 | 
			
		||||
                )
 | 
			
		||||
            ]
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if self.type == self.Type.file and self.value:
 | 
			
		||||
            self.controller.playlist = [ self.value ]
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
    def save(self, *args, **kwargs):
 | 
			
		||||
        if self.type in (self.Type.file, self.Type.fallback) and \
 | 
			
		||||
                not self.url:
 | 
			
		||||
@ -462,7 +437,7 @@ class Log(programs.Related):
 | 
			
		||||
    )
 | 
			
		||||
    date = models.DateTimeField(
 | 
			
		||||
        _('date'),
 | 
			
		||||
        auto_now_add=True,
 | 
			
		||||
        default=tz.now,
 | 
			
		||||
    )
 | 
			
		||||
    comment = models.CharField(
 | 
			
		||||
        _('comment'),
 | 
			
		||||
 | 
			
		||||
@ -44,8 +44,6 @@ class Monitor:
 | 
			
		||||
        """
 | 
			
		||||
        if not self.controller:
 | 
			
		||||
            self.station.prepare()
 | 
			
		||||
            for stream in self.station.stream_sources:
 | 
			
		||||
                stream.load_playlist()
 | 
			
		||||
        self.controller = self.station.controller
 | 
			
		||||
 | 
			
		||||
        if not self.controller.ready():
 | 
			
		||||
@ -78,12 +76,13 @@ class Monitor:
 | 
			
		||||
                    .filter(station = self.station).order_by('date').last()
 | 
			
		||||
 | 
			
		||||
        # only streamed
 | 
			
		||||
        if log and not log.related.diffusion:
 | 
			
		||||
        if log and (log.related and not log.related.diffusion):
 | 
			
		||||
            self.trace_sound_tracks(log)
 | 
			
		||||
 | 
			
		||||
        # TODO: expiration
 | 
			
		||||
        if log and (log.source == current_source.id_ and \
 | 
			
		||||
                    log.related.path == current_sound):
 | 
			
		||||
                log.related and
 | 
			
		||||
                log.related.path == current_sound):
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        sound = programs.Sound.objects.filter(path = current_sound)
 | 
			
		||||
@ -133,9 +132,8 @@ class Monitor:
 | 
			
		||||
 | 
			
		||||
        for source in self.station.stream_sources:
 | 
			
		||||
            playlist = [ sound.path for sound in
 | 
			
		||||
                            source.program.sound_set.all().order_by('path') ]
 | 
			
		||||
            if playlist != source.controller.playlist:
 | 
			
		||||
                source.controller.playlist = playlist
 | 
			
		||||
                            source.program.sound_set.all() ]
 | 
			
		||||
            source.controller.playlist = playlist
 | 
			
		||||
 | 
			
		||||
    def trace_canceled(self):
 | 
			
		||||
        """
 | 
			
		||||
 | 
			
		||||
@ -5,6 +5,8 @@ import atexit
 | 
			
		||||
 | 
			
		||||
from django.template.loader import render_to_string
 | 
			
		||||
 | 
			
		||||
import aircox.programs.models as programs
 | 
			
		||||
 | 
			
		||||
class Plugins(type):
 | 
			
		||||
    registry = {}
 | 
			
		||||
 | 
			
		||||
@ -192,12 +194,15 @@ class SourceController:
 | 
			
		||||
        """
 | 
			
		||||
        Current playlist on the Source, list of paths to play
 | 
			
		||||
        """
 | 
			
		||||
        self.fetch()
 | 
			
		||||
        return self.__playlist
 | 
			
		||||
 | 
			
		||||
    @playlist.setter
 | 
			
		||||
    def playlist(self, value):
 | 
			
		||||
        self.__playlist = value
 | 
			
		||||
        self.push()
 | 
			
		||||
        value = sorted(value)
 | 
			
		||||
        if value != self.__playlist:
 | 
			
		||||
            self.__playlist = value
 | 
			
		||||
            self.push()
 | 
			
		||||
 | 
			
		||||
    def __init__(self, **kwargs):
 | 
			
		||||
        self.__dict__.update(kwargs)
 | 
			
		||||
@ -205,6 +210,10 @@ class SourceController:
 | 
			
		||||
        if not self.path:
 | 
			
		||||
            self.path = os.path.join(self.source.station.path,
 | 
			
		||||
                                     self.source.slug + '.m3u')
 | 
			
		||||
            self.from_file()
 | 
			
		||||
 | 
			
		||||
        if not self.__playlist:
 | 
			
		||||
            self.from_db()
 | 
			
		||||
 | 
			
		||||
    def skip(self):
 | 
			
		||||
        """
 | 
			
		||||
@ -225,7 +234,7 @@ class SourceController:
 | 
			
		||||
        """
 | 
			
		||||
        os.makedirs(os.path.dirname(self.path), exist_ok = True)
 | 
			
		||||
        with open(self.path, 'w') as file:
 | 
			
		||||
            file.write('\n'.join(self.playlist or []))
 | 
			
		||||
            file.write('\n'.join(self.__playlist or []))
 | 
			
		||||
 | 
			
		||||
    def activate(self, value = True):
 | 
			
		||||
        """
 | 
			
		||||
@ -234,6 +243,46 @@ class SourceController:
 | 
			
		||||
        """
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    def from_db(self, diffusion = None, program = None):
 | 
			
		||||
        """
 | 
			
		||||
        Load a playlist to the controller from the database. If diffusion or
 | 
			
		||||
        program is given use it, otherwise, try with self.program if exists, or
 | 
			
		||||
        (if URI, self.url).
 | 
			
		||||
 | 
			
		||||
        A playlist from a program uses all its available archives.
 | 
			
		||||
        """
 | 
			
		||||
        if diffusion:
 | 
			
		||||
            self.playlist = diffusion.playlist
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        source = self.source
 | 
			
		||||
        program = program or source.program
 | 
			
		||||
        if program:
 | 
			
		||||
            self.playlist = [ sound.path for sound in
 | 
			
		||||
                programs.Sound.objects.filter(
 | 
			
		||||
                    type = programs.Sound.Type.archive,
 | 
			
		||||
                    program = program,
 | 
			
		||||
                )
 | 
			
		||||
            ]
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        if source.type == source.Type.file and source.url:
 | 
			
		||||
            self.playlist = [ self.url ]
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
    def from_file(self, path = None):
 | 
			
		||||
        """
 | 
			
		||||
        Load a playlist from the given file (if not, use the
 | 
			
		||||
        controller's one
 | 
			
		||||
        """
 | 
			
		||||
        path = path or self.path
 | 
			
		||||
        if not os.path.exists(path):
 | 
			
		||||
            return
 | 
			
		||||
 | 
			
		||||
        with open(path, 'r') as file:
 | 
			
		||||
            self.__playlist = file.read()
 | 
			
		||||
            self.__playlist = self.__playlist.split('\n') \
 | 
			
		||||
                                if self.__playlist else []
 | 
			
		||||
 | 
			
		||||
class Monitor:
 | 
			
		||||
    station = None
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user