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 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]
44 for name
, container
in self
.server
.containers
.items():
46 path
= unquote_plus(self
.path
)
47 if path
.startswith('/' + name
):
48 plugin
= GetPlugin(container
['type'])
49 plugin
.send_file(self
, container
, name
)
52 ## Not a file not a TiVo command fuck them
53 if not self
.path
.startswith('/TiVoConnect'):
57 o
= urlparse("http://fake.host" + self
.path
)
58 query
= parse_qs(o
[4])
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] == '/'):
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
):
76 plugin
= GetPlugin(container
['type'])
77 if hasattr(plugin
,command
):
78 method
= getattr(plugin
, command
)
81 self
.unsuported(query
)
83 if not foundContainer
:
84 self
.unsuported(query
)
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)
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()