Files
@ 7ed414bdaab9
Branch filter:
Location: light9/light9/ascoltami/webapp.py
7ed414bdaab9
6.1 KiB
text/x-python
wip porting asco to TS and not-jquery
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | import json, socket, subprocess, os, logging, time
from cyclone import template
from rdflib import URIRef
import cyclone.web, cyclone.websocket
from cycloneerr import PrettyErrorHandler
from light9.metrics import metricsRoute
from light9.namespaces import L9
from light9.showconfig import getSongsFromShow, songOnDisk
from twisted.internet import reactor
_songUris = {} # locationUri : song
log = logging.getLogger()
loader = template.Loader(os.path.dirname(__file__))
def songLocation(graph, songUri):
loc = URIRef("file://%s" % songOnDisk(songUri))
_songUris[loc] = songUri
return loc
def songUri(graph, locationUri):
return _songUris[locationUri]
class root(PrettyErrorHandler, cyclone.web.RequestHandler):
def get(self):
self.set_header("Content-Type", "text/html")
self.write(
loader.load('index.html').generate())
class config(cyclone.web.RequestHandler):
def get(self):
self.set_header("Content-Type", "application/json")
self.write(json.dumps(dict(host=socket.gethostname(),
times={
'intro': 4,
'post': 4
})))
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,
}
class timeResource(PrettyErrorHandler, cyclone.web.RequestHandler):
def get(self):
player = self.settings.app.player
graph = self.settings.app.graph
self.set_header("Content-Type", "application/json")
self.write(json.dumps(currentState(graph, player)))
def post(self):
"""
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 = json.loads(self.request.body)
player = self.settings.app.player
if params.get('pause', False):
player.pause()
if params.get('resume', False):
player.resume()
if 't' in params:
player.seek(params['t'])
self.set_header("Content-Type", "text/plain")
self.write("ok")
class timeStreamResource(cyclone.websocket.WebSocketHandler):
def connectionMade(self, *args, **kwargs) -> None:
self.lastSent = None
self.lastSentTime = 0.
self.loop()
def loop(self):
now = time.time()
msg = currentState(self.settings.app.graph, self.settings.app.player)
if msg != self.lastSent or now > self.lastSentTime + 2:
self.sendMessage(json.dumps(msg))
self.lastSent = msg
self.lastSentTime = now
if self.transport.connected:
reactor.callLater(.2, self.loop)
def connectionLost(self, reason):
log.info("bye ws client %r: %s", self, reason)
class songs(PrettyErrorHandler, cyclone.web.RequestHandler):
def get(self):
graph = self.settings.app.graph
songs = getSongsFromShow(graph, self.settings.app.show)
self.set_header("Content-Type", "application/json")
self.write(
json.dumps({
"songs": [{
"uri": s,
"path": graph.value(s, L9['showPath']),
"label": graph.label(s)
} for s in songs]
}))
class songResource(PrettyErrorHandler, cyclone.web.RequestHandler):
def post(self):
"""post a uri of song to switch to (and start playing)"""
graph = self.settings.app.graph
self.settings.app.player.setSong(
songLocation(graph, URIRef(self.request.body.decode('utf8'))))
self.set_header("Content-Type", "text/plain")
self.write("ok")
class seekPlayOrPause(PrettyErrorHandler, cyclone.web.RequestHandler):
"""curveCalc's ctrl-p or a vidref scrub"""
def post(self):
player = self.settings.app.player
data = json.loads(self.request.body)
if 'scrub' in data:
player.pause()
player.seek(data['scrub'])
return
if 'action' in data:
if data['action'] == 'play':
player.resume()
elif data['action'] == 'pause':
player.pause()
else:
raise NotImplementedError
return
if player.isPlaying():
player.pause()
else:
player.seek(data['t'])
player.resume()
class output(PrettyErrorHandler, cyclone.web.RequestHandler):
def post(self):
d = json.loads(self.request.body)
subprocess.check_call(["bin/movesinks", str(d['sink'])])
class goButton(PrettyErrorHandler, cyclone.web.RequestHandler):
def post(self):
"""
if music is playing, this silently does nothing.
"""
player = self.settings.app.player
if player.isAutostopped():
player.resume()
elif player.isPlaying():
pass
else:
player.resume()
self.set_header("Content-Type", "text/plain")
self.write("ok")
def makeWebApp(app):
return cyclone.web.Application(handlers=[
(r"/", root),
(r"/config", config),
(r"/time", timeResource),
(r"/time/stream", timeStreamResource),
(r"/song", songResource),
(r"/songs", songs),
(r"/seekPlayOrPause", seekPlayOrPause),
(r"/output", output),
(r"/go", goButton),
metricsRoute(),
],
app=app)
|