Commit c3f21f38 authored by fred's avatar fred

stamina: record played tracks

parent b7771b86
......@@ -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()
......@@ -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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment