Changed line endings
[pyTivo.git] / httpserver.py
blobb567dd37d4ec3f5c3dd61502927252769261868e
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
7 import Config
9 SCRIPTDIR = os.path.dirname(__file__)
10 debug = Config.getDebug()
11 hack83 = Config.getHack83()
12 def debug_write(data):
13 if debug:
14 debug_out = []
15 debug_out.append('httpserver.py - ')
16 for x in data:
17 debug_out.append(str(x))
18 fdebug = open('debug.txt', 'a')
19 fdebug.write(' '.join(debug_out))
20 fdebug.close()
22 class TivoHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
23 containers = {}
25 def __init__(self, server_address, RequestHandlerClass):
26 BaseHTTPServer.HTTPServer.__init__(self, server_address, RequestHandlerClass)
27 self.daemon_threads = True
29 def add_container(self, name, settings):
30 if self.containers.has_key(name) or name == 'TivoConnect':
31 raise "Container Name in use"
32 self.containers[name] = settings
34 class TivoHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
36 def address_string(self):
37 host, port = self.client_address[:2]
38 return host
40 def do_GET(self):
42 ## Get File
43 for name, container in self.server.containers.items():
44 #XXX make a regex
45 path = unquote_plus(self.path)
46 if path.startswith('/' + name):
47 plugin = GetPlugin(container['type'])
48 plugin.SendFile(self, container, name)
49 return
51 ## Not a file not a TiVo command fuck them
52 if not self.path.startswith('/TiVoConnect'):
53 self.infopage()
54 return
56 o = urlparse("http://fake.host" + self.path)
57 query = parse_qs(o[4])
59 mname = False
60 if query.has_key('Command') and len(query['Command']) >= 1:
62 command = query['Command'][0]
64 #If we are looking at the root container
65 if command == "QueryContainer" and ( not query.has_key('Container') or query['Container'][0] == '/'):
66 self.RootContiner()
67 return
69 if query.has_key('Container'):
70 #Dispatch to the container plugin
71 foundContainer = False
72 for name, container in self.server.containers.items():
73 if query['Container'][0].startswith(name):
74 foundContainer = True
75 plugin = GetPlugin(container['type'])
76 if hasattr(plugin,command):
77 method = getattr(plugin, command)
78 method(self, query)
79 else:
80 self.unsuported(query)
81 break
82 if not foundContainer:
83 self.unsuported(query)
84 else:
85 self.unsuported(query)
87 def RootContiner(self):
88 t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'root_container.tmpl'))
89 t.containers = self.server.containers
90 t.hostname = socket.gethostname()
91 t.GetPlugin = GetPlugin
92 self.send_response(200)
93 self.end_headers()
94 self.wfile.write(t)
96 def infopage(self):
97 self.send_response(200)
98 self.send_header('Content-type', 'text/html')
99 self.end_headers()
100 t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'info_page.tmpl'))
101 self.wfile.write(t)
102 self.end_headers()
104 def unsuported(self, query):
105 if hack83 and 'Command' in query and 'Filter' in query:
106 debug_write(['Unsupported request, checking to see if it is video.', '\n'])
107 command = query['Command'][0]
108 plugin = plugin = GetPlugin('video')
109 if "".join(query['Filter']).find('video') >= 0 and hasattr(plugin,command):
110 debug_write(['Unsupported request, yup it is video send to video plugin for it to sort out.', '\n'])
111 method = getattr(plugin, command)
112 method(self, query)
113 else:
114 self.send_response(404)
115 self.send_header('Content-type', 'text/html')
116 self.end_headers()
117 t = Template(file=os.path.join(SCRIPTDIR,'templates','unsuported.tmpl'))
118 t.query = query
119 self.wfile.write(t)
120 else:
121 self.send_response(404)
122 self.send_header('Content-type', 'text/html')
123 self.end_headers()
124 t = Template(file=os.path.join(SCRIPTDIR,'templates','unsuported.tmpl'))
125 t.query = query
126 self.wfile.write(t)
129 if __name__ == '__main__':
130 def start_server():
131 httpd = TivoHTTPServer(('', 9032), TivoHTTPHandler)
132 httpd.add_container('test', 'x-container/tivo-videos', r'C:\Documents and Settings\Armooo\Desktop\pyTivo\test')
133 httpd.serve_forever()
135 start_server()