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 from xml
.sax
.saxutils
import escape
10 SCRIPTDIR
= os
.path
.dirname(__file__
)
12 debug
= config
.getDebug()
13 hack83
= config
.getHack83()
15 def debug_write(data
):
18 debug_out
.append('httpserver.py - ')
20 debug_out
.append(str(x
))
21 fdebug
= open('debug.txt', 'a')
22 fdebug
.write(' '.join(debug_out
))
25 class TivoHTTPServer(SocketServer
.ThreadingMixIn
, BaseHTTPServer
.HTTPServer
):
28 def __init__(self
, server_address
, RequestHandlerClass
):
29 BaseHTTPServer
.HTTPServer
.__init
__(self
, server_address
,
31 self
.daemon_threads
= True
33 def add_container(self
, name
, settings
):
34 if self
.containers
.has_key(name
) or name
== 'TiVoConnect':
35 raise "Container Name in use"
36 settings
['content_type'] = GetPlugin(settings
['type']).CONTENT_TYPE
37 self
.containers
[name
] = settings
39 class TivoHTTPHandler(BaseHTTPServer
.BaseHTTPRequestHandler
):
41 def address_string(self
):
42 host
, port
= self
.client_address
[:2]
47 basepath
= unquote_plus(self
.path
).split('/')[1]
50 for name
, container
in self
.server
.containers
.items():
52 plugin
= GetPlugin(container
['type'])
53 plugin
.send_file(self
, container
, name
)
56 ## Not a file not a TiVo command fuck them
57 if not self
.path
.startswith('/TiVoConnect'):
61 o
= urlparse("http://fake.host" + self
.path
)
62 query
= parse_qs(o
[4])
65 if query
.has_key('Command') and len(query
['Command']) >= 1:
67 command
= query
['Command'][0]
69 # If we are looking at the root container
70 if command
== "QueryContainer" and \
71 (not query
.has_key('Container') or query
['Container'][0] == '/'):
75 if query
.has_key('Container'):
76 # Dispatch to the container plugin
77 for name
, container
in self
.server
.containers
.items():
78 if query
['Container'][0].startswith(name
):
79 plugin
= GetPlugin(container
['type'])
80 if hasattr(plugin
,command
):
81 method
= getattr(plugin
, command
)
84 self
.unsupported(query
)
87 self
.unsupported(query
)
89 def root_container(self
):
90 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates',
91 'root_container.tmpl'))
92 t
.containers
= self
.server
.containers
93 t
.hostname
= socket
.gethostname()
95 self
.send_response(200)
100 self
.send_response(200)
101 self
.send_header('Content-type', 'text/html')
103 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates',
108 def unsupported(self
, query
):
109 if hack83
and 'Command' in query
and 'Filter' in query
:
110 debug_write(['Unsupported request,',
111 'checking to see if it is video.\n'])
112 command
= query
['Command'][0]
113 plugin
= GetPlugin('video')
114 if ''.join(query
['Filter']).find('video') >= 0 and \
115 hasattr(plugin
, command
):
116 debug_write(['Unsupported request,',
118 'send to video plugin for it to sort out.\n'])
119 method
= getattr(plugin
, command
)
123 self
.send_response(404)
124 self
.send_header('Content-type', 'text/html')
126 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates',
131 if __name__
== '__main__':
133 httpd
= TivoHTTPServer(('', 9032), TivoHTTPHandler
)
134 httpd
.add_container('test', 'x-container/tivo-videos',
135 r
'C:\Documents and Settings\Armooo\Desktop\pyTivo\test')
136 httpd
.serve_forever()