Merge branch 'master' into subfolders-8.3
[pyTivo/wgw.git] / plugins / video / video.py
blob4e6520c9066019f040c8a46699a1688b917d80a9
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
10 import time
12 SCRIPTDIR = os.path.dirname(__file__)
14 CLASS_NAME = 'Video'
16 extfile = os.path.join(SCRIPTDIR, 'video.ext')
17 try:
18 extensions = file(extfile).read().split()
19 except:
20 extensions = None
22 debug = config.getDebug()
23 hack83 = config.getHack83()
25 def debug_write(data):
26 if debug:
27 debug_out = []
28 debug_out.append('Video.py - ')
29 for x in data:
30 debug_out.append(str(x))
31 fdebug = open('debug.txt', 'a')
32 fdebug.write(' '.join(debug_out))
33 fdebug.close()
35 if hack83:
36 debug_write(['Hack83 is enabled.\n'])
38 class Video(Plugin):
40 CONTENT_TYPE = 'x-container/tivo-videos'
42 # Used for 8.3's broken requests
43 count = 0
44 request_history = {}
46 def pre_cache(self, full_path):
47 if Video.video_file_filter(self, full_path):
48 transcode.supported_format(full_path)
50 def video_file_filter(self, full_path, type=None):
51 if os.path.isdir(full_path):
52 return True
53 if extensions:
54 return os.path.splitext(full_path)[1].lower() in extensions
55 else:
56 return transcode.supported_format(full_path)
58 def hack(self, handler, query, subcname):
59 debug_write(['Hack new request ------------------------\n'])
60 debug_write(['Hack TiVo request is: \n', query, '\n'])
61 queryAnchor = ''
62 rightAnchor = ''
63 leftAnchor = ''
64 tsn = handler.headers.getheader('tsn', '')
66 # not a tivo
67 if not tsn:
68 debug_write(['Hack this was not a TiVo request.\n'])
69 return query, None
71 # this breaks up the anchor item request into seperate parts
72 if 'AnchorItem' in query and query['AnchorItem'] != ['Hack8.3']:
73 queryAnchor = urllib.unquote_plus(''.join(query['AnchorItem']))
74 if queryAnchor.find('Container=') >= 0:
75 # This is a folder
76 queryAnchor = queryAnchor.split('Container=')[-1]
77 else:
78 # This is a file
79 queryAnchor = queryAnchor.split('/', 1)[-1]
80 leftAnchor, rightAnchor = queryAnchor.rsplit('/', 1)
81 debug_write(['Hack queryAnchor: ', queryAnchor,
82 ' leftAnchor: ', leftAnchor,
83 ' rightAnchor: ', rightAnchor, '\n'])
84 try:
85 path, state = self.request_history[tsn]
86 except KeyError:
87 # Never seen this tsn, starting new history
88 debug_write(['New TSN.\n'])
89 path = []
90 state = {}
91 self.request_history[tsn] = (path, state)
92 state['query'] = query
93 state['page'] = ''
94 state['time'] = int(time.time()) + 1000
96 debug_write(['Hack our saved request is: \n', state['query'], '\n'])
98 current_folder = subcname.split('/')[-1]
100 # Begin figuring out what the request TiVo sent us means
101 # There are 7 options that can occur
103 # 1. at the root - This request is always accurate
104 if len(subcname.split('/')) == 1:
105 debug_write(['Hack we are at the root.',
106 'Saving query, Clearing state[page].\n'])
107 path[:] = [current_folder]
108 state['query'] = query
109 state['page'] = ''
110 return query, path
112 # 2. entering a new folder
113 # If there is no AnchorItem in the request then we must be
114 # entering a new folder.
115 if 'AnchorItem' not in query:
116 debug_write(['Hack we are entering a new folder.',
117 'Saving query, setting time, setting state[page].\n'])
118 path[:] = subcname.split('/')
119 state['query'] = query
120 state['time'] = int(time.time())
121 files, total, start = self.get_files(handler, query,
122 self.video_file_filter)
123 if files:
124 state['page'] = files[0]
125 else:
126 state['page'] = ''
127 return query, path
129 # 3. Request a page after pyTivo sent a 302 code
130 # we know this is the proper page
131 if ''.join(query['AnchorItem']) == 'Hack8.3':
132 debug_write(['Hack requested page from 302 code.',
133 'Returning saved query,\n'])
134 return state['query'], path
136 # 4. this is a request for a file
137 if 'ItemCount' in query and int(''.join(query['ItemCount'])) == 1:
138 debug_write(['Hack requested a file', '\n'])
139 # Everything in this request is right except the container
140 query['Container'] = ['/'.join(path)]
141 state['page'] = ''
142 return query, path
144 # All remaining requests could be a second erroneous request for
145 # each of the following we will pause to see if a correct
146 # request is coming right behind it.
148 # Sleep just in case the erroneous request came first this
149 # allows a proper request to be processed first
150 debug_write(['Hack maybe erroneous request, sleeping.\n'])
151 time.sleep(.25)
153 # 5. scrolling in a folder
154 # This could be a request to exit a folder or scroll up or down
155 # within the folder
156 # First we have to figure out if we are scrolling
157 if 'AnchorOffset' in query:
158 debug_write(['Hack Anchor offset was in query.',
159 'leftAnchor needs to match ', '/'.join(path), '\n'])
160 if leftAnchor == str('/'.join(path)):
161 debug_write(['Hack leftAnchor matched.', '\n'])
162 query['Container'] = ['/'.join(path)]
163 files, total, start = self.get_files(handler, query,
164 self.video_file_filter)
165 debug_write(['Hack saved page is= ', state['page'],
166 ' top returned file is= ', files[0], '\n'])
167 # If the first file returned equals the top of the page
168 # then we haven't scrolled pages
169 if files[0] != str(state['page']):
170 debug_write(['Hack this is scrolling within a folder.\n'])
171 state['page'] = files[0]
172 return query, path
174 # The only remaining options are exiting a folder or this is a
175 # erroneous second request.
177 # 6. this an extraneous request
178 # this came within a second of a valid request; just use that
179 # request.
180 if (int(time.time()) - state['time']) <= 1:
181 debug_write(['Hack erroneous request, send a 302 error', '\n'])
182 return None, path
184 # 7. this is a request to exit a folder
185 # this request came by itself; it must be to exit a folder
186 else:
187 debug_write(['Hack over 1 second,',
188 'must be request to exit folder\n'])
189 path.pop()
190 state['query'] = {'Command': query['Command'],
191 'SortOrder': query['SortOrder'],
192 'ItemCount': query['ItemCount'],
193 'Filter': query['Filter'],
194 'Container': ['/'.join(path)]}
195 return None, path
197 # just in case we missed something.
198 debug_write(['Hack ERROR, should not have made it here. ',
199 'Trying to recover.\n'])
200 return state['query'], path
202 def send_file(self, handler, container, name):
203 if handler.headers.getheader('Range') and \
204 handler.headers.getheader('Range') != 'bytes=0-':
205 handler.send_response(206)
206 handler.send_header('Connection', 'close')
207 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
208 handler.send_header('Transfer-Encoding', 'chunked')
209 handler.end_headers()
210 handler.wfile.write("\x30\x0D\x0A")
211 return
213 tsn = handler.headers.getheader('tsn', '')
215 o = urlparse("http://fake.host" + handler.path)
216 path = unquote(o[2])
217 handler.send_response(200)
218 handler.end_headers()
219 transcode.output_video(container['path'] + path[len(name) + 1:],
220 handler.wfile, tsn)
222 def __isdir(self, full_path):
223 return os.path.isdir(full_path)
225 def __duration(self, full_path):
226 return transcode.video_info(full_path)[4]
228 def __est_size(self, full_path, tsn = ''):
229 # Size is estimated by taking audio and video bit rate adding 2%
231 if transcode.tivo_compatable(full_path, tsn):
232 # Is TiVo-compatible mpeg2
233 return int(os.stat(full_path).st_size)
234 else:
235 # Must be re-encoded
236 audioBPS = config.strtod(config.getAudioBR(tsn))
237 videoBPS = config.strtod(config.getVideoBR(tsn))
238 bitrate = audioBPS + videoBPS
239 return int((self.__duration(full_path) / 1000) *
240 (bitrate * 1.02 / 8))
242 def __getMetadataFromTxt(self, full_path):
243 metadata = {}
245 default_file = os.path.join(os.path.split(full_path)[0], 'default.txt')
246 description_file = full_path + '.txt'
248 metadata.update(self.__getMetadataFromFile(default_file))
249 metadata.update(self.__getMetadataFromFile(description_file))
251 return metadata
253 def __getMetadataFromFile(self, file):
254 metadata = {}
256 if os.path.exists(file):
257 for line in open(file):
258 if line.strip().startswith('#'):
259 continue
260 if not ':' in line:
261 continue
263 key, value = line.split(':', 1)
264 key = key.strip()
265 value = value.strip()
267 if key.startswith('v'):
268 if key in metadata:
269 metadata[key].append(value)
270 else:
271 metadata[key] = [value]
272 else:
273 metadata[key] = value
275 return metadata
277 def __metadata_basic(self, full_path):
278 metadata = {}
280 base_path, title = os.path.split(full_path)
281 originalAirDate = datetime.fromtimestamp(os.stat(full_path).st_ctime)
283 metadata['title'] = '.'.join(title.split('.')[:-1])
284 metadata['seriesTitle'] = metadata['title'] # default to the filename
285 metadata['originalAirDate'] = originalAirDate.isoformat()
287 metadata.update(self.__getMetadataFromTxt(full_path))
289 return metadata
291 def __metadata_full(self, full_path, tsn=''):
292 metadata = {}
293 metadata.update(self.__metadata_basic(full_path))
295 now = datetime.utcnow()
297 duration = self.__duration(full_path)
298 duration_delta = timedelta(milliseconds = duration)
300 metadata['time'] = now.isoformat()
301 metadata['startTime'] = now.isoformat()
302 metadata['stopTime'] = (now + duration_delta).isoformat()
304 metadata.update( self.__getMetadataFromTxt(full_path) )
306 metadata['size'] = self.__est_size(full_path, tsn)
307 metadata['duration'] = duration
309 min = duration_delta.seconds / 60
310 sec = duration_delta.seconds % 60
311 hours = min / 60
312 min = min % 60
313 metadata['iso_duration'] = 'P' + str(duration_delta.days) + \
314 'DT' + str(hours) + 'H' + str(min) + \
315 'M' + str(sec) + 'S'
316 return metadata
318 def QueryContainer(self, handler, query):
319 tsn = handler.headers.getheader('tsn', '')
320 subcname = query['Container'][0]
322 # If you are running 8.3 software you want to enable hack83
323 # in the config file
325 if hack83:
326 print '=' * 73
327 query, hackPath = self.hack(handler, query, subcname)
328 hackPath = '/'.join(hackPath)
329 print 'Tivo said:', subcname, '|| Hack said:', hackPath
330 debug_write(['Hack Tivo said: ', subcname, ' || Hack said: ',
331 hackPath, '\n'])
332 subcname = hackPath
334 if not query:
335 debug_write(['Hack sending 302 redirect page', '\n'])
336 handler.send_response(302)
337 handler.send_header('Location ', 'http://' +
338 handler.headers.getheader('host') +
339 '/TiVoConnect?Command=QueryContainer&' +
340 'AnchorItem=Hack8.3&Container=' + hackPath)
341 handler.end_headers()
342 return
344 # End hack mess
346 cname = subcname.split('/')[0]
348 if not handler.server.containers.has_key(cname) or \
349 not self.get_local_path(handler, query):
350 handler.send_response(404)
351 handler.end_headers()
352 return
354 container = handler.server.containers[cname]
355 precache = container.get('precache', 'False').lower() == 'true'
357 files, total, start = self.get_files(handler, query,
358 self.video_file_filter)
360 videos = []
361 local_base_path = self.get_local_base_path(handler, query)
362 for file in files:
363 video = VideoDetails()
364 video['name'] = os.path.split(file)[1]
365 video['path'] = file
366 video['part_path'] = file.replace(local_base_path, '', 1)
367 video['title'] = os.path.split(file)[1]
368 video['is_dir'] = self.__isdir(file)
369 if video['is_dir']:
370 video['small_path'] = subcname + '/' + video['name']
371 else:
372 if precache or len(files) == 1 or file in transcode.info_cache:
373 video['valid'] = transcode.supported_format(file)
374 if video['valid']:
375 video.update(self.__metadata_full(file, tsn))
376 else:
377 video['valid'] = True
378 video.update(self.__metadata_basic(file))
380 videos.append(video)
382 handler.send_response(200)
383 handler.end_headers()
384 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
385 t.container = cname
386 t.name = subcname
387 t.total = total
388 t.start = start
389 t.videos = videos
390 t.quote = quote
391 t.escape = escape
392 t.crc = zlib.crc32
393 t.guid = config.getGUID()
394 handler.wfile.write(t)
396 def TVBusQuery(self, handler, query):
397 tsn = handler.headers.getheader('tsn', '')
398 file = query['File'][0]
399 path = self.get_local_path(handler, query)
400 file_path = path + file
402 file_info = VideoDetails()
403 file_info['valid'] = transcode.supported_format(file_path)
404 if file_info['valid']:
405 file_info.update(self.__metadata_full(file_path, tsn))
407 handler.send_response(200)
408 handler.end_headers()
409 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
410 t.video = file_info
411 t.escape = escape
412 handler.wfile.write(t)
414 class VideoDetails(DictMixin):
416 def __init__(self, d=None):
417 if d:
418 self.d = d
419 else:
420 self.d = {}
422 def __getitem__(self, key):
423 if key not in self.d:
424 self.d[key] = self.default(key)
425 return self.d[key]
427 def __contains__(self, key):
428 return True
430 def __setitem__(self, key, value):
431 self.d[key] = value
433 def __delitem__(self):
434 del self.d[key]
436 def keys(self):
437 return self.d.keys()
439 def __iter__(self):
440 return self.d.__iter__()
442 def iteritems(self):
443 return self.d.iteritems()
445 def default(self, key):
446 defaults = {
447 'showingBits' : '0',
448 'episodeNumber' : '0',
449 'displayMajorNumber' : '0',
450 'displayMinorNumber' : '0',
451 'isEpisode' : 'true',
452 'colorCode' : ('COLOR', '4'),
453 'showType' : ('SERIES', '5'),
454 'tvRating' : ('NR', '7')
456 if key in defaults:
457 return defaults[key]
458 elif key.startswith('v'):
459 return []
460 else:
461 return ''