cdosea-play/cdoseaplay/utils.py

74 lines
1.8 KiB
Python
Raw Normal View History

2017-08-30 14:07:50 +00:00
import os
import sys
from glob import glob
import datetime
import time
import string
import random
import ox
from . import mpv
import logging
logger = logging.getLogger('cdosea')
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'
2017-10-04 11:02:13 +00:00
player.loop_file = 'no'
2017-08-30 14:07:50 +00:00
return player
def update_playlist(playlist, prefix, position=None):
files = []
videos = {}
if not prefix.endswith('/'):
prefix += '/'
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 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)