Merge branch 'master' into subfolders-8.3
[pyTivo.git] / httpserver.py
blobcb6a4a94971465731db62a75e43f9cc7593de67c
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 settings['content_type'] = GetPlugin(settings['type']).CONTENT_TYPE
33 self.containers[name] = settings
35 class TivoHTTPHandler(BaseHTTPServer.BaseHTTPRequestHandler):
37 def address_string(self):
38 host, port = self.client_address[:2]
39 return host
41 def do_GET(self):
43 ## Get File
44 for name, container in self.server.containers.items():
45 #XXX make a regex
46 path = unquote_plus(self.path)
47 if path.startswith('/' + name):
48 plugin = GetPlugin(container['type'])
49 plugin.send_file(self, container, name)
50 return
52 ## Not a file not a TiVo command fuck them
53 if not self.path.startswith('/TiVoConnect'):
54 self.infopage()
55 return
57 o = urlparse("http://fake.host" + self.path)
58 query = parse_qs(o[4])
60 mname = False
61 if query.has_key('Command') and len(query['Command']) >= 1:
63 command = query['Command'][0]
65 #If we are looking at the root container
66 if command == "QueryContainer" and ( not query.has_key('Container') or query['Container'][0] == '/'):
67 self.root_continer()
68 return
70 if query.has_key('Container'):
71 #Dispatch to the container plugin
72 foundContainer = False
73 for name, container in self.server.containers.items():
74 if query['Container'][0].startswith(name):
75 foundContainer = True
76 plugin = GetPlugin(container['type'])
77 if hasattr(plugin,command):
78 method = getattr(plugin, command)
79 method(self, query)
80 else:
81 self.unsuported(query)
82 break
83 if not foundContainer:
84 self.unsuported(query)
85 else:
86 self.unsuported(query)
88 def root_continer(self):
89 t = Template(file=os.path.join(SCRIPTDIR, 'templates', 'root_container.tmpl'))
90 t.containers = self.server.containers
91 t.hostname = socket.gethostname()
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()