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