Fixed imports in video
[pyTivo.git] / pyTivo / plugins / video / video.py
blobf9b92467fbc91df2d90ed907a8fa4eeab6417c78
1 import transcode, os, socket, re, urllib, zlib
2 from Cheetah.Template import Template
3 from pyTivo.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 pyTivo.config as config
10 import time
11 import pyTivo.mind as mind
12 from pyTivo.debug import debug_write, fn_attr
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 debug_write(__name__, fn_attr(), ['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 debug_write(__name__, fn_attr(), ['new request ------------------------'])
49 debug_write(__name__, fn_attr(), ['TiVo request is: \n', query])
50 queryAnchor = ''
51 rightAnchor = ''
52 leftAnchor = ''
53 tsn = handler.headers.getheader('tsn', '')
55 # not a tivo
56 if not tsn:
57 debug_write(__name__, fn_attr(), ['this was not a TiVo request.',
58 '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 debug_write(__name__, fn_attr(), ['queryAnchor: ', queryAnchor,
72 ' leftAnchor: ', leftAnchor,
73 ' rightAnchor: ', rightAnchor])
74 try:
75 path, state = self.request_history[tsn]
76 except KeyError:
77 # Never seen this tsn, starting new history
78 debug_write(__name__, fn_attr(), ['New TSN.'])
79 path = []
80 state = {}
81 self.request_history[tsn] = (path, state)
82 state['query'] = query
83 state['page'] = ''
84 state['time'] = int(time.time()) + 1000
86 debug_write(__name__, fn_attr(), ['our saved request is: \n', state['query']])
88 current_folder = subcname.split('/')[-1]
90 # Begin figuring out what the request TiVo sent us means
91 # There are 7 options that can occur
93 # 1. at the root - This request is always accurate
94 if len(subcname.split('/')) == 1:
95 debug_write(__name__, fn_attr(), ['we are at the root.',
96 'Saving query, Clearing state[page].'])
97 path[:] = [current_folder]
98 state['query'] = query
99 state['page'] = ''
100 return query, path
102 # 2. entering a new folder
103 # If there is no AnchorItem in the request then we must be
104 # entering a new folder.
105 if 'AnchorItem' not in query:
106 debug_write(__name__, fn_attr(), ['we are entering a new folder.',
107 'Saving query, setting time, setting state[page].'])
108 path[:] = subcname.split('/')
109 state['query'] = query
110 state['time'] = int(time.time())
111 files, total, start = self.get_files(handler, query,
112 self.video_file_filter)
113 if files:
114 state['page'] = files[0]
115 else:
116 state['page'] = ''
117 return query, path
119 # 3. Request a page after pyTivo sent a 302 code
120 # we know this is the proper page
121 if ''.join(query['AnchorItem']) == 'Hack8.3':
122 debug_write(__name__, fn_attr(), ['requested page from 302 code.',
123 'Returning saved query.'])
124 return state['query'], path
126 # 4. this is a request for a file
127 if 'ItemCount' in query and int(''.join(query['ItemCount'])) == 1:
128 debug_write(__name__, fn_attr(), ['requested a file'])
129 # Everything in this request is right except the container
130 query['Container'] = ['/'.join(path)]
131 state['page'] = ''
132 return query, path
134 # All remaining requests could be a second erroneous request for
135 # each of the following we will pause to see if a correct
136 # request is coming right behind it.
138 # Sleep just in case the erroneous request came first this
139 # allows a proper request to be processed first
140 debug_write(__name__, fn_attr(), ['maybe erroneous request, sleeping.'])
141 time.sleep(.25)
143 # 5. scrolling in a folder
144 # This could be a request to exit a folder or scroll up or down
145 # within the folder
146 # First we have to figure out if we are scrolling
147 if 'AnchorOffset' in query:
148 debug_write(__name__, fn_attr(), ['Anchor offset was in query.',
149 'leftAnchor needs to match ', '/'.join(path)])
150 if leftAnchor == str('/'.join(path)):
151 debug_write(__name__, fn_attr(), ['leftAnchor matched.'])
152 query['Container'] = ['/'.join(path)]
153 files, total, start = self.get_files(handler, query,
154 self.video_file_filter)
155 debug_write(__name__, fn_attr(), ['saved page is= ', state['page'],
156 ' top returned file is= ', files[0]])
157 # If the first file returned equals the top of the page
158 # then we haven't scrolled pages
159 if files[0] != str(state['page']):
160 debug_write(__name__, fn_attr(), ['this is scrolling within a folder.'])
161 state['page'] = files[0]
162 return query, path
164 # The only remaining options are exiting a folder or this is a
165 # erroneous second request.
167 # 6. this an extraneous request
168 # this came within a second of a valid request; just use that
169 # request.
170 if (int(time.time()) - state['time']) <= 1:
171 debug_write(__name__, fn_attr(), ['erroneous request, send a 302 error'])
172 return None, path
174 # 7. this is a request to exit a folder
175 # this request came by itself; it must be to exit a folder
176 else:
177 debug_write(__name__, fn_attr(), ['over 1 second,',
178 'must be request to exit folder'])
179 path.pop()
180 state['query'] = {'Command': query['Command'],
181 'SortOrder': query['SortOrder'],
182 'ItemCount': query['ItemCount'],
183 'Filter': query['Filter'],
184 'Container': ['/'.join(path)]}
185 return None, path
187 # just in case we missed something.
188 debug_write(__name__, fn_attr(), ['ERROR, should not have made it here. ',
189 'Trying to recover.'])
190 return state['query'], path
192 def send_file(self, handler, container, name):
193 if handler.headers.getheader('Range') and \
194 handler.headers.getheader('Range') != 'bytes=0-':
195 handler.send_response(206)
196 handler.send_header('Connection', 'close')
197 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
198 handler.send_header('Transfer-Encoding', 'chunked')
199 handler.end_headers()
200 handler.wfile.write("\x30\x0D\x0A")
201 return
203 tsn = handler.headers.getheader('tsn', '')
205 o = urlparse("http://fake.host" + handler.path)
206 path = unquote(o[2])
207 handler.send_response(200)
208 handler.end_headers()
209 transcode.output_video(container['path'] + path[len(name) + 1:],
210 handler.wfile, tsn)
212 def __isdir(self, full_path):
213 return os.path.isdir(full_path)
215 def __duration(self, full_path):
216 return transcode.video_info(full_path)[4]
218 def __total_items(self, full_path):
219 count = 0
220 try:
221 for file in os.listdir(full_path):
222 if file.startswith('.'):
223 continue
224 file = os.path.join(full_path, file)
225 if os.path.isdir(file):
226 count += 1
227 elif extensions:
228 if os.path.splitext(file)[1].lower() in extensions:
229 count += 1
230 elif file in transcode.info_cache:
231 if transcode.supported_format(file):
232 count += 1
233 except:
234 pass
235 return count
237 def __est_size(self, full_path, tsn = ''):
238 # Size is estimated by taking audio and video bit rate adding 2%
240 if transcode.tivo_compatable(full_path, tsn):
241 # Is TiVo-compatible mpeg2
242 return int(os.stat(full_path).st_size)
243 else:
244 # Must be re-encoded
245 if config.getAudioCodec(tsn) == None:
246 audioBPS = config.getMaxAudioBR(tsn)*1000
247 else:
248 audioBPS = config.strtod(config.getAudioBR(tsn))
249 videoBPS = config.strtod(config.getVideoBR(tsn))
250 bitrate = audioBPS + videoBPS
251 return int((self.__duration(full_path) / 1000) *
252 (bitrate * 1.02 / 8))
254 def __getMetadataFromTxt(self, full_path):
255 metadata = {}
257 default_meta = os.path.join(os.path.split(full_path)[0], 'default.txt')
258 standard_meta = full_path + '.txt'
259 subdir_meta = os.path.join(os.path.dirname(full_path), '.meta',
260 os.path.basename(full_path)) + '.txt'
262 for metafile in (default_meta, standard_meta, subdir_meta):
263 metadata.update(self.__getMetadataFromFile(metafile))
265 return metadata
267 def __getMetadataFromFile(self, file):
268 metadata = {}
270 if os.path.exists(file):
271 for line in open(file):
272 if line.strip().startswith('#'):
273 continue
274 if not ':' in line:
275 continue
277 key, value = line.split(':', 1)
278 key = key.strip()
279 value = value.strip()
281 if key.startswith('v'):
282 if key in metadata:
283 metadata[key].append(value)
284 else:
285 metadata[key] = [value]
286 else:
287 metadata[key] = value
289 return metadata
291 def __metadata_basic(self, full_path):
292 metadata = {}
294 base_path, title = os.path.split(full_path)
295 originalAirDate = datetime.fromtimestamp(os.stat(full_path).st_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 = {}
307 metadata.update(self.__metadata_basic(full_path))
309 now = datetime.utcnow()
311 duration = self.__duration(full_path)
312 duration_delta = timedelta(milliseconds = duration)
314 metadata['time'] = now.isoformat()
315 metadata['startTime'] = now.isoformat()
316 metadata['stopTime'] = (now + duration_delta).isoformat()
317 metadata['size'] = self.__est_size(full_path, tsn)
318 metadata['duration'] = duration
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 print '=' * 73
337 query, hackPath = self.hack(handler, query, subcname)
338 hackPath = '/'.join(hackPath)
339 print 'Tivo said:', subcname, '|| Hack said:', hackPath
340 debug_write(__name__, fn_attr(), ['Tivo said: ', subcname, ' || Hack said: ',
341 hackPath])
342 subcname = hackPath
344 if not query:
345 debug_write(__name__, fn_attr(), ['sending 302 redirect page'])
346 handler.send_response(302)
347 handler.send_header('Location ', 'http://' +
348 handler.headers.getheader('host') +
349 '/TiVoConnect?Command=QueryContainer&' +
350 'AnchorItem=Hack8.3&Container=' + hackPath)
351 handler.end_headers()
352 return
354 # End hack mess
356 cname = subcname.split('/')[0]
358 if not handler.server.containers.has_key(cname) or \
359 not self.get_local_path(handler, query):
360 handler.send_response(404)
361 handler.end_headers()
362 return
364 container = handler.server.containers[cname]
365 precache = container.get('precache', 'False').lower() == 'true'
367 files, total, start = self.get_files(handler, query,
368 self.video_file_filter)
370 videos = []
371 local_base_path = self.get_local_base_path(handler, query)
372 for file in files:
373 mtime = datetime.fromtimestamp(os.stat(file).st_mtime)
374 video = VideoDetails()
375 video['captureDate'] = hex(int(time.mktime(mtime.timetuple())))
376 video['name'] = os.path.split(file)[1]
377 video['path'] = file
378 video['part_path'] = file.replace(local_base_path, '', 1)
379 video['title'] = os.path.split(file)[1]
380 video['is_dir'] = self.__isdir(file)
381 if video['is_dir']:
382 video['small_path'] = subcname + '/' + video['name']
383 video['total_items'] = self.__total_items(file)
384 else:
385 if precache or len(files) == 1 or file in transcode.info_cache:
386 video['valid'] = transcode.supported_format(file)
387 if video['valid']:
388 video.update(self.__metadata_full(file, tsn))
389 else:
390 video['valid'] = True
391 video.update(self.__metadata_basic(file))
393 videos.append(video)
395 handler.send_response(200)
396 handler.end_headers()
397 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
398 t.container = cname
399 t.name = subcname
400 t.total = total
401 t.start = start
402 t.videos = videos
403 t.quote = quote
404 t.escape = escape
405 t.crc = zlib.crc32
406 t.guid = config.getGUID()
407 t.tivos = handler.tivos
408 handler.wfile.write(t)
410 def TVBusQuery(self, handler, query):
411 tsn = handler.headers.getheader('tsn', '')
412 file = query['File'][0]
413 path = self.get_local_path(handler, query)
414 file_path = path + file
416 file_info = VideoDetails()
417 file_info['valid'] = transcode.supported_format(file_path)
418 if file_info['valid']:
419 file_info.update(self.__metadata_full(file_path, tsn))
421 handler.send_response(200)
422 handler.end_headers()
423 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
424 t.video = file_info
425 t.escape = escape
426 handler.wfile.write(t)
428 def XSL(self, handler, query):
429 file = open(os.path.join(SCRIPTDIR, 'templates', 'container.xsl'))
430 handler.send_response(200)
431 handler.end_headers()
432 handler.wfile.write(file.read())
435 def Push(self, handler, query):
436 file = unquote(query['File'][0])
437 tsn = query['tsn'][0]
438 path = self.get_local_path(handler, query)
439 file_path = path + file
441 file_info = VideoDetails()
442 file_info['valid'] = transcode.supported_format(file_path)
443 if file_info['valid']:
444 file_info.update(self.__metadata_full(file_path, tsn))
446 import socket
447 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
448 s.connect(('tivo.com',123))
449 ip = s.getsockname()[0]
450 container = quote(query['Container'][0].split('/')[0])
451 port = config.getPort()
453 url = 'http://%s:%s/%s%s' % (ip, port, container, quote(file))
455 print 'tsn', tsn
456 print 'url', url
457 print query
459 username = config.getTivoUsername()
460 password = config.getTivoPassword()
462 if not username or not password:
463 raise Exception("tivo_username and tivo_password required")
465 try:
466 m = mind.Mind(username, password, True)
467 m.pushVideo(
468 tsn = tsn,
469 url = url,
470 description = file_info['description'],
471 duration = file_info['duration'] / 1000,
472 size = file_info['size'],
473 title = file_info['title'],
474 subtitle = file_info['name'])
475 except Exception, e:
476 import traceback
477 handler.send_response(500)
478 handler.end_headers()
479 handler.wfile.write('%s\n\n%s' % (e, traceback.format_exc() ))
480 raise
482 referer = handler.headers.getheader('Referer')
483 handler.send_response(302)
484 handler.send_header('Location', referer)
485 handler.end_headers()
488 class VideoDetails(DictMixin):
490 def __init__(self, d=None):
491 if d:
492 self.d = d
493 else:
494 self.d = {}
496 def __getitem__(self, key):
497 if key not in self.d:
498 self.d[key] = self.default(key)
499 return self.d[key]
501 def __contains__(self, key):
502 return True
504 def __setitem__(self, key, value):
505 self.d[key] = value
507 def __delitem__(self):
508 del self.d[key]
510 def keys(self):
511 return self.d.keys()
513 def __iter__(self):
514 return self.d.__iter__()
516 def iteritems(self):
517 return self.d.iteritems()
519 def default(self, key):
520 defaults = {
521 'showingBits' : '0',
522 'episodeNumber' : '0',
523 'displayMajorNumber' : '0',
524 'displayMinorNumber' : '0',
525 'isEpisode' : 'true',
526 'colorCode' : ('COLOR', '4'),
527 'showType' : ('SERIES', '5'),
528 'tvRating' : ('NR', '7')
530 if key in defaults:
531 return defaults[key]
532 elif key.startswith('v'):
533 return []
534 else:
535 return ''