Commit 20fc4ca6 authored by fred's avatar fred

stamina: play for real

parent e5d72e6b
......@@ -18,5 +18,17 @@ class AppSettings:
# relative to ..._BASE_PATH
return self.get_setting('JINGLES_PREFIX', 'SPOTS')
@property
def DEBUG_WITH_SLEEPS(self):
return self.get_setting('DEBUG_WITH_SLEEPS', False)
@property
def PLAYER_COMMAND(self):
return self.get_setting('PLAYER_COMMAND', 'mpv')
@property
def PLAYER_ARGS(self):
return self.get_setting('PLAYER_ARGS', [])
app_settings = AppSettings()
......@@ -7,6 +7,7 @@ from django.core.management.base import BaseCommand
from emissions.models import Nonstop
from nonstop.models import Track, ScheduledDiffusion, RecurringStreamOccurence, RecurringRandomDirectoryOccurence
from nonstop.app_settings import app_settings
class Command(BaseCommand):
......@@ -82,11 +83,29 @@ class Command(BaseCommand):
return playlist
async def player_process(self, cmd, timeout=None):
self.player = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
async def player_process(self, item, timeout=None):
if app_settings.DEBUG_WITH_SLEEPS:
if hasattr(item, 'is_stream') and item.is_stream():
cmd = 'sleep 86400 # %s' % item.stream.url
else:
cmd = 'sleep %s # %s' % (item.duration.total_seconds(), item.file_path())
else:
cmd = [app_settings.PLAYER_COMMAND] + app_settings.PLAYER_ARGS
if hasattr(item, 'is_stream') and item.is_stream():
cmd.append(item.stream.url)
else:
cmd.append(item.file_path())
print('cmd:', cmd)
if isinstance(cmd, str):
self.player = await asyncio.create_subprocess_shell(
cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
else:
self.player = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
if timeout is None:
await self.player.communicate()
else:
......@@ -110,33 +129,24 @@ 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]])
cmd = 'sleep %s # %s' % (track.duration.seconds, track.title)
await self.player_process(cmd)
await self.player_process(track)
# TODO: detect "soft spot", to switch to another nonstop
# tranche
self.playhead += 1
elif slot.is_stream():
print(now, 'playing stream', slot.stream)
if slot.get_jingle_filepath():
cmd = 'sleep 15 # jingle %s' % slot.get_jingle_filepath()
await self.player_process(cmd, timeout=60)
cmd = 'sleep 86400 # stream' # will never stop by itself
if slot.jingle_id:
await self.player_process(slot.jingle, timeout=60)
print('timeout at', (slot.end_datetime - now).total_seconds())
await self.player_process(cmd, timeout=(slot.end_datetime - now).total_seconds())
await self.player_process(slot, timeout=(slot.end_datetime - now).total_seconds())
else:
if hasattr(slot, 'episode'):
print(now, 'playing sound', slot.episode)
else:
print(now, 'playing random')
if slot.get_jingle_filepath():
cmd = 'sleep 15 # jingle %s' % slot.get_jingle_filepath()
await self.player_process(cmd, timeout=60)
filepath = slot.get_sound_filepath()
if hasattr(slot, 'soundfile'):
cmd = 'sleep %s # %s' % (slot.soundfile.duration, filepath)
else:
cmd = 'sleep %s # %s' % (45 * 60, filepath)
await self.player_process(cmd)
if slot.jingle_id:
await self.player_process(slot.jingle, timeout=60)
await self.player_process(slot)
def recompute_playlist(self):
current_track = self.playlist[self.playhead]
......
......@@ -278,7 +278,7 @@ class ScheduledDiffusion(models.Model):
def get_jingle_filepath(self):
return self.jingle.get_local_filepath() if self.jingle_id else None
def get_sound_filepath(self):
def file_path(self):
# TODO, copy and stuff
return self.soundfile.file.path
......@@ -355,7 +355,7 @@ class RecurringRandomDirectoryOccurence(models.Model):
def get_jingle_filepath(self):
return self.diffusion.jingle.get_local_filepath() if self.diffusion.jingle_id else None
def get_sound_filepath(self):
def file_path(self):
directory = self.diffusion.directory
return os.path.join(directory, random.choice(os.listdir(directory)))
......
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