add shuffle

This commit is contained in:
iou1name 2020-06-16 09:13:12 -04:00
parent b4882ea4d9
commit d7eb2c0863
2 changed files with 21 additions and 0 deletions

View File

@ -59,6 +59,22 @@ async def select_track(request, ws, data):
await ws.send_json(ret) await ws.send_json(ret)
async def random_track(request, ws, data):
"""Select a random track."""
async with request.app['pool'].acquire() as conn:
track = await conn.fetchrow(
"SELECT * FROM TRACK ORDER BY random() LIMIT 1")
track = dict(track)
fpath = track.pop('filepath')
track.pop('last_modified')
fpath = config.static_prefix + os.path.relpath(fpath, config.music_dir)
fpath = fpath.replace('flac', 'opus')
fpath = parse.quote(fpath)
track['url'] = fpath
ret = {'event': 'track', 'ok': True, 'data': track}
await ws.send_json(ret)
events = {} events = {}
for obj in dir(): for obj in dir():
if type(locals()[obj]) == types.FunctionType: if type(locals()[obj]) == types.FunctionType:

View File

@ -2,6 +2,11 @@ var socket;
function load() { function load() {
socket = init_websocket(); socket = init_websocket();
document.querySelector('#player').addEventListener('ended', function() {
if (document.querySelector('#shuffle').checked) {
socket.send_event('random_track', {});
}
});
} }
/* Websocket setup */ /* Websocket setup */