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.
This commit is contained in:
Bryan Schumaker 2010-11-17 23:33:37 -05:00
parent 9f20a17228
commit 63ec0030e7
1 changed files with 56 additions and 39 deletions

View File

@ -2,6 +2,7 @@
import ocarina import ocarina
import imp import imp
from libsaria.sources import library
from libsaria import threads from libsaria import threads
from libsaria import lastfm from libsaria import lastfm
@ -10,6 +11,7 @@ exists = path.exists
join = path.join join = path.join
sep = path.sep sep = path.sep
basename = path.basename basename = path.basename
splitext = path.splitext
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
@ -17,17 +19,16 @@ html = join(path.cwd(), "html")
server = None server = None
types = {".html":"text/html", ".js":"text/javascript", types = {".html":"text/html", ".js":"text/javascript",
".ico":"image/vnd.microsoft.icon", ".ico":"image/vnd.microsoft.icon",
".py":"text/html", ".py":"text/html", ".jpg":"image/jpeg",
".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): def format_text(text):
text = text.replace("%20", " ") text = text.replace("%20", " ")
text = text.replace("%2F", "/")
text = text.replace("%27", "'")
text = text.replace("%2C", ",")
return text return text
def format_path(path): def format_path(path):
@ -51,40 +52,16 @@ class HTTPRequest(BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
try: try:
fpath, args = format_path(self.path) fpath, args = format_path(self.path)
#global html print fpath
#if self.path == "" or self.path == "/": base, ext = splitext(fpath)
# 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()
if ext == ".py": 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: else:
f = open(fpath) self.other_file(fpath, ext)
self.wfile.write(f.read())
f.close()
except IOError: except IOError:
self.send_error(404, 'File Not Found: %s' % self.path) self.send_error(404, 'File Not Found: %s' % self.path)
@ -92,6 +69,46 @@ class HTTPRequest(BaseHTTPRequestHandler):
print self.path print self.path
print "Received a post request!" 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(): def start_server():
global server global server