From 63ec0030e76d2244fb5f398113ee62673a8da649 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Wed, 17 Nov 2010 23:33:37 -0500 Subject: [PATCH] Web server improvements The web server can now handle album art, ogg and mp3 files, and do it all from functions within the class instead of external functions. --- plugins/web_server.py | 95 +++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 39 deletions(-) diff --git a/plugins/web_server.py b/plugins/web_server.py index 22b8040a..ccaf39be 100644 --- a/plugins/web_server.py +++ b/plugins/web_server.py @@ -2,6 +2,7 @@ import ocarina import imp +from libsaria.sources import library from libsaria import threads from libsaria import lastfm @@ -10,6 +11,7 @@ exists = path.exists join = path.join sep = path.sep basename = path.basename +splitext = path.splitext from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer @@ -17,17 +19,16 @@ html = join(path.cwd(), "html") server = None types = {".html":"text/html", ".js":"text/javascript", ".ico":"image/vnd.microsoft.icon", - ".py":"text/html", - ".jpg":"image/jpeg"} + ".py":"text/html", ".jpg":"image/jpeg", + ".mp3":"audio/mpeg", + ".ogg":"audio/ogg"} -def pyfile(wfile, kw, path, base): - print kw - file = basename(base) - mod = imp.load_source(file, path) - mod.to_html(wfile, kw) def format_text(text): text = text.replace("%20", " ") + text = text.replace("%2F", "/") + text = text.replace("%27", "'") + text = text.replace("%2C", ",") return text def format_path(path): @@ -51,40 +52,16 @@ class HTTPRequest(BaseHTTPRequestHandler): def do_GET(self): try: fpath, args = format_path(self.path) - #global html - #if self.path == "" or self.path == "/": - # self.path = "/index.html" - #try: - # split = self.path.split("?", 1) - # self.path = split[0] - # args = "" - # if len(split) > 1: - # args = split[1] - # args = args.split("&") - # split = self.path.strip(sep).split(sep) - # #print split - # if split[0] == "artwork": - # split = split[1].split(".") - # artist, album = split[0], split[1] - # artist = artist.replace("%20", " ") - # album = album.replace("%20", " ") - # # print "%s, %s" % (artist, album) - # fpath = lastfm.get_artwork_tags(artist, album) - # else: - # fpath = html + self.path - #print fpath - if not exists(fpath): - raise IOError - base, ext = path.splitext(fpath) - self.send_response(200) - self.send_header('Content-type', '%s' % types[ext]) - self.end_headers() + print fpath + base, ext = splitext(fpath) if ext == ".py": - pyfile(self.wfile, args, fpath, base) + self.py_file(args, fpath, base) + elif ext == ".jpg": + self.jpg_file(fpath) + elif ext == ".mp3" or ext == ".ogg": + self.audio_file() else: - f = open(fpath) - self.wfile.write(f.read()) - f.close() + self.other_file(fpath, ext) except IOError: self.send_error(404, 'File Not Found: %s' % self.path) @@ -92,6 +69,46 @@ class HTTPRequest(BaseHTTPRequestHandler): print self.path print "Received a post request!" + def res_ok(self, ext): + self.send_response(200) + self.send_header('Content-type', '%s' % types[ext]) + self.end_headers() + + def py_file(self, kw, path, base): + if not exists(path): + raise IOError + file = basename(base) + mod = imp.load_source(file, path) + self.res_ok(".py") + mod.to_html(self.wfile, kw) + + def jpg_file(self, ipath): + split = self.path.strip("/").split("/") + if split[0] == "artwork": + artist = format_text(split[1]) + album = splitext(split[2])[0] + album = format_text(album) + ipath = lastfm.get_artwork_tags(artist, album) + base, ext = splitext(ipath) + self.other_file(ipath, ext) + + def audio_file(self): + sid, ext = splitext(self.path.strip("/")) + fpath = library.get_attrs(long(sid), "filepath")[0] + self.other_file(fpath, ext) + + def other_file(self, path, ext): + if not exists(path): + raise IOError + try: + print "Opening: %s" % path + f = open(path) + self.res_ok(ext) + self.wfile.write(f.read()) + f.close() + except Exception,e: + print "Error!! message: %s" % e + def start_server(): global server