Files
@ 440d116ba5df
Branch filter:
Location: light9/light9/ascoltami/webapp.py
440d116ba5df
3.3 KiB
text/x-python
asco: add switcher to pick the pulseaudio sink
Ignore-this: 57af0bce1573ecefc1536fe98c301a8f
Ignore-this: 57af0bce1573ecefc1536fe98c301a8f
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 | import web, json, socket, subprocess
from twisted.python.util import sibpath
from light9.namespaces import L9
from light9.showconfig import getSongsFromShow, songOnDisk
from rdflib import URIRef
from web.contrib.template import render_genshi
render = render_genshi([sibpath(__file__, ".")])
app = None
_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]
class root(object):
def GET(self):
web.header("Content-type", "application/xhtml+xml")
# todo: use a template; embed the show name and the intro/post
# times into the page
return render.index(host=socket.gethostname())
class timeResource(object):
def GET(self):
player = app.player
graph = app.graph
playingLocation = player.getSong()
if playingLocation:
song = songUri(graph, URIRef(playingLocation))
else:
song = None
web.header("content-type", "application/json")
return json.dumps({
"song" : song,
"started" : player.playStartTime,
"duration" : player.duration(),
"playing" : player.isPlaying(),
"t" : player.currentTime(),
"state" : player.states(),
})
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(web.data())
player = app.player
if params.get('pause', False):
player.pause()
if params.get('resume', False):
player.resume()
if 't' in params:
player.seek(params['t'])
web.header("content-type", "text/plain")
return "ok"
class songs(object):
def GET(self):
graph = app.graph
songs = getSongsFromShow(graph, app.show)
web.header("Content-type", "application/json")
return json.dumps({"songs" : [
{"uri" : s,
"path" : graph.value(s, L9['showPath']),
"label" : graph.label(s)} for s in songs]})
class songResource(object):
def POST(self):
"""post a uri of song to switch to (and start playing)"""
graph = app.graph
app.player.setSong(songLocation(graph, URIRef(web.data())))
web.header("content-type", "text/plain")
return "ok"
class seekPlayOrPause(object):
def POST(self):
player = app.player
data = json.loads(web.data())
if player.isPlaying():
player.pause()
else:
player.seek(data['t'])
player.resume()
class output(object):
def POST(self):
d = json.loads(web.data())
subprocess.check_call(["bin/movesinks", str(d['sink'])])
def makeWebApp(theApp):
global app
app = theApp
urls = (r"/", "root",
r"/time", "timeResource",
r"/song", "songResource",
r"/songs", "songs",
r"/seekPlayOrPause", "seekPlayOrPause",
r"/output", "output",
)
return web.application(urls, globals(), autoreload=False)
|