Files
@ 94039df5cdd9
Branch filter:
Location: light9/light9/ascoltami/webapp.py
94039df5cdd9
3.0 KiB
text/x-python
create Playlist class which is now used in wavecurve, musicPad, and ascoltami2.
Ignore-this: da54d0239906e81ad5d95741d22f5975
Several other refactorings. Also, totally untested.
Ignore-this: da54d0239906e81ad5d95741d22f5975
Several other refactorings. Also, totally untested.
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 | import web, jsonlib
from twisted.python.util import sibpath
from light9.namespaces import L9
from light9.showconfig import getSongsFromShow
from rdflib import URIRef
app = None
def songLocation(graph, songUri):
loc = graph.value(songUri, L9['showPath'])
if loc is None:
raise ValueError("no showPath for %r" % songUri)
return loc
def songUri(graph, locationUri):
try:
return graph.subjects(L9['showPath'], locationUri).next()
except StopIteration:
raise ValueError("no song has :showPath of %r" % 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 open(sibpath(__file__, "index.html")).read()
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
return jsonlib.write({
"song" : song,
"started" : player.playStartTime,
"duration" : player.duration(),
"playing" : player.isPlaying(),
"t" : player.currentTime()})
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 = jsonlib.read(web.data(), use_float=True)
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'])
return "ok"
class songs(object):
def GET(self):
graph = app.graph
songs = getSongsFromShow(graph, app.show)
web.header("Content-type", "application/json")
return jsonlib.write({"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())))
return "ok"
class seekPlayOrPause(object):
def POST(self):
player = app.player
data = jsonlib.read(web.data(), use_float=True)
if player.isPlaying():
player.pause()
else:
player.seek(data['t'])
player.resume()
def makeWebApp(theApp):
global app
app = theApp
urls = (r"/", "root",
r"/time", "timeResource",
r"/song", "songResource",
r"/songs", "songs",
r"/seekPlayOrPause", "seekPlayOrPause",
)
return web.application(urls, globals(), autoreload=False)
|