From d7eb2c08635908b4dcb3424610028f2ce11c709b Mon Sep 17 00:00:00 2001 From: iou1name Date: Tue, 16 Jun 2020 09:13:12 -0400 Subject: [PATCH] add shuffle --- events.py | 16 ++++++++++++++++ static/scorch.js | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/events.py b/events.py index bcc5d84..ec963f7 100644 --- a/events.py +++ b/events.py @@ -59,6 +59,22 @@ async def select_track(request, ws, data): 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 = {} for obj in dir(): if type(locals()[obj]) == types.FunctionType: diff --git a/static/scorch.js b/static/scorch.js index 323c5ed..d84ddc6 100644 --- a/static/scorch.js +++ b/static/scorch.js @@ -2,6 +2,11 @@ var socket; function load() { socket = init_websocket(); + document.querySelector('#player').addEventListener('ended', function() { + if (document.querySelector('#shuffle').checked) { + socket.send_event('random_track', {}); + } + }); } /* Websocket setup */