Metadata time in UTC.
[pyTivo/krkeegan.git] / plugins / video / video.py
blob9835f92c6804c512ac1bcec6d619a3dc88fedfa3
1 import transcode, os, socket, re, urllib, zlib
2 from Cheetah.Template import Template
3 from plugin import Plugin, quote, unquote
4 from urlparse import urlparse
5 from xml.sax.saxutils import escape
6 from lrucache import LRUCache
7 from UserDict import DictMixin
8 from datetime import datetime, timedelta
9 import config
11 SCRIPTDIR = os.path.dirname(__file__)
13 CLASS_NAME = 'Video'
15 extfile = os.path.join(SCRIPTDIR, 'video.ext')
16 try:
17 extensions = file(extfile).read().split()
18 except:
19 extensions = None
21 class Video(Plugin):
23 CONTENT_TYPE = 'x-container/tivo-videos'
25 def pre_cache(self, full_path):
26 if Video.video_file_filter(self, full_path):
27 transcode.supported_format(full_path)
29 def video_file_filter(self, full_path, type=None):
30 if os.path.isdir(full_path):
31 return True
32 if extensions:
33 return os.path.splitext(full_path)[1].lower() in extensions
34 else:
35 return transcode.supported_format(full_path)
37 def send_file(self, handler, container, name):
38 if handler.headers.getheader('Range') and \
39 handler.headers.getheader('Range') != 'bytes=0-':
40 handler.send_response(206)
41 handler.send_header('Connection', 'close')
42 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
43 handler.send_header('Transfer-Encoding', 'chunked')
44 handler.end_headers()
45 handler.wfile.write("\x30\x0D\x0A")
46 return
48 tsn = handler.headers.getheader('tsn', '')
50 o = urlparse("http://fake.host" + handler.path)
51 path = unquote(o[2])
52 handler.send_response(200)
53 handler.end_headers()
54 transcode.output_video(container['path'] + path[len(name) + 1:],
55 handler.wfile, tsn)
57 def __isdir(self, full_path):
58 return os.path.isdir(full_path)
60 def __duration(self, full_path):
61 return transcode.video_info(full_path)[4]
63 def __est_size(self, full_path, tsn = ''):
64 # Size is estimated by taking audio and video bit rate adding 2%
66 if transcode.tivo_compatable(full_path, tsn):
67 # Is TiVo-compatible mpeg2
68 return int(os.stat(full_path).st_size)
69 else:
70 # Must be re-encoded
71 audioBPS = config.strtod(config.getAudioBR(tsn))
72 videoBPS = config.strtod(config.getVideoBR(tsn))
73 bitrate = audioBPS + videoBPS
74 return int((self.__duration(full_path) / 1000) *
75 (bitrate * 1.02 / 8))
77 def __getMetadataFromTxt(self, full_path):
78 metadata = {}
80 default_file = os.path.join(os.path.split(full_path)[0], 'default.txt')
81 description_file = full_path + '.txt'
83 metadata.update(self.__getMetadataFromFile(default_file))
84 metadata.update(self.__getMetadataFromFile(description_file))
86 return metadata
88 def __getMetadataFromFile(self, file):
89 metadata = {}
91 if os.path.exists(file):
92 for line in open(file):
93 if line.strip().startswith('#'):
94 continue
95 if not ':' in line:
96 continue
98 key, value = line.split(':', 1)
99 key = key.strip()
100 value = value.strip()
102 if key.startswith('v'):
103 if key in metadata:
104 metadata[key].append(value)
105 else:
106 metadata[key] = [value]
107 else:
108 metadata[key] = value
110 return metadata
112 def __metadata_basic(self, full_path):
113 metadata = {}
115 base_path, title = os.path.split(full_path)
116 originalAirDate = datetime.fromtimestamp(os.stat(full_path).st_ctime)
118 metadata['title'] = '.'.join(title.split('.')[:-1])
119 metadata['seriesTitle'] = metadata['title'] # default to the filename
120 metadata['originalAirDate'] = originalAirDate.isoformat()
122 metadata.update(self.__getMetadataFromTxt(full_path))
124 return metadata
126 def __metadata_full(self, full_path, tsn=''):
127 metadata = {}
128 metadata.update(self.__metadata_basic(full_path))
130 now = datetime.utcnow()
132 duration = self.__duration(full_path)
133 duration_delta = timedelta(milliseconds = duration)
135 metadata['time'] = now.isoformat()
136 metadata['startTime'] = now.isoformat()
137 metadata['stopTime'] = (now + duration_delta).isoformat()
139 metadata.update( self.__getMetadataFromTxt(full_path) )
141 metadata['size'] = self.__est_size(full_path, tsn)
142 metadata['duration'] = duration
144 min = duration_delta.seconds / 60
145 sec = duration_delta.seconds % 60
146 hours = min / 60
147 min = min % 60
148 metadata['iso_duration'] = 'P' + str(duration_delta.days) + \
149 'DT' + str(hours) + 'H' + str(min) + \
150 'M' + str(sec) + 'S'
151 return metadata
153 def QueryContainer(self, handler, query):
154 tsn = handler.headers.getheader('tsn', '')
155 subcname = query['Container'][0]
156 cname = subcname.split('/')[0]
158 if not handler.server.containers.has_key(cname) or \
159 not self.get_local_path(handler, query):
160 handler.send_response(404)
161 handler.end_headers()
162 return
164 container = handler.server.containers[cname]
165 precache = container.get('precache', 'False').lower() == 'true'
167 files, total, start = self.get_files(handler, query,
168 self.video_file_filter)
170 videos = []
171 local_base_path = self.get_local_base_path(handler, query)
172 for file in files:
173 video = VideoDetails()
174 video['name'] = os.path.split(file)[1]
175 video['path'] = file
176 video['part_path'] = file.replace(local_base_path, '', 1)
177 video['title'] = os.path.split(file)[1]
178 video['is_dir'] = self.__isdir(file)
179 if video['is_dir']:
180 video['small_path'] = subcname + '/' + video['name']
181 else:
182 if precache or len(files) == 1 or file in transcode.info_cache:
183 video['valid'] = transcode.supported_format(file)
184 if video['valid']:
185 video.update(self.__metadata_full(file, tsn))
186 else:
187 video['valid'] = True
188 video.update(self.__metadata_basic(file))
190 videos.append(video)
192 handler.send_response(200)
193 handler.end_headers()
194 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
195 t.container = cname
196 t.name = subcname
197 t.total = total
198 t.start = start
199 t.videos = videos
200 t.quote = quote
201 t.escape = escape
202 t.crc = zlib.crc32
203 t.guid = config.getGUID()
204 handler.wfile.write(t)
206 def TVBusQuery(self, handler, query):
207 tsn = handler.headers.getheader('tsn', '')
208 file = query['File'][0]
209 path = self.get_local_path(handler, query)
210 file_path = path + file
212 file_info = VideoDetails()
213 file_info['valid'] = transcode.supported_format(file_path)
214 if file_info['valid']:
215 file_info.update(self.__metadata_full(file_path, tsn))
217 handler.send_response(200)
218 handler.end_headers()
219 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
220 t.video = file_info
221 t.escape = escape
222 handler.wfile.write(t)
224 class VideoDetails(DictMixin):
226 def __init__(self, d=None):
227 if d:
228 self.d = d
229 else:
230 self.d = {}
232 def __getitem__(self, key):
233 if key not in self.d:
234 self.d[key] = self.default(key)
235 return self.d[key]
237 def __contains__(self, key):
238 return True
240 def __setitem__(self, key, value):
241 self.d[key] = value
243 def __delitem__(self):
244 del self.d[key]
246 def keys(self):
247 return self.d.keys()
249 def __iter__(self):
250 return self.d.__iter__()
252 def iteritems(self):
253 return self.d.iteritems()
255 def default(self, key):
256 defaults = {
257 'showingBits' : '0',
258 'episodeNumber' : '0',
259 'displayMajorNumber' : '0',
260 'displayMinorNumber' : '0',
261 'isEpisode' : 'true',
262 'colorCode' : ('COLOR', '4'),
263 'showType' : ('SERIES', '5'),
264 'tvRating' : ('NR', '7')
266 if key in defaults:
267 return defaults[key]
268 elif key.startswith('v'):
269 return []
270 else:
271 return ''