- Added dlfl's mod to allow for transferring of ".tivo" files
[pyTivo.git] / httpserver.py
bloba08f28fdb5b00144e890cfaf558ec0bbdea28c72
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[4])
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 if query['Container'][0].startswith(name):
56 plugin = GetPlugin(container['type'])
57 if hasattr(plugin,command):
58 method = getattr(plugin, command)
59 method(self, query)
60 else:
61 self.unsuported(query)
62 break
63 else:
64 self.unsuported(query)
66 def RootContiner(self):
67 t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'root_container.tmpl'))
68 t.containers = self.server.containers
69 t.hostname = socket.gethostname()
70 t.GetPlugin = GetPlugin
71 self.send_response(200)
72 self.end_headers()
73 self.wfile.write(t)
75 def infopage(self):
76 self.send_response(200)
77 self.send_header('Content-type', 'text/html')
78 self.end_headers()
79 t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'info_page.tmpl'))
80 self.wfile.write(t)
81 self.end_headers()
83 def unsuported(self, query):
84 self.send_response(404)
85 self.send_header('Content-type', 'text/html')
86 self.end_headers()
87 t = Template(file=os.path.join(SCRIPTDIR,'templates','unsuported.tmpl'))
88 t.query = query
89 self.wfile.write(t)
91 if __name__ == '__main__':
92 def start_server():
93 httpd = TivoHTTPServer(('', 9032), TivoHTTPHandler)
94 httpd.add_container('test', 'x-container/tivo-videos', r'C:\Documents and Settings\Armooo\Desktop\pyTivo\test')
95 httpd.serve_forever()
97 start_server()