pyTivo
[pyTivo/krkeegan.git] / plugins / video / video.py
blob16b50eec0a99568910b9d1418385eca3b5fc88df
1 import transcode, os, socket, re
2 from Cheetah.Template import Template
3 from plugin import Plugin
4 from urllib import unquote_plus, quote, unquote
5 from urlparse import urlparse
6 from xml.sax.saxutils import escape
8 SCRIPTDIR = os.path.dirname(__file__)
10 class video(Plugin):
12 content_type = 'x-container/tivo-videos'
14 def SendFile(self, handler, container, name):
16 #cheep hack
17 if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-':
18 handler.send_response(404)
19 handler.end_headers()
20 return
22 o = urlparse("http://fake.host" + handler.path)
23 path = unquote_plus(o.path)
24 handler.send_response(200)
25 handler.end_headers()
26 transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile)
28 def QueryContainer(self, handler, query):
30 subcname = query['Container'][0]
31 cname = subcname.split('/')[0]
33 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
34 handler.send_response(404)
35 handler.end_headers()
36 return
38 path = self.get_local_path(handler, query)
39 def isdir(file):
40 return os.path.isdir(os.path.join(path, file))
42 handler.send_response(200)
43 handler.end_headers()
44 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
45 t.name = subcname
46 t.files, t.total, t.start = self.get_files(handler, query, lambda f: os.path.isdir(os.path.join(path, f)) or transcode.suported_format(os.path.join(path,f)) )
47 t.isdir = isdir
48 t.quote = quote
49 t.escape = escape
50 handler.wfile.write(t)