pyTivo
[pyTivo/krkeegan.git] / httpserver.py
blob22b8d17b04e88d5f3bc646c5d18f0a3befcc5960
1 import time, os, BaseHTTPServer, SocketServer, socket, re
2 from urllib import unquote_plus, quote, unquote
3 from urlparse import urlparse
4 from cgi import parse_qs
5 from Cheetah.Template import Template
6 from plugin import GetPlugin
8 SCRIPTDIR = os.path.dirname(__file__)
10 class TivoHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
11 containers = {}
13 def __init__(self, server_address, RequestHandlerClass):
14 BaseHTTPServer.HTTPServer.__init__(self, server_address, RequestHandlerClass)
15 self.daemon_threads = True
17 def add_container(self, name, type, path):
18 if self.containers.has_key(name) or name == 'TivoConnect':
19 raise "Container Name in use"
20 self.containers[name] = {'type' : type, 'path' : path}
22 class TivoHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
23 def do_GET(self):
25 ## Get File
26 for name, container in self.server.containers.items():
27 #XXX make a regex
28 path = unquote_plus(self.path)
29 if path.startswith('/' + name):
30 plugin = GetPlugin(container['type'])
31 plugin.SendFile(self, container, name)
32 return
34 ## Not a file not a TiVo command fuck them
35 if not self.path.startswith('/TiVoConnect'):
36 self.infopage()
37 return
39 o = urlparse("http://fake.host" + self.path)
40 query = parse_qs(o.query)
42 mname = False
43 if query.has_key('Command') and len(query['Command']) >= 1:
45 command = query['Command'][0]
47 #If we are looking at the root container
48 if command == "QueryContainer" and ( not query.has_key('Container') or query['Container'][0] == '/'):
49 self.RootContiner()
50 return
52 if query.has_key('Container'):
53 #Dispatch to the container plugin
54 for name, container in self.server.containers.items():
55 print name, query['Container'][0]
56 if query['Container'][0].startswith(name):
57 plugin = GetPlugin(container['type'])
58 if hasattr(plugin,command):
59 method = getattr(plugin, command)
60 method(self, query)
61 else:
62 self.unsuported(query)
63 break
64 else:
65 self.unsuported(query)
67 def RootContiner(self):
68 t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'root_container.tmpl'))
69 t.containers = self.server.containers
70 t.hostname = socket.gethostname()
71 t.GetPlugin = GetPlugin
72 self.send_response(200)
73 self.end_headers()
74 self.wfile.write(t)
76 def infopage(self):
77 self.send_response(200)
78 self.send_header('Content-type', 'text/html')
79 self.end_headers()
80 t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'info_page.tmpl'))
81 self.wfile.write(t)
82 self.end_headers()
84 def unsuported(self, query):
85 self.send_response(404)
86 self.send_header('Content-type', 'text/html')
87 self.end_headers()
88 t = Template(file=os.path.join(SCRIPTDIR,'templates','unsuported.tmpl'))
89 t.query = query
90 self.wfile.write(t)
92 if __name__ == '__main__':
93 def start_server():
94 httpd = TivoHTTPServer(('', 9032), TivoHTTPHandler)
95 httpd.add_container('test', 'x-container/tivo-videos', r'C:\Documents and Settings\Armooo\Desktop\pyTivo\test')
96 httpd.serve_forever()
98 start_server()