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
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():
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