ocarina/plugins/web_server.py

82 lines
1.8 KiB
Python

# Bryan Schumaker (11/13/2010)
import ocarina
import imp
from libsaria import threads
from libsaria import lastfm
path = ocarina.libsaria.path
exists = path.exists
join = path.join
sep = path.sep
basename = path.basename
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
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"}
class HTTPRequest(BaseHTTPRequestHandler):
def do_GET(self):
global html
if self.path == "" or self.path == "/":
self.path = "/index.html"
try:
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":
file = basename(base)
mod = imp.load_source(file, fpath)
mod.to_html(self.wfile)
else:
f = open(fpath)
self.wfile.write(f.read())
f.close()
except IOError:
self.send_error(404, 'File Not Found: %s' % self.path)
def do_POST(self):
print self.path
print "Received a post request!"
def start_server():
global server
server = HTTPServer(('', 4242), HTTPRequest)
server.serve_forever()
def start():
threads.background(start_server)
def stop():
server.shutdown()
def check_version():
if ocarina.__major__ != 4:
return False
if ocarina.__minor__ >= 1:
return True
return False