Files
@ 4b8f8fabeb2f
Branch filter:
Location: light9/src/light9/ascoltami/webapp.py
4b8f8fabeb2f
5.0 KiB
text/x-python
vscode settings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | import asyncio
import json
import logging
import socket
import subprocess
import time
from typing import cast
from rdflib import RDFS, Graph, URIRef
from light9.ascoltami.player import Player
from sse_starlette.sse import EventSourceResponse
from starlette.requests import Request
from starlette.responses import JSONResponse, PlainTextResponse
from light9.namespaces import L9
from light9.showconfig import getSongsFromShow, showUri, songOnDisk
log = logging.getLogger()
_songUris = {} # locationUri : song
def songLocation(graph, songUri):
loc = URIRef("file://%s" % songOnDisk(songUri))
_songUris[loc] = songUri
return loc
def songUri(graph, locationUri):
return _songUris[locationUri]
async def get_config(request: Request) -> JSONResponse:
return JSONResponse(
dict(
host=socket.gethostname(),
show=str(showUri()),
times={
# these are just for the web display. True values are on Player.__init__
'intro': 4,
'post': 0
}))
def playerSongUri(graph, player):
"""or None"""
playingLocation = player.getSong()
if playingLocation:
return songUri(graph, URIRef(playingLocation))
else:
return None
def currentState(graph, player):
if player.isAutostopped():
nextAction = 'finish'
elif player.isPlaying():
nextAction = 'disabled'
else:
nextAction = 'play'
return {
"song": playerSongUri(graph, player),
"started": player.playStartTime,
"duration": player.duration(),
"playing": player.isPlaying(),
"t": player.currentTime(),
"state": player.states(),
"next": nextAction,
}
async def get_time(request: Request) -> JSONResponse:
player = cast(Player, request.app.state.player)
graph = cast(Graph, request.app.state.graph)
return JSONResponse(currentState(graph, player))
async def post_time(request: Request) -> PlainTextResponse:
"""
post a json object with {pause: true} or {resume: true} if you
want those actions. Use {t: <seconds>} to seek, optionally
with a pause/resume command too.
"""
params = await request.json()
player = cast(Player, request.app.state.player)
if params.get('pause', False):
player.pause()
if params.get('resume', False):
player.resume()
if 't' in params:
player.seek(params['t'])
return PlainTextResponse("ok")
async def timeStream(request: Request):
graph = cast(Graph, request.app.state.graph)
player = cast(Player, request.app.state.player)
async def event_generator():
last_sent = None
last_sent_time = 0.0
while True:
now = time.time()
msg = currentState(graph, player)
if msg != last_sent or now > last_sent_time + 2:
event_data = json.dumps(msg)
yield event_data
last_sent = msg
last_sent_time = now
await asyncio.sleep(0.1)
return EventSourceResponse(event_generator())
async def get_songs(request: Request) -> JSONResponse:
graph = cast(Graph, request.app.state.graph)
songs = getSongsFromShow(graph, request.app.state.show)
songs_data = [
{ #
"uri": s,
"path": graph.value(s, L9['songFilename']),
"label": graph.value(s, RDFS.label)
} for s in songs
]
return JSONResponse({"songs": songs_data})
async def post_song(request: Request) -> PlainTextResponse:
"""post a uri of song to switch to (and start playing)"""
graph = cast(Graph, request.app.state.graph)
player = cast(Player, request.app.state.player)
song_uri = URIRef((await request.body()).decode('utf8'))
player.setSong(songLocation(graph, song_uri))
return PlainTextResponse("ok")
async def post_seekPlayOrPause(request: Request) -> PlainTextResponse:
"""curveCalc's ctrl-p or a vidref scrub"""
player = cast(Player, request.app.state.player)
data = await request.json()
if 'scrub' in data:
player.pause()
player.seek(data['scrub'])
return PlainTextResponse("ok")
if 'action' in data:
if data['action'] == 'play':
player.resume()
elif data['action'] == 'pause':
player.pause()
else:
raise NotImplementedError
return PlainTextResponse("ok")
if player.isPlaying():
player.pause()
else:
player.seek(data['t'])
player.resume()
return PlainTextResponse("ok")
async def post_output(request: Request) -> PlainTextResponse:
d = await request.json()
subprocess.check_call(["bin/movesinks", str(d['sink'])])
return PlainTextResponse("ok")
async def post_goButton(request: Request) -> PlainTextResponse:
"""
if music is playing, this silently does nothing.
"""
player = cast(Player, request.app.state.player)
if player.isAutostopped():
player.resume()
elif player.isPlaying():
pass
else:
player.resume()
return PlainTextResponse("ok")
|