1 import time
, os
, BaseHTTPServer
, SocketServer
, socket
, shutil
, os
.path
2 from urllib
import unquote_plus
, quote
, unquote
3 from urlparse
import urlparse
4 from xml
.sax
.saxutils
import escape
5 from cgi
import parse_qs
6 from Cheetah
.Template
import Template
9 SCRIPTDIR
= os
.path
.dirname(__file__
)
11 class TivoHTTPServer(SocketServer
.ThreadingMixIn
, BaseHTTPServer
.HTTPServer
):
14 def add_container(self
, name
, type, path
):
15 if self
.containers
.has_key(name
) or name
== 'TivoConnect':
16 raise "Container Name in use"
17 self
.containers
[name
] = {'type' : type, 'path' : path
}
19 class TivoHTTPHandler(BaseHTTPServer
.BaseHTTPRequestHandler
):
22 for name
, container
in self
.server
.containers
.items():
24 if self
.path
.startswith('/' + name
):
25 self
.send_static(name
, container
)
28 if not self
.path
.startswith('/TiVoConnect'):
32 o
= urlparse("http://fake.host" + self
.path
)
33 query
= parse_qs(o
.query
)
36 if query
.has_key('Command') and len(query
['Command']) >= 1:
37 mname
= 'do_' + query
['Command'][0]
38 if mname
and hasattr(self
, mname
):
39 method
= getattr(self
, mname
)
42 self
.unsuported(query
)
44 def do_QueryContainer(self
, query
):
46 if not query
.has_key('Container'):
47 query
['Container'] = ['/']
48 self
.send_response(200)
50 if query
['Container'][0] == '/':
51 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates', 'root_container.tmpl'))
52 t
.containers
= self
.server
.containers
53 t
.hostname
= socket
.gethostname()
57 subcname
= query
['Container'][0]
58 cname
= subcname
.split('/')[0]
60 if not self
.server
.containers
.has_key(cname
):
63 container
= self
.server
.containers
[cname
]
65 folders
= subcname
.split('/')
66 path
= container
['path']
67 for folder
in folders
[1:]:
70 path
= os
.path
.join(path
, folder
)
73 files
= os
.listdir(path
)
75 files
= filter(lambda f
: os
.path
.isdir(os
.path
.join(path
, f
)) or transcode
.suported_format(os
.path
.join(path
,f
)), files
)
77 totalFiles
= len(files
)
80 return os
.path
.isdir(os
.path
.join(path
, file))
83 if query
.has_key('ItemCount'):
84 count
= int(query
['ItemCount'] [0])
86 if query
.has_key('AnchorItem'):
87 anchor
= unquote(query
['AnchorItem'][0])
89 for i
in range(len(files
)):
92 file_url
= '/TiVoConnect?Command=QueryContainer&Container=' + subcname
+ '/' + files
[i
]
94 file_url
= '/' + subcname
+ '/' + files
[i
]
95 if file_url
== anchor
:
98 if query
.has_key('AnchorOffset'):
99 index
= index
+ int(query
['AnchorOffset'][0])
100 files
= files
[index
:index
+ count
]
104 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
114 def send_static(self
, name
, container
):
117 if self
.headers
.getheader('Range') and not self
.headers
.getheader('Range') == 'bytes=0-':
118 self
.send_response(404)
122 o
= urlparse("http://fake.host" + self
.path
)
123 path
= unquote_plus(o
.path
)
124 self
.send_response(200)
126 transcode
.output_video(container
['path'] + path
[len(name
)+1:], self
.wfile
)
129 self
.send_response(200)
130 self
.send_header('Content-type', 'text/html')
132 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates', 'info_page.tmpl'))
136 def unsuported(self
, query
):
137 self
.send_response(404)
138 self
.send_header('Content-type', 'text/html')
140 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates','unsuported.tmpl'))
144 if __name__
== '__main__':
146 httpd
= TivoHTTPServer(('', 9032), TivoHTTPHandler
)
147 httpd
.add_container('test', 'x-container/tivo-videos', r
'C:\Documents and Settings\Armooo\Desktop\pyTivo\test')
148 httpd
.serve_forever()