libsaria: Web server controls playback

I can choose songs to play by clicking rows in an html table.  I also
write text to the client as it becomes available, rather than doing it
all at once.
This commit is contained in:
Bryan Schumaker 2011-05-15 15:39:14 -04:00
parent 7ffd4e14fd
commit c02bffb652
7 changed files with 85 additions and 27 deletions

7
html/controls.js vendored
View File

@ -2,7 +2,7 @@
function control(action) function control(action)
{ {
send_request("controls.py?a=" + action); send_request("RPC/" + action);
} }
function play() function play()
@ -39,3 +39,8 @@ function rewind()
{ {
control("rewind"); control("rewind");
} }
function play_id(id)
{
control("play_id/" + id)
}

View File

@ -1,20 +1,20 @@
<html> <html>
<head> <!--<head>
<script type="text/javascript" src="utils.js"></script> <script type="text/javascript" src="utils.js"></script>-->
</head> </head>
<body> <body>
<table> <table>
<tr><td><a href="library.py">Library Browser</a></td></tr> <tr><td><a href="library.html">Library Browser</a></td></tr>
<tr><td><a href="library2.py">Library Browser 2</a></td></tr> <!--<tr><td><a href="library2.py">Library Browser 2</a></td></tr>
<tr><td><a href="controls.html">Remote Controls</a></td></tr> <tr><td><a href="controls.html">Remote Controls</a></td></tr>
<tr><td id="vers"></td><td id="up"></td></tr> <tr><td id="vers"></td><td id="up"></td></tr>-->
</table> </table>
<script type="text/javascript"> <!--<script type="text/javascript">
set_attr_once("version", "vers"); set_attr_once("version", "vers");
set_attr("uptime", "up"); set_attr("uptime", "up");
</script> </script>-->
</body> </body>
</html> </html>

View File

@ -11,5 +11,8 @@ def format_path(path):
def find_file(path): def find_file(path):
return pages.has_file(path) return pages.has_file(path)
def open_file(path): def get_type(path):
return pages.page_text(path) return pages.file_type(path)
def send_file(write, path):
pages.page_text(write, path)

View File

@ -9,19 +9,30 @@ docs.update(rpc.docs)
types = { types = {
"html":"text/html", "html":"text/html",
"js":"text/javascript",
} }
def lookup(file): def lookup(file):
doc = docs doc = docs
for cmp in file: for i, cmp in enumerate(file):
doc = doc.get(cmp, None) doc = doc.get(cmp, None)
if doc == None: if doc == None:
break break
return doc elif len(doc) == 2:
break
return doc, i + 1
def has_file(file): def has_file(file):
return lookup(file) != None doc, index = lookup(file)
return doc != None
def page_text(file): def file_type(file):
func, type = lookup(file) (func, type), index = lookup(file)
return func(), types[type] return type
def page_text(write, file):
(func, type), index = lookup(file)
if len(file) > index:
res = func(write, file[index:])
else:
res = func(write)

View File

@ -1,11 +1,42 @@
# Bryan Schumaker (5 / 15 / 2011) # Bryan Schumaker (5 / 15 / 2011)
def index(): from libsaria import sources
text = "<html> \
<body>Hello, world!</body> \ SCRIPT = "<script type=\"text/javascript\" src=\"%s\"></script>"
</html>"
return text def write_file(write, file):
f = open(file)
write(f.read())
f.close()
def index(write):
write_file(write, "html/index.html")
def library(write):
write("<html><head>")
write(SCRIPT % "controls.js")
write(SCRIPT % "utils.js")
write("</head><body><table>\n")
color = "white"
for id, title, artist, album in sources.library.walk_library("id", "title", "artist", "album"):
write("<tr style=\"background:%s\" ondblclick=play_id(%s)>" % (color, id))
write("<td><a href=\"RPC/play_id/%s\" onclick=\"play_id(%s);return false\">%s</a></td>" % (id, id, title))
write("<td>%s</td><td>%s</td>\n" % (artist, album))
if color == "white":
color = "aliceblue"
else:
color = "white"
write("</table></body></html>")
def controls(write):
write_file(write, "html/controls.js")
def utils(write):
write_file(write, "html/utils.js")
docs = { docs = {
"index.html":(index, "html"), "index.html":(index, "html"),
"library.html":(library, "html"),
"controls.js":(controls, "js"),
"utils.js":(utils, "js"),
} }

View File

@ -1,18 +1,24 @@
# Bryan Schumaker (5 / 15 / 2011) # Bryan Schumaker (5 / 15 / 2011)
from libsaria import sources
from libsaria import controls from libsaria import controls
def play(): def play(write):
controls.play() controls.play()
return "" write("")
def pause(): def pause(write):
controls.pause() controls.pause()
return "" write("")
def play_id(write, id):
sources.play_id(long(id[0]))
write("")
rpc = { rpc = {
"play.html":(play, "html"), "play.html":(play, "html"),
"pause.html":(pause, "html"), "pause.html":(pause, "html"),
"play_id":(play_id, "html"),
} }
docs = { docs = {

View File

@ -9,9 +9,11 @@ class Handler(BaseHTTPRequestHandler):
if not files.find_file(path): if not files.find_file(path):
self.send_error(404, "File Not Found: %s" % path) self.send_error(404, "File Not Found: %s" % path)
return return
text, type = files.open_file(path)
type = files.get_type(path)
self.send_response(200) self.send_response(200)
self.send_header('Content-type', type) self.send_header('Content-type', type)
self.end_headers() self.end_headers()
self.wfile.write(text)
files.send_file(self.wfile.write, path)
self.wfile.close()