Trivial.
[pyTivo.git] / plugins / video / video.py
blobaab7c7d8064d70d9abcc5c5f152fec07a27bdf5e
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 video_file_filter(self, full_path, type=None):
47 if os.path.isdir(full_path):
48 return True
49 if extensions:
50 return os.path.splitext(full_path)[1].lower() in extensions
51 else:
52 return transcode.supported_format(full_path)
54 def hack(self, handler, query, subcname):
55 debug_write(['Hack new request ------------------------\n'])
56 debug_write(['Hack TiVo request is: \n', query, '\n'])
57 queryAnchor = ''
58 rightAnchor = ''
59 leftAnchor = ''
60 tsn = handler.headers.getheader('tsn', '')
62 # not a tivo
63 if not tsn:
64 debug_write(['Hack this was not a TiVo request.\n'])
65 return query, None
67 # this breaks up the anchor item request into seperate parts
68 if 'AnchorItem' in query and query['AnchorItem'] != ['Hack8.3']:
69 queryAnchor = urllib.unquote_plus(''.join(query['AnchorItem']))
70 if queryAnchor.find('Container=') >= 0:
71 # This is a folder
72 queryAnchor = queryAnchor.split('Container=')[-1]
73 else:
74 # This is a file
75 queryAnchor = queryAnchor.split('/', 1)[-1]
76 leftAnchor, rightAnchor = queryAnchor.rsplit('/', 1)
77 debug_write(['Hack queryAnchor: ', queryAnchor,
78 ' leftAnchor: ', leftAnchor,
79 ' rightAnchor: ', rightAnchor, '\n'])
80 try:
81 path, state = self.request_history[tsn]
82 except KeyError:
83 # Never seen this tsn, starting new history
84 debug_write(['New TSN.\n'])
85 path = []
86 state = {}
87 self.request_history[tsn] = (path, state)
88 state['query'] = query
89 state['page'] = ''
90 state['time'] = int(time.time()) + 1000
92 debug_write(['Hack our saved request is: \n', state['query'], '\n'])
94 current_folder = subcname.split('/')[-1]
96 # Begin figuring out what the request TiVo sent us means
97 # There are 7 options that can occur
99 # 1. at the root - This request is always accurate
100 if len(subcname.split('/')) == 1:
101 debug_write(['Hack we are at the root.',
102 'Saving query, Clearing state[page].\n'])
103 path[:] = [current_folder]
104 state['query'] = query
105 state['page'] = ''
106 return query, path
108 # 2. entering a new folder
109 # If there is no AnchorItem in the request then we must be
110 # entering a new folder.
111 if 'AnchorItem' not in query:
112 debug_write(['Hack we are entering a new folder.',
113 'Saving query, setting time, setting state[page].\n'])
114 path[:] = subcname.split('/')
115 state['query'] = query
116 state['time'] = int(time.time())
117 files, total, start = self.get_files(handler, query,
118 self.video_file_filter)
119 if files:
120 state['page'] = files[0]
121 else:
122 state['page'] = ''
123 return query, path
125 # 3. Request a page after pyTivo sent a 302 code
126 # we know this is the proper page
127 if ''.join(query['AnchorItem']) == 'Hack8.3':
128 debug_write(['Hack requested page from 302 code.',
129 'Returning saved query,\n'])
130 return state['query'], path
132 # 4. this is a request for a file
133 if 'ItemCount' in query and int(''.join(query['ItemCount'])) == 1:
134 debug_write(['Hack requested a file', '\n'])
135 # Everything in this request is right except the container
136 query['Container'] = ['/'.join(path)]
137 state['page'] = ''
138 return query, path
140 # All remaining requests could be a second erroneous request for
141 # each of the following we will pause to see if a correct
142 # request is coming right behind it.
144 # Sleep just in case the erroneous request came first this
145 # allows a proper request to be processed first
146 debug_write(['Hack maybe erroneous request, sleeping.\n'])
147 time.sleep(.25)
149 # 5. scrolling in a folder
150 # This could be a request to exit a folder or scroll up or down
151 # within the folder
152 # First we have to figure out if we are scrolling
153 if 'AnchorOffset' in query:
154 debug_write(['Hack Anchor offset was in query.',
155 'leftAnchor needs to match ', '/'.join(path), '\n'])
156 if leftAnchor == str('/'.join(path)):
157 debug_write(['Hack leftAnchor matched.', '\n'])
158 query['Container'] = ['/'.join(path)]
159 files, total, start = self.get_files(handler, query,
160 self.video_file_filter)
161 debug_write(['Hack saved page is= ', state['page'],
162 ' top returned file is= ', files[0], '\n'])
163 # If the first file returned equals the top of the page
164 # then we haven't scrolled pages
165 if files[0] != str(state['page']):
166 debug_write(['Hack this is scrolling within a folder.\n'])
167 state['page'] = files[0]
168 return query, path
170 # The only remaining options are exiting a folder or this is a
171 # erroneous second request.
173 # 6. this an extraneous request
174 # this came within a second of a valid request; just use that
175 # request.
176 if (int(time.time()) - state['time']) <= 1:
177 debug_write(['Hack erroneous request, send a 302 error', '\n'])
178 return None, path
180 # 7. this is a request to exit a folder
181 # this request came by itself; it must be to exit a folder
182 else:
183 debug_write(['Hack over 1 second,',
184 'must be request to exit folder\n'])
185 path.pop()
186 downQuery = {}
187 downQuery['Command'] = query['Command']
188 downQuery['SortOrder'] = query['SortOrder']
189 downQuery['ItemCount'] = query['ItemCount']
190 downQuery['Filter'] = query['Filter']
191 downQuery['Container'] = ['/'.join(path)]
192 state['query'] = downQuery
193 return None, path
195 # just in case we missed something.
196 debug_write(['Hack ERROR, should not have made it here. ',
197 'Trying to recover.\n'])
198 return state['query'], path
200 def send_file(self, handler, container, name):
201 if handler.headers.getheader('Range') and \
202 handler.headers.getheader('Range') != 'bytes=0-':
203 handler.send_response(206)
204 handler.send_header('Connection', 'close')
205 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
206 handler.send_header('Transfer-Encoding', 'chunked')
207 handler.end_headers()
208 handler.wfile.write("\x30\x0D\x0A")
209 return
211 tsn = handler.headers.getheader('tsn', '')
213 o = urlparse("http://fake.host" + handler.path)
214 path = unquote(o[2])
215 handler.send_response(200)
216 handler.end_headers()
217 transcode.output_video(container['path'] + path[len(name) + 1:],
218 handler.wfile, tsn)
220 def __isdir(self, full_path):
221 return os.path.isdir(full_path)
223 def __duration(self, full_path):
224 return transcode.video_info(full_path)[4]
226 def __est_size(self, full_path, tsn = ''):
227 # Size is estimated by taking audio and video bit rate adding 2%
229 if transcode.tivo_compatable(full_path, tsn):
230 # Is TiVo-compatible mpeg2
231 return int(os.stat(full_path).st_size)
232 else:
233 # Must be re-encoded
234 audioBPS = config.strtod(config.getAudioBR(tsn))
235 videoBPS = config.strtod(config.getVideoBR(tsn))
236 bitrate = audioBPS + videoBPS
237 return int((self.__duration(full_path) / 1000) *
238 (bitrate * 1.02 / 8))
240 def __getMetadataFromTxt(self, full_path):
241 metadata = {}
243 default_file = os.path.join(os.path.split(full_path)[0], 'default.txt')
244 description_file = full_path + '.txt'
246 metadata.update(self.__getMetadataFromFile(default_file))
247 metadata.update(self.__getMetadataFromFile(description_file))
249 return metadata
251 def __getMetadataFromFile(self, file):
252 metadata = {}
254 if os.path.exists(file):
255 for line in open(file):
256 if line.strip().startswith('#'):
257 continue
258 if not ':' in line:
259 continue
261 key, value = line.split(':', 1)
262 key = key.strip()
263 value = value.strip()
265 if key.startswith('v'):
266 if key in metadata:
267 metadata[key].append(value)
268 else:
269 metadata[key] = [value]
270 else:
271 metadata[key] = value
273 return metadata
275 def __metadata(self, full_path, tsn =''):
276 metadata = {}
278 base_path, title = os.path.split(full_path)
279 now = datetime.now()
280 originalAirDate = datetime.fromtimestamp(os.stat(full_path).st_ctime)
281 duration = self.__duration(full_path)
282 duration_delta = timedelta(milliseconds = duration)
284 metadata['title'] = '.'.join(title.split('.')[:-1])
285 metadata['seriesTitle'] = metadata['title'] # default to the filename
286 metadata['originalAirDate'] = originalAirDate.isoformat()
287 metadata['time'] = now.isoformat()
288 metadata['startTime'] = now.isoformat()
289 metadata['stopTime'] = (now + duration_delta).isoformat()
291 metadata.update( self.__getMetadataFromTxt(full_path) )
293 metadata['size'] = self.__est_size(full_path, tsn)
294 metadata['duration'] = duration
296 min = duration_delta.seconds / 60
297 sec = duration_delta.seconds % 60
298 hours = min / 60
299 min = min % 60
300 metadata['iso_duration'] = 'P' + str(duration_delta.days) + \
301 'DT' + str(hours) + 'H' + str(min) + \
302 'M' + str(sec) + 'S'
303 return metadata
305 def QueryContainer(self, handler, query):
306 tsn = handler.headers.getheader('tsn', '')
307 subcname = query['Container'][0]
309 # If you are running 8.3 software you want to enable hack83
310 # in the config file
312 if hack83:
313 print '=' * 73
314 query, hackPath = self.hack(handler, query, subcname)
315 hackPath = '/'.join(hackPath)
316 print 'Tivo said:', subcname, '|| Hack said:', hackPath
317 debug_write(['Hack Tivo said: ', subcname, ' || Hack said: ',
318 hackPath, '\n'])
319 subcname = hackPath
321 if not query:
322 debug_write(['Hack sending 302 redirect page', '\n'])
323 handler.send_response(302)
324 handler.send_header('Location ', 'http://' +
325 handler.headers.getheader('host') +
326 '/TiVoConnect?Command=QueryContainer&' +
327 'AnchorItem=Hack8.3&Container=' + hackPath)
328 handler.end_headers()
329 return
331 # End hack mess
333 cname = subcname.split('/')[0]
335 if not handler.server.containers.has_key(cname) or \
336 not self.get_local_path(handler, query):
337 handler.send_response(404)
338 handler.end_headers()
339 return
341 files, total, start = self.get_files(handler, query,
342 self.video_file_filter)
344 videos = []
345 local_base_path = self.get_local_base_path(handler, query)
346 for file in files:
347 video = VideoDetails()
348 video['name'] = os.path.split(file)[1]
349 video['path'] = file
350 video['part_path'] = file.replace(local_base_path, '', 1)
351 video['title'] = os.path.split(file)[1]
352 video['is_dir'] = self.__isdir(file)
353 if video['is_dir']:
354 video['small_path'] = subcname + '/' + video['name']
355 else:
356 video['valid'] = transcode.supported_format(file)
357 if video['valid']:
358 video.update(self.__metadata(file, tsn))
360 videos.append(video)
362 handler.send_response(200)
363 handler.end_headers()
364 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
365 t.container = cname
366 t.name = subcname
367 t.total = total
368 t.start = start
369 t.videos = videos
370 t.quote = quote
371 t.escape = escape
372 t.crc = zlib.crc32
373 t.guid = config.getGUID()
374 handler.wfile.write(t)
376 def TVBusQuery(self, handler, query):
377 tsn = handler.headers.getheader('tsn', '')
378 file = query['File'][0]
379 path = self.get_local_path(handler, query)
380 file_path = path + file
382 file_info = VideoDetails()
383 valid = transcode.supported_format(file_path)
384 if valid:
385 file_info.update(self.__metadata(file_path, tsn))
387 handler.send_response(200)
388 handler.end_headers()
389 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
390 t.video = file_info
391 t.escape = escape
392 handler.wfile.write(t)
394 class VideoDetails(DictMixin):
396 def __init__(self, d=None):
397 if d:
398 self.d = d
399 else:
400 self.d = {}
402 def __getitem__(self, key):
403 if key not in self.d:
404 self.d[key] = self.default(key)
405 return self.d[key]
407 def __contains__(self, key):
408 return True
410 def __setitem__(self, key, value):
411 self.d[key] = value
413 def __delitem__(self):
414 del self.d[key]
416 def keys(self):
417 return self.d.keys()
419 def __iter__(self):
420 return self.d.__iter__()
422 def iteritems(self):
423 return self.d.iteritems()
425 def default(self, key):
426 defaults = {
427 'showingBits' : '0',
428 'episodeNumber' : '0',
429 'displayMajorNumber' : '0',
430 'displayMinorNumber' : '0',
431 'isEpisode' : 'true',
432 'colorCode' : ('COLOR', '4'),
433 'showType' : ('SERIES', '5'),
434 'tvRating' : ('NR', '7')
436 if key in defaults:
437 return defaults[key]
438 elif key.startswith('v'):
439 return []
440 else:
441 return ''