]> git.0d.be Git - django-panik-nonstop.git/commitdiff
stamina: record played tracks
authorFrédéric Péters <fpeters@0d.be>
Thu, 2 Jul 2020 07:57:43 +0000 (09:57 +0200)
committerFrédéric Péters <fpeters@0d.be>
Thu, 2 Jul 2020 08:10:57 +0000 (10:10 +0200)
nonstop/app_settings.py
nonstop/management/commands/stamina.py

index ac94733360f8beea6d0b12861fb299511cfa9cc1..35f6d402d769e291147441e58aa3d7888f6386e9 100644 (file)
@@ -30,5 +30,9 @@ class AppSettings:
     def PLAYER_ARGS(self):
         return self.get_setting('PLAYER_ARGS', [])
 
+    @property
+    def ON_AIR_SWITCH_URL(self):
+        return self.get_setting('ON_AIR_SWITCH_URL', None)
+
 
 app_settings = AppSettings()
index 62da875566b6927860f9084edd18bfd423357dc6..4095b8d014711b406c9ee03be07fc8748d16f585 100644 (file)
@@ -3,10 +3,12 @@ import datetime
 import random
 import signal
 
+import requests
+
 from django.core.management.base import BaseCommand
 
 from emissions.models import Nonstop
-from nonstop.models import Track, ScheduledDiffusion, RecurringStreamOccurence, RecurringRandomDirectoryOccurence
+from nonstop.models import Track, SomaLogLine, ScheduledDiffusion, RecurringStreamOccurence, RecurringRandomDirectoryOccurence
 from nonstop.app_settings import app_settings
 
 
@@ -94,6 +96,33 @@ class Command(BaseCommand):
 
         return playlist
 
+    def is_nonstop_on_air(self):
+        # check if nonstop system is currently on air
+        if app_settings.ON_AIR_SWITCH_URL is None:
+            return None
+        switch_response = requests.get(app_settings.ON_AIR_SWITCH_URL, timeout=5)
+        if not switch_response.ok:
+            return None
+        try:
+            status = switch_response.json()
+        except ValueError:
+            return None
+        if status.get('active') == 0:
+            return True
+        elif status.get('active') == 1 and status.get('nonstop-via-stud1') == 0:
+            return True
+        elif status.get('active') == 2 and status.get('nonstop-via-stud2') == 1:
+            return True
+        return False
+
+    async def record_nonstop_line(self, track, now):
+        log_line = SomaLogLine()
+        log_line.play_timestamp = now
+        log_line.track = track
+        log_line.filepath = track.nonstopfile_set.first()
+        log_line.on_air = self.is_nonstop_on_air()
+        log_line.save()
+
     async def player_process(self, item, timeout=None):
         if app_settings.DEBUG_WITH_SLEEPS:
             if hasattr(item, 'is_stream') and item.is_stream():
@@ -141,7 +170,12 @@ class Command(BaseCommand):
                 self.current_track_start_datetime = now
                 print(now, track.title, track.duration,
                         '- future tracks:', [x.title for x in self.playlist[self.playhead + 1:self.playhead + 3]])
+                record_task = None
+                if isinstance(track, Track):  # not jingles
+                    record_task = asyncio.create_task(self.record_nonstop_line(track, datetime.datetime.now()))
                 await self.player_process(track)
+                if record_task:
+                    await record_task
                 if self.softstop:
                     # track was left to finish, but now the playlist should stop.
                     break