Convert video_info to return a dict rather than list
[pyTivo/krkeegan.git] / plugins / video / video.py
blobfe236f562642bcabd065bd93ea8b1ee469d24f68
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
11 import mind
12 import logging
14 SCRIPTDIR = os.path.dirname(__file__)
16 CLASS_NAME = 'Video'
18 extfile = os.path.join(SCRIPTDIR, 'video.ext')
19 try:
20 extensions = file(extfile).read().split()
21 except:
22 extensions = None
24 if config.getHack83():
25 logging.getLogger('pyTivo.hack83').info('Hack83 is enabled.')
27 class Video(Plugin):
29 CONTENT_TYPE = 'x-container/tivo-videos'
31 # Used for 8.3's broken requests
32 count = 0
33 request_history = {}
35 def pre_cache(self, full_path):
36 if Video.video_file_filter(self, full_path):
37 transcode.supported_format(full_path)
39 def video_file_filter(self, full_path, type=None):
40 if os.path.isdir(full_path):
41 return True
42 if extensions:
43 return os.path.splitext(full_path)[1].lower() in extensions
44 else:
45 return transcode.supported_format(full_path)
47 def hack(self, handler, query, subcname):
48 logger = logging.getLogger('pyTivo.hack83')
49 logger.debug('new request ------------------------')
50 logger.debug('TiVo request is: \n%s' % query)
51 queryAnchor = ''
52 rightAnchor = ''
53 leftAnchor = ''
54 tsn = handler.headers.getheader('tsn', '')
56 # not a tivo
57 if not tsn:
58 logger.debug('this was not a TiVo request. Using default tsn.')
59 tsn = '123456789'
61 # this breaks up the anchor item request into seperate parts
62 if 'AnchorItem' in query and query['AnchorItem'] != ['Hack8.3']:
63 queryAnchor = urllib.unquote_plus(''.join(query['AnchorItem']))
64 if queryAnchor.find('Container=') >= 0:
65 # This is a folder
66 queryAnchor = queryAnchor.split('Container=')[-1]
67 else:
68 # This is a file
69 queryAnchor = queryAnchor.split('/', 1)[-1]
70 leftAnchor, rightAnchor = queryAnchor.rsplit('/', 1)
71 logger.debug('queryAnchor:%s \n leftAnchor:%s\n rightAnchor: %s' %
72 (queryAnchor, leftAnchor, rightAnchor))
73 try:
74 path, state = self.request_history[tsn]
75 except KeyError:
76 # Never seen this tsn, starting new history
77 logger.debug('New TSN.')
78 path = []
79 state = {}
80 self.request_history[tsn] = (path, state)
81 state['query'] = query
82 state['page'] = ''
83 state['time'] = int(time.time()) + 1000
85 logger.debug('our saved request is: \n%s' % state['query'])
87 current_folder = subcname.split('/')[-1]
89 # Begin figuring out what the request TiVo sent us means
90 # There are 7 options that can occur
92 # 1. at the root - This request is always accurate
93 if len(subcname.split('/')) == 1:
94 logger.debug('we are at the root. Saving query, Clearing state[page].')
95 path[:] = [current_folder]
96 state['query'] = query
97 state['page'] = ''
98 return query, path
100 # 2. entering a new folder
101 # If there is no AnchorItem in the request then we must be
102 # entering a new folder.
103 if 'AnchorItem' not in query:
104 logger.debug('we are entering a new folder. Saving query, setting time, setting state[page].')
105 path[:] = subcname.split('/')
106 state['query'] = query
107 state['time'] = int(time.time())
108 files, total, start = self.get_files(handler, query,
109 self.video_file_filter)
110 if files:
111 state['page'] = files[0]
112 else:
113 state['page'] = ''
114 return query, path
116 # 3. Request a page after pyTivo sent a 302 code
117 # we know this is the proper page
118 if ''.join(query['AnchorItem']) == 'Hack8.3':
119 logger.debug('requested page from 302 code. Returning saved query.')
120 return state['query'], path
122 # 4. this is a request for a file
123 if 'ItemCount' in query and int(''.join(query['ItemCount'])) == 1:
124 logger.debug('requested a file')
125 # Everything in this request is right except the container
126 query['Container'] = ['/'.join(path)]
127 state['page'] = ''
128 return query, path
130 # All remaining requests could be a second erroneous request for
131 # each of the following we will pause to see if a correct
132 # request is coming right behind it.
134 # Sleep just in case the erroneous request came first this
135 # allows a proper request to be processed first
136 logger.debug('maybe erroneous request, sleeping.')
137 time.sleep(.25)
139 # 5. scrolling in a folder
140 # This could be a request to exit a folder or scroll up or down
141 # within the folder
142 # First we have to figure out if we are scrolling
143 if 'AnchorOffset' in query:
144 logger.debug('Anchor offset was in query. leftAnchor needs to match %s' % '/'.join(path))
145 if leftAnchor == str('/'.join(path)):
146 logger.debug('leftAnchor matched.')
147 query['Container'] = ['/'.join(path)]
148 files, total, start = self.get_files(handler, query,
149 self.video_file_filter)
150 logger.debug('saved page is=%s top returned file is= %s' % (state['page'], files[0]))
151 # If the first file returned equals the top of the page
152 # then we haven't scrolled pages
153 if files[0] != str(state['page']):
154 logger.debug('this is scrolling within a folder.')
155 state['page'] = files[0]
156 return query, path
158 # The only remaining options are exiting a folder or this is a
159 # erroneous second request.
161 # 6. this an extraneous request
162 # this came within a second of a valid request; just use that
163 # request.
164 if (int(time.time()) - state['time']) <= 1:
165 logger.debug('erroneous request, send a 302 error')
166 return None, path
168 # 7. this is a request to exit a folder
169 # this request came by itself; it must be to exit a folder
170 else:
171 logger.debug('over 1 second must be request to exit folder')
172 path.pop()
173 state['query'] = {'Command': query['Command'],
174 'SortOrder': query['SortOrder'],
175 'ItemCount': query['ItemCount'],
176 'Filter': query['Filter'],
177 'Container': ['/'.join(path)]}
178 files, total, start = self.get_files(handler, state['query'],
179 self.video_file_filter)
180 if files:
181 state['page'] = files[0]
182 else:
183 state['page'] = ''
184 return None, path
186 # just in case we missed something.
187 logger.debug('ERROR, should not have made it here Trying to recover.')
188 return state['query'], path
190 def send_file(self, handler, container, name):
191 if handler.headers.getheader('Range') and \
192 handler.headers.getheader('Range') != 'bytes=0-':
193 handler.send_response(206)
194 handler.send_header('Connection', 'close')
195 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
196 handler.send_header('Transfer-Encoding', 'chunked')
197 handler.end_headers()
198 handler.wfile.write("\x30\x0D\x0A")
199 return
201 tsn = handler.headers.getheader('tsn', '')
203 o = urlparse("http://fake.host" + handler.path)
204 path = unquote(o[2])
205 handler.send_response(200)
206 handler.end_headers()
207 transcode.output_video(container['path'] + path[len(name) + 1:],
208 handler.wfile, tsn)
210 def __isdir(self, full_path):
211 return os.path.isdir(full_path)
213 def __duration(self, full_path):
214 return transcode.video_info(full_path)['millisecs']
216 def __total_items(self, full_path):
217 count = 0
218 try:
219 for file in os.listdir(full_path):
220 if file.startswith('.'):
221 continue
222 file = os.path.join(full_path, file)
223 if os.path.isdir(file):
224 count += 1
225 elif extensions:
226 if os.path.splitext(file)[1].lower() in extensions:
227 count += 1
228 elif file in transcode.info_cache:
229 if transcode.supported_format(file):
230 count += 1
231 except:
232 pass
233 return count
235 def __est_size(self, full_path, tsn = ''):
236 # Size is estimated by taking audio and video bit rate adding 2%
238 if transcode.tivo_compatable(full_path, tsn):
239 # Is TiVo-compatible mpeg2
240 return int(os.stat(full_path).st_size)
241 else:
242 # Must be re-encoded
243 if config.getAudioCodec(tsn) == None:
244 audioBPS = config.getMaxAudioBR(tsn)*1000
245 else:
246 audioBPS = config.strtod(config.getAudioBR(tsn))
247 videoBPS = config.strtod(transcode.select_videostr(full_path, tsn))
248 bitrate = audioBPS + videoBPS
249 return int((self.__duration(full_path) / 1000) *
250 (bitrate * 1.02 / 8))
252 def __getMetadataFromTxt(self, full_path):
253 metadata = {}
255 default_meta = os.path.join(os.path.split(full_path)[0], 'default.txt')
256 standard_meta = full_path + '.txt'
257 subdir_meta = os.path.join(os.path.dirname(full_path), '.meta',
258 os.path.basename(full_path)) + '.txt'
260 for metafile in (default_meta, standard_meta, subdir_meta):
261 metadata.update(self.__getMetadataFromFile(metafile))
263 return metadata
265 def __getMetadataFromFile(self, file):
266 metadata = {}
268 if os.path.exists(file):
269 for line in open(file):
270 if line.strip().startswith('#'):
271 continue
272 if not ':' in line:
273 continue
275 key, value = line.split(':', 1)
276 key = key.strip()
277 value = value.strip()
279 if key.startswith('v'):
280 if key in metadata:
281 metadata[key].append(value)
282 else:
283 metadata[key] = [value]
284 else:
285 metadata[key] = value
287 return metadata
289 def metadata_basic(self, full_path):
290 metadata = {}
292 base_path, title = os.path.split(full_path)
293 ctime = os.stat(full_path).st_ctime
294 if (ctime < 0): ctime = 0
295 originalAirDate = datetime.fromtimestamp(ctime)
297 metadata['title'] = '.'.join(title.split('.')[:-1])
298 metadata['seriesTitle'] = metadata['title'] # default to the filename
299 metadata['originalAirDate'] = originalAirDate.isoformat()
301 metadata.update(self.__getMetadataFromTxt(full_path))
303 return metadata
305 def metadata_full(self, full_path, tsn=''):
306 metadata = {}
308 now = datetime.utcnow()
310 duration = self.__duration(full_path)
311 duration_delta = timedelta(milliseconds = duration)
313 metadata['time'] = now.isoformat()
314 metadata['startTime'] = now.isoformat()
315 metadata['stopTime'] = (now + duration_delta).isoformat()
316 metadata['size'] = self.__est_size(full_path, tsn)
317 metadata['duration'] = duration
318 metadata.update(self.metadata_basic(full_path))
320 min = duration_delta.seconds / 60
321 sec = duration_delta.seconds % 60
322 hours = min / 60
323 min = min % 60
324 metadata['iso_duration'] = 'P' + str(duration_delta.days) + \
325 'DT' + str(hours) + 'H' + str(min) + \
326 'M' + str(sec) + 'S'
327 return metadata
329 def QueryContainer(self, handler, query):
330 tsn = handler.headers.getheader('tsn', '')
331 subcname = query['Container'][0]
333 # If you are running 8.3 software you want to enable hack83
334 # in the config file
335 if config.getHack83():
336 logger = logging.getLogger('pyTivo.hack83')
337 logger.debug('=' * 73)
338 query, hackPath = self.hack(handler, query, subcname)
339 hackPath = '/'.join(hackPath)
340 logger.debug('Tivo said: %s || Hack said: %s' % (subcname, hackPath))
341 subcname = hackPath
343 if not query:
344 logger.debug('sending 302 redirect page')
345 handler.send_response(302)
346 handler.send_header('Location ', 'http://' +
347 handler.headers.getheader('host') +
348 '/TiVoConnect?Command=QueryContainer&' +
349 'AnchorItem=Hack8.3&Container=' + quote(hackPath))
350 handler.end_headers()
351 return
353 # End hack mess
355 cname = subcname.split('/')[0]
357 if not handler.server.containers.has_key(cname) or \
358 not self.get_local_path(handler, query):
359 handler.send_response(404)
360 handler.end_headers()
361 return
363 container = handler.server.containers[cname]
364 precache = container.get('precache', 'False').lower() == 'true'
366 files, total, start = self.get_files(handler, query,
367 self.video_file_filter)
369 videos = []
370 local_base_path = self.get_local_base_path(handler, query)
371 for file in files:
372 mtime = os.stat(file).st_mtime
373 if (mtime < 0): mtime = 0
374 mtime = datetime.fromtimestamp(mtime)
375 video = VideoDetails()
376 video['captureDate'] = hex(int(time.mktime(mtime.timetuple())))
377 video['name'] = os.path.split(file)[1]
378 video['path'] = file
379 video['part_path'] = file.replace(local_base_path, '', 1)
380 if not video['part_path'].startswith(os.path.sep):
381 video['part_path'] = os.path.sep + video['part_path']
382 video['title'] = os.path.split(file)[1]
383 video['is_dir'] = self.__isdir(file)
384 if video['is_dir']:
385 video['small_path'] = subcname + '/' + video['name']
386 video['total_items'] = self.__total_items(file)
387 else:
388 if precache or len(files) == 1 or file in transcode.info_cache:
389 video['valid'] = transcode.supported_format(file)
390 if video['valid']:
391 video.update(self.metadata_full(file, tsn))
392 else:
393 video['valid'] = True
394 video.update(self.metadata_basic(file))
396 videos.append(video)
398 handler.send_response(200)
399 handler.end_headers()
400 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
401 t.container = cname
402 t.name = subcname
403 t.total = total
404 t.start = start
405 t.videos = videos
406 t.quote = quote
407 t.escape = escape
408 t.crc = zlib.crc32
409 t.guid = config.getGUID()
410 t.tivos = handler.tivos
411 handler.wfile.write(t)
413 def TVBusQuery(self, handler, query):
414 tsn = handler.headers.getheader('tsn', '')
415 file = query['File'][0]
416 path = self.get_local_path(handler, query)
417 file_path = path + file
419 file_info = VideoDetails()
420 file_info['valid'] = transcode.supported_format(file_path)
421 if file_info['valid']:
422 file_info.update(self.metadata_full(file_path, tsn))
424 handler.send_response(200)
425 handler.end_headers()
426 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
427 t.video = file_info
428 t.escape = escape
429 handler.wfile.write(t)
431 def XSL(self, handler, query):
432 file = open(os.path.join(SCRIPTDIR, 'templates', 'container.xsl'))
433 handler.send_response(200)
434 handler.end_headers()
435 handler.wfile.write(file.read())
438 def Push(self, handler, query):
439 file = unquote(query['File'][0])
440 tsn = query['tsn'][0]
441 path = self.get_local_path(handler, query)
442 file_path = path + file
444 file_info = VideoDetails()
445 file_info['valid'] = transcode.supported_format(file_path)
446 if file_info['valid']:
447 file_info.update(self.metadata_full(file_path, tsn))
449 import socket
450 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
451 s.connect(('tivo.com',123))
452 ip = s.getsockname()[0]
453 container = quote(query['Container'][0].split('/')[0])
454 port = config.getPort()
456 url = 'http://%s:%s/%s%s' % (ip, port, container, quote(file))
458 try:
459 m = mind.getMind()
460 m.pushVideo(
461 tsn = tsn,
462 url = url,
463 description = file_info['description'],
464 duration = file_info['duration'] / 1000,
465 size = file_info['size'],
466 title = file_info['title'],
467 subtitle = file_info['name'])
468 except Exception, e:
469 import traceback
470 handler.send_response(500)
471 handler.end_headers()
472 handler.wfile.write('%s\n\n%s' % (e, traceback.format_exc() ))
473 raise
475 referer = handler.headers.getheader('Referer')
476 handler.send_response(302)
477 handler.send_header('Location', referer)
478 handler.end_headers()
481 class VideoDetails(DictMixin):
483 def __init__(self, d=None):
484 if d:
485 self.d = d
486 else:
487 self.d = {}
489 def __getitem__(self, key):
490 if key not in self.d:
491 self.d[key] = self.default(key)
492 return self.d[key]
494 def __contains__(self, key):
495 return True
497 def __setitem__(self, key, value):
498 self.d[key] = value
500 def __delitem__(self):
501 del self.d[key]
503 def keys(self):
504 return self.d.keys()
506 def __iter__(self):
507 return self.d.__iter__()
509 def iteritems(self):
510 return self.d.iteritems()
512 def default(self, key):
513 defaults = {
514 'showingBits' : '0',
515 'episodeNumber' : '0',
516 'displayMajorNumber' : '0',
517 'displayMinorNumber' : '0',
518 'isEpisode' : 'true',
519 'colorCode' : ('COLOR', '4'),
520 'showType' : ('SERIES', '5'),
521 'tvRating' : ('NR', '7')
523 if key in defaults:
524 return defaults[key]
525 elif key.startswith('v'):
526 return []
527 else:
528 return ''