pyTivo
[pyTivo/krkeegan.git] / plugins / video / video.py
blobcac78e6cec5a094a14d103b1dc08d3aeee470bb2
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
7 from lrucache import LRUCache
9 SCRIPTDIR = os.path.dirname(__file__)
12 class video(Plugin):
14 content_type = 'x-container/tivo-videos'
15 playable_cache = LRUCache(1000)
17 def SendFile(self, handler, container, name):
19 #cheep hack
20 if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-':
21 handler.send_response(404)
22 handler.end_headers()
23 return
25 o = urlparse("http://fake.host" + handler.path)
26 path = unquote_plus(o.path)
27 handler.send_response(200)
28 handler.end_headers()
29 transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile)
33 def QueryContainer(self, handler, query):
35 subcname = query['Container'][0]
36 cname = subcname.split('/')[0]
38 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
39 handler.send_response(404)
40 handler.end_headers()
41 return
43 path = self.get_local_path(handler, query)
44 def isdir(file):
45 return os.path.isdir(os.path.join(path, file))
47 def VideoFileFilter(file):
48 full_path = os.path.join(path, file)
50 if full_path in self.playable_cache:
51 return self.playable_cache[full_path]
52 if os.path.isdir(full_path) or transcode.suported_format(full_path):
53 self.playable_cache[full_path] = True
54 return True
55 else:
56 self.playable_cache[full_path] = False
57 return False
59 handler.send_response(200)
60 handler.end_headers()
61 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
62 t.name = subcname
63 t.files, t.total, t.start = self.get_files(handler, query, VideoFileFilter)
64 t.isdir = isdir
65 t.quote = quote
66 t.escape = escape
67 handler.wfile.write(t)