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"
37 settings
['content_type'] = GetPlugin(settings
['type']).CONTENT_TYPE
38 self
.containers
[name
] = settings
40 print 'Unable to add container', name
42 class TivoHTTPHandler(BaseHTTPServer
.BaseHTTPRequestHandler
):
44 def address_string(self
):
45 host
, port
= self
.client_address
[:2]
50 basepath
= unquote_plus(self
.path
).split('/')[1]
53 for name
, container
in self
.server
.containers
.items():
55 plugin
= GetPlugin(container
['type'])
56 plugin
.send_file(self
, container
, name
)
59 ## Not a file not a TiVo command fuck them
60 if not self
.path
.startswith('/TiVoConnect'):
64 o
= urlparse("http://fake.host" + self
.path
)
65 query
= parse_qs(o
[4])
68 if query
.has_key('Command') and len(query
['Command']) >= 1:
70 command
= query
['Command'][0]
72 # If we are looking at the root container
73 if command
== "QueryContainer" and \
74 (not query
.has_key('Container') or query
['Container'][0] == '/'):
78 if query
.has_key('Container'):
79 # Dispatch to the container plugin
80 for name
, container
in self
.server
.containers
.items():
81 if query
['Container'][0].startswith(name
):
82 plugin
= GetPlugin(container
['type'])
83 if hasattr(plugin
, command
):
84 method
= getattr(plugin
, command
)
87 self
.unsupported(query
)
90 self
.unsupported(query
)
92 def root_container(self
):
93 tsn
= self
.headers
.getheader('TiVo_TCD_ID', '')
94 tsnshares
= config
.getShares(tsn
)
96 for section
, settings
in tsnshares
:
98 settings
['content_type'] = GetPlugin(settings
['type']).CONTENT_TYPE
99 tsncontainers
[section
] = settings
102 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates',
103 'root_container.tmpl'))
104 t
.containers
= tsncontainers
105 t
.hostname
= socket
.gethostname()
107 self
.send_response(200)
112 self
.send_response(200)
113 self
.send_header('Content-type', 'text/html')
115 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates',
120 def unsupported(self
, query
):
121 if hack83
and 'Command' in query
and 'Filter' in query
:
122 debug_write(['Unsupported request,',
123 'checking to see if it is video.\n'])
124 command
= query
['Command'][0]
125 plugin
= GetPlugin('video')
126 if ''.join(query
['Filter']).find('video') >= 0 and \
127 hasattr(plugin
, command
):
128 debug_write(['Unsupported request,',
130 'send to video plugin for it to sort out.\n'])
131 method
= getattr(plugin
, command
)
135 self
.send_response(404)
136 self
.send_header('Content-type', 'text/html')
138 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates',
143 if __name__
== '__main__':
145 httpd
= TivoHTTPServer(('', 9032), TivoHTTPHandler
)
146 httpd
.add_container('test', 'x-container/tivo-videos',
147 r
'C:\Documents and Settings\Armooo\Desktop\pyTivo\test')
148 httpd
.serve_forever()