Renamed method SendFile to send_file
[pyTivo.git] / httpserver.py
blobb5eda8f534cc64a719c952edcd2547de7978644c
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, settings):
18 if self.containers.has_key(name) or name == 'TivoConnect':
19 raise "Container Name in use"
20 self.containers[name] = settings
22 class TivoHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
24 def address_string(self):
25 host, port = self.client_address[:2]
26 return host
28 def do_GET(self):
30 ## Get File
31 for name, container in self.server.containers.items():
32 #XXX make a regex
33 path = unquote_plus(self.path)
34 if path.startswith('/' + name):
35 plugin = GetPlugin(container['type'])
36 plugin.send_file(self, container, name)
37 return
39 ## Not a file not a TiVo command fuck them
40 if not self.path.startswith('/TiVoConnect'):
41 self.infopage()
42 return
44 o = urlparse("http://fake.host" + self.path)
45 query = parse_qs(o[4])
47 mname = False
48 if query.has_key('Command') and len(query['Command']) >= 1:
50 command = query['Command'][0]
52 #If we are looking at the root container
53 if command == "QueryContainer" and ( not query.has_key('Container') or query['Container'][0] == '/'):
54 self.root_continer()
55 return
57 if query.has_key('Container'):
58 #Dispatch to the container plugin
59 for name, container in self.server.containers.items():
60 if query['Container'][0].startswith(name):
61 plugin = GetPlugin(container['type'])
62 if hasattr(plugin,command):
63 method = getattr(plugin, command)
64 method(self, query)
65 else:
66 self.unsuported(query)
67 break
68 else:
69 self.unsuported(query)
71 def root_continer(self):
72 t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'root_container.tmpl'))
73 t.containers = self.server.containers
74 t.hostname = socket.gethostname()
75 t.GetPlugin = GetPlugin
76 self.send_response(200)
77 self.end_headers()
78 self.wfile.write(t)
80 def infopage(self):
81 self.send_response(200)
82 self.send_header('Content-type', 'text/html')
83 self.end_headers()
84 t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'info_page.tmpl'))
85 self.wfile.write(t)
86 self.end_headers()
88 def unsuported(self, query):
89 self.send_response(404)
90 self.send_header('Content-type', 'text/html')
91 self.end_headers()
92 t = Template(file=os.path.join(SCRIPTDIR,'templates','unsuported.tmpl'))
93 t.query = query
94 self.wfile.write(t)
96 if __name__ == '__main__':
97 def start_server():
98 httpd = TivoHTTPServer(('', 9032), TivoHTTPHandler)
99 httpd.add_container('test', 'x-container/tivo-videos', r'C:\Documents and Settings\Armooo\Desktop\pyTivo\test')
100 httpd.serve_forever()
102 start_server()