Merge branch 'master' into krkeegan
[pyTivo.git] / httpserver.py
blob931d7ed53d3e8f3050474109421312ebce1693fb
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 ## Get File
45 for name, container in self.server.containers.items():
46 #XXX make a regex
47 path = unquote_plus(self.path)
48 if path.startswith('/' + 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 = 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 else:
112 self.send_response(404)
113 self.send_header('Content-type', 'text/html')
114 self.end_headers()
115 t = Template(file=os.path.join(SCRIPTDIR,'templates','unsupported.tmpl'))
116 t.query = query
117 self.wfile.write(t)
118 else:
119 self.send_response(404)
120 self.send_header('Content-type', 'text/html')
121 self.end_headers()
122 t = Template(file=os.path.join(SCRIPTDIR,'templates','unsupported.tmpl'))
123 t.query = query
124 self.wfile.write(t)
126 if __name__ == '__main__':
127 def start_server():
128 httpd = TivoHTTPServer(('', 9032), TivoHTTPHandler)
129 httpd.add_container('test', 'x-container/tivo-videos', r'C:\Documents and Settings\Armooo\Desktop\pyTivo\test')
130 httpd.serve_forever()
132 start_server()