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
9 SCRIPTDIR
= os
.path
.dirname(__file__
)
10 debug
= Config
.getDebug()
11 hack83
= Config
.getHack83()
12 def debug_write(data
):
15 debug_out
.append('httpserver.py - ')
17 debug_out
.append(str(x
))
18 fdebug
= open('debug.txt', 'a')
19 fdebug
.write(' '.join(debug_out
))
22 class TivoHTTPServer(SocketServer
.ThreadingMixIn
, BaseHTTPServer
.HTTPServer
):
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 self
.containers
[name
] = settings
34 class TivoHTTPHandler(BaseHTTPServer
.BaseHTTPRequestHandler
):
36 def address_string(self
):
37 host
, port
= self
.client_address
[:2]
43 for name
, container
in self
.server
.containers
.items():
45 path
= unquote_plus(self
.path
)
46 if path
.startswith('/' + name
):
47 plugin
= GetPlugin(container
['type'])
48 plugin
.SendFile(self
, container
, name
)
51 ## Not a file not a TiVo command fuck them
52 if not self
.path
.startswith('/TiVoConnect'):
56 o
= urlparse("http://fake.host" + self
.path
)
57 query
= parse_qs(o
[4])
60 if query
.has_key('Command') and len(query
['Command']) >= 1:
62 command
= query
['Command'][0]
64 #If we are looking at the root container
65 if command
== "QueryContainer" and ( not query
.has_key('Container') or query
['Container'][0] == '/'):
69 if query
.has_key('Container'):
70 #Dispatch to the container plugin
71 foundContainer
= False
72 for name
, container
in self
.server
.containers
.items():
73 if query
['Container'][0].startswith(name
):
75 plugin
= GetPlugin(container
['type'])
76 if hasattr(plugin
,command
):
77 method
= getattr(plugin
, command
)
80 self
.unsuported(query
)
82 if not foundContainer
:
83 self
.unsuported(query
)
85 self
.unsuported(query
)
87 def RootContiner(self
):
88 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates', 'root_container.tmpl'))
89 t
.containers
= self
.server
.containers
90 t
.hostname
= socket
.gethostname()
91 t
.GetPlugin
= GetPlugin
92 self
.send_response(200)
97 self
.send_response(200)
98 self
.send_header('Content-type', 'text/html')
100 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates', 'info_page.tmpl'))
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
)
114 self
.send_response(404)
115 self
.send_header('Content-type', 'text/html')
117 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates','unsuported.tmpl'))
121 self
.send_response(404)
122 self
.send_header('Content-type', 'text/html')
124 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates','unsuported.tmpl'))
129 if __name__
== '__main__':
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()