1 import time
, os
, BaseHTTPServer
, SocketServer
, socket
, shutil
, os
.path
2 from urllib
import unquote_plus
3 from urlparse
import urlparse
4 from cgi
import parse_qs
5 from Cheetah
.Template
import Template
8 SCRIPTDIR
= os
.path
.dirname(__file__
)
10 class TivoHTTPServer(SocketServer
.ThreadingMixIn
, BaseHTTPServer
.HTTPServer
):
13 def add_container(self
, name
, type, path
):
14 if self
.containers
.has_key(name
) or name
== 'TivoConnect':
15 raise "Container Name in use"
16 self
.containers
[name
] = {'type' : type, 'path' : path
}
18 class TivoHTTPHandler(BaseHTTPServer
.BaseHTTPRequestHandler
):
21 for name
, container
in self
.server
.containers
.items():
23 if self
.path
.startswith('/' + name
):
24 self
.send_static(name
, container
)
27 if not self
.path
.startswith('/TiVoConnect'):
31 o
= urlparse("http://fake.host" + self
.path
)
32 query
= parse_qs(o
.query
)
35 if query
.has_key('Command') and len(query
['Command']) >= 1:
36 mname
= 'do_' + query
['Command'][0]
37 if mname
and hasattr(self
, mname
):
38 method
= getattr(self
, mname
)
41 self
.unsuported(query
)
43 def do_QueryContainer(self
, query
):
45 if not query
.has_key('Container'):
46 query
['Container'] = ['/']
47 self
.send_response(200)
49 if query
['Container'][0] == '/':
50 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates', 'root_container.tmpl'))
51 t
.containers
= self
.server
.containers
52 t
.hostname
= socket
.gethostname()
56 subcname
= unquote_plus(query
['Container'][0])
57 cname
= subcname
.split('/')[0]
59 if not self
.server
.containers
.has_key(cname
):
62 container
= self
.server
.containers
[cname
]
64 folders
= subcname
.split('/')
65 path
= container
['path']
66 for folder
in folders
[1:]:
69 path
= path
+ '/' + folder
72 path
= container
['path'] + '/' + file
73 return os
.path
.isdir(path
)
75 files
= os
.listdir(path
)
77 files
= filter(lambda f
: os
.path
.isdir(path
+'/'+f
) or transcode
.suported_format(path
+'/'+f
), files
)
79 totalFiles
= len(files
)
82 if query
.has_key('ItemCount'):
83 count
= int(query
['ItemCount'] [0])
85 if query
.has_key('AnchorItem'):
86 anchor
= query
['AnchorItem'] [0]
87 for i
in range(len(files
)):
90 file_url
= '/TiVoConnect?Command=QueryContainer&Container=' + subcname
+ '/' + files
[i
]
92 file_url
= '/' + subcname
+ '/' + files
[i
]
93 if file_url
== anchor
:
96 if query
.has_key('AnchorOffset'):
97 index
= index
+ int(query
['AnchorOffset'][0])
98 files
= files
[index
:index
+ count
]
101 path
= container
['path'] + '/' + file
102 return os
.path
.isdir(path
)
104 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
112 def send_static(self
, name
, container
):
115 if self
.headers
.getheader('Range') and not self
.headers
.getheader('Range') == 'bytes=0-':
116 self
.send_response(404)
120 o
= urlparse("http://fake.host" + self
.path
)
121 path
= unquote_plus(o
.path
)
122 self
.send_response(200)
124 transcode
.output_video(container
['path'] + path
[len(name
)+1:], self
.wfile
)
127 self
.send_response(200)
128 self
.send_header('Content-type', 'text/html')
130 t
= Template(file=os
.path
.join(SCRIPTDIR
, 'templates', 'info_page.tmpl'))
134 def unsuported(self
, query
):
135 self
.send_response(404)
136 self
.send_header('Content-type', 'text/html')
138 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates','unsuported.tmpl'))
142 if __name__
== '__main__':
144 httpd
= TivoHTTPServer(('', 9032), TivoHTTPHandler
)
145 httpd
.add_container('test', 'x-container/tivo-videos', r
'C:\Documents and Settings\Armooo\Desktop\pyTivo\test')
146 httpd
.serve_forever()