85 lines
2.2 KiB
Python
85 lines
2.2 KiB
Python
import os
|
|
import sys
|
|
from glob import glob
|
|
import datetime
|
|
import time
|
|
import string
|
|
import random
|
|
import subprocess
|
|
|
|
import ox
|
|
|
|
from . import mpv
|
|
|
|
import logging
|
|
|
|
|
|
logger = logging.getLogger('cdosea')
|
|
STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'static')
|
|
|
|
|
|
def mpv_log(loglevel, component, message):
|
|
logger.info('[{}] {}: {}'.format(loglevel, component, message))
|
|
|
|
|
|
def get_player(fullscreen=True):
|
|
# 42 max
|
|
player = mpv.MPV(
|
|
log_handler=mpv_log, input_default_bindings=True,
|
|
input_vo_keyboard=True, sub_text_font_size=28, sub_text_font='Menlo'
|
|
)
|
|
#player.observe_property('time-pos', lambda pos: print('Now playing at {:.2f}s'.format(pos)))
|
|
player.fullscreen = fullscreen
|
|
player.loop = 'inf'
|
|
player.loop_file = 'no'
|
|
return player
|
|
|
|
|
|
def update_playlist(playlist, prefix, position=None, shift=True):
|
|
|
|
files = []
|
|
videos = {}
|
|
if not prefix.endswith('/'):
|
|
prefix += '/'
|
|
|
|
if not shift:
|
|
files.append(os.path.join(STATIC_ROOT, 'title.png'))
|
|
for letter in string.ascii_uppercase:
|
|
videos[letter] = glob('%s%s*.mp4' % (prefix, letter.lower()))
|
|
random.shuffle(videos[letter])
|
|
|
|
for i in range(10):
|
|
for letter in string.ascii_uppercase:
|
|
files.append(videos[letter][i])
|
|
|
|
if shift:
|
|
if position is None:
|
|
today = datetime.date.today()
|
|
seconds_since_midnight = time.time() - time.mktime(today.timetuple())
|
|
offset = 0
|
|
position = 0
|
|
|
|
while offset < seconds_since_midnight:
|
|
f = files.pop(0)
|
|
try:
|
|
offset += ox.avinfo(f)['duration']
|
|
files.append(f)
|
|
except:
|
|
logger.debug('WTF', exc_info=True)
|
|
pass
|
|
position += 1
|
|
else:
|
|
pos = position
|
|
while pos:
|
|
f = files.pop(0)
|
|
files.append(f)
|
|
pos -= 1
|
|
|
|
with open(playlist, 'w') as f:
|
|
f.write('\n'.join(files))
|
|
f.write('\n')
|
|
return position, len(files)
|
|
|
|
def trigger_lights(path):
|
|
cmd = ['python3', '-m', 'cdoseaplay.lights', path]
|
|
subprocess.Popen(cmd)
|