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
11 SCRIPTDIR
= os
.path
.dirname(__file__
)
13 class TivoHTTPServer(SocketServer
.ThreadingMixIn
, BaseHTTPServer
.HTTPServer
):
16 def __init__(self
, server_address
, RequestHandlerClass
):
17 BaseHTTPServer
.HTTPServer
.__init
__(self
, server_address
,
19 self
.daemon_threads
= True
21 def add_container(self
, name
, settings
):
22 if self
.containers
.has_key(name
) or name
== 'TiVoConnect':
23 raise "Container Name in use"
25 settings
['content_type'] = GetPlugin(settings
['type']).CONTENT_TYPE
26 self
.containers
[name
] = settings
28 print 'Unable to add container', name
31 self
.containers
.clear()
32 for section
, settings
in config
.getShares():
33 self
.add_container(section
, settings
)
35 def set_beacon(self
, beacon
):
38 class TivoHTTPHandler(BaseHTTPServer
.BaseHTTPRequestHandler
):
42 def address_string(self
):
43 host
, port
= self
.client_address
[:2]
47 tsn
= self
.headers
.getheader('TiVo_TCD_ID',
48 self
.headers
.getheader('tsn', ''))
50 ip
= self
.address_string()
53 if not tsn
in self
.tivo_names
:
54 self
.tivo_names
[tsn
] = self
.server
.beacon
.get_name(ip
)
56 basepath
= unquote_plus(self
.path
).split('/')[1]
59 for name
, container
in self
.server
.containers
.items():
61 plugin
= GetPlugin(container
['type'])
62 plugin
.send_file(self
, container
, name
)
65 ## Not a file not a TiVo command fuck them
66 if not self
.path
.startswith('/TiVoConnect'):
70 o
= urlparse("http://fake.host" + self
.path
)
71 query
= parse_qs(o
[4])
74 if query
.has_key('Command') and len(query
['Command']) >= 1:
76 command
= query
['Command'][0]
78 # If we are looking at the root container
79 if command
== "QueryContainer" and \
80 (not query
.has_key('Container') or query
['Container'][0] == '/'):
84 if query
.has_key('Container'):
85 # Dispatch to the container plugin
86 basepath
= unquote(query
['Container'][0].split('/')[0])
87 for name
, container
in self
.server
.containers
.items():
89 plugin
= GetPlugin(container
['type'])
90 if hasattr(plugin
, command
):
91 method
= getattr(plugin
, command
)
97 # If we made it here it means we couldn't match the request to
99 self
.unsupported(query
)
101 def root_container(self
):
102 tsn
= self
.headers
.getheader('TiVo_TCD_ID', '')
103 tsnshares
= config
.getShares(tsn
)
105 for section
, settings
in tsnshares
:
107 settings
['content_type'] = \
108 GetPlugin(settings
['type']).CONTENT_TYPE
109 tsncontainers
[section
] = settings
110 except Exception, msg
:
111 print section
, '-', msg
112 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates',
113 'root_container.tmpl'))
114 t
.containers
= tsncontainers
115 t
.hostname
= socket
.gethostname()
118 self
.send_response(200)
123 self
.send_response(200)
124 self
.send_header('Content-type', 'text/html')
126 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates',
129 for section
, settings
in config
.getShares():
130 if 'type' in settings
and settings
['type'] == 'admin':
131 t
.admin
+= '<a href="/TiVoConnect?Command=Admin&Container=' + \
133 '">pyTivo Web Configuration</a><br>' + \
134 '<a href="/TiVoConnect?Command=NPL&Container=' + \
135 quote(section
) + '">pyTivo ToGo</a><br>'
137 t
.admin
= '<br><b>No Admin plugin installed in pyTivo.conf</b>' + \
138 '<br> If you wish to use the admin plugin add the ' + \
139 'following lines to pyTivo.conf<br><br>' + \
140 '[Admin]<br>type=admin'
142 t
.shares
= 'Video shares:<br/>'
143 for section
, settings
in config
.getShares():
144 if settings
.get('type') == 'video':
145 t
.shares
+= '<a href="TiVoConnect?Command=QueryContainer&' + \
146 'Container=' + quote(section
) + '">' + section
+ \
151 def unsupported(self
, query
):
152 self
.send_response(404)
153 self
.send_header('Content-type', 'text/html')
155 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates',
160 if __name__
== '__main__':
162 httpd
= TivoHTTPServer(('', 9032), TivoHTTPHandler
)
163 httpd
.add_container('test', 'x-container/tivo-videos',
164 r
'C:\Documents and Settings\Armooo' +
165 r
'\Desktop\pyTivo\test')
166 httpd
.serve_forever()