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__
)
11 debug
= config
.getDebug()
12 hack83
= config
.getHack83()
13 def debug_write(data
):
16 debug_out
.append('httpserver.py - ')
18 debug_out
.append(str(x
))
19 fdebug
= open('debug.txt', 'a')
20 fdebug
.write(' '.join(debug_out
))
23 class TivoHTTPServer(SocketServer
.ThreadingMixIn
, BaseHTTPServer
.HTTPServer
):
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]
44 basepath
= unquote_plus(self
.path
).split('/')[1]
47 for name
, container
in self
.server
.containers
.items():
49 plugin
= GetPlugin(container
['type'])
50 plugin
.send_file(self
, container
, name
)
53 ## Not a file not a TiVo command fuck them
54 if not self
.path
.startswith('/TiVoConnect'):
58 o
= urlparse("http://fake.host" + self
.path
)
59 query
= parse_qs(o
[4])
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] == '/'):
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
)
80 self
.unsupported(query
)
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()
90 self
.send_response(200)
95 self
.send_response(200)
96 self
.send_header('Content-type', 'text/html')
98 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates', 'info_page.tmpl'))
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
)
112 self
.send_response(404)
113 self
.send_header('Content-type', 'text/html')
115 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates','unsupported.tmpl'))
119 self
.send_response(404)
120 self
.send_header('Content-type', 'text/html')
122 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates','unsupported.tmpl'))
126 if __name__
== '__main__':
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()