Merge branch 'master' into subfolders-8.3
[pyTivo.git] / plugins / video / video.py
blob6cf9aed92f0c7b274ff9378ad576cd3550b30f91
1 import transcode, os, socket, re
2 from Cheetah.Template import Template
3 from plugin import Plugin
4 from urllib import unquote_plus, quote, unquote
5 from urlparse import urlparse
6 from xml.sax.saxutils import escape
7 from lrucache import LRUCache
8 from UserDict import DictMixin
9 from datetime import datetime, timedelta
10 import config
11 import time
13 SCRIPTDIR = os.path.dirname(__file__)
15 CLASS_NAME = 'Video'
17 debug = config.getDebug()
18 hack83 = config.getHack83()
19 def debug_write(data):
20 if debug:
21 debug_out = []
22 debug_out.append('Video.py - ')
23 for x in data:
24 debug_out.append(str(x))
25 fdebug = open('debug.txt', 'a')
26 fdebug.write(' '.join(debug_out))
27 fdebug.close()
28 if hack83:
29 debug_write(['Hack83 is enabled.\n'])
31 class Video(Plugin):
33 CONTENT_TYPE = 'x-container/tivo-videos'
35 # Used for 8.3's broken requests
36 count = 0
37 request_history = {}
39 def hack(self, handler, query, subcname):
40 debug_write(['Hack new request ------------------------', '\n'])
41 debug_write(['Hack TiVo request is: \n', query, '\n'])
42 queryAnchor = ''
43 rightAnchor = ''
44 leftAnchor = ''
45 tsn = handler.headers.getheader('tsn', '')
47 #not a tivo
48 if not tsn:
49 debug_write(['Hack this was not a TiVo request.', '\n'])
50 return query, None
52 #this breaks up the anchor item request into seperate parts
53 if 'AnchorItem' in query and (query['AnchorItem']) != ['Hack8.3']:
54 if "".join(query['AnchorItem']).find('Container=') >= 0:
55 #This is a folder
56 queryAnchor = unquote_plus("".join(query['AnchorItem'])).split('Container=')[-1]
57 (leftAnchor, rightAnchor) = queryAnchor.rsplit('/', 1)
58 else:
59 #This is a file
60 queryAnchor = unquote_plus("".join(query['AnchorItem'])).split('/',1)[-1]
61 (leftAnchor, rightAnchor) = queryAnchor.rsplit('/', 1)
62 debug_write(['Hack queryAnchor: ', queryAnchor, ' leftAnchor: ', leftAnchor, ' rightAnchor: ', rightAnchor, '\n'])
64 try:
65 path, state, = self.request_history[tsn]
66 except KeyError:
67 #Never seen this tsn, starting new history
68 debug_write(['New TSN.', '\n'])
69 path = []
70 state = {}
71 self.request_history[tsn] = (path, state)
72 state['query'] = query
73 state['page'] = ''
74 state['time'] = int(time.time()) + 1000
76 debug_write(['Hack our saved request is: \n', state['query'], '\n'])
78 current_folder = subcname.split('/')[-1]
80 #Needed to get list of files
81 def VideoFileFilter(file):
82 full_path = os.path.join(filePath, file)
84 if os.path.isdir(full_path):
85 return True
86 return transcode.suported_format(full_path)
88 #Begin figuring out what the request TiVo sent us means
89 #There are 7 options that can occur
91 #1. at the root - This request is always accurate
92 if len(subcname.split('/')) == 1:
93 debug_write(['Hack we are at the root. Saving query, Clearing state[page].', '\n'])
94 path[:] = [current_folder]
95 state['query'] = query
96 state['page'] = ''
97 return state['query'], path
99 #2. entering a new folder
100 #If there is no AnchorItem in the request then we must
101 #be entering a new folder.
102 if 'AnchorItem' not in query:
103 debug_write(['Hack we are entering a new folder. Saving query, setting time, setting state[page].', '\n'])
104 path[:] = subcname.split('/')
105 state['query'] = query
106 state['time'] = int(time.time())
107 filePath = self.get_local_path(handler, state['query'])
108 files, total, start = self.get_files(handler, state['query'], VideoFileFilter)
109 if len(files) >= 1:
110 state['page'] = files[0]
111 else:
112 state['page'] = ''
113 return state['query'], path
115 #3. Request a page after pyTivo sent a 302 code
116 #we know this is the proper page
117 if "".join(query['AnchorItem']) == 'Hack8.3':
118 debug_write(['Hack requested page from 302 code. Returning saved query, ', '\n'])
119 return state['query'], path
121 #4. this is a request for a file
122 if 'ItemCount' in query and int("".join(query['ItemCount'])) == 1:
123 debug_write(['Hack requested a file', '\n'])
124 #Everything in this request is right except the container
125 query['Container'] = ["/".join(path)]
126 return query, path
128 ##All remaining requests could be a second erroneous request
129 #for each of the following we will pause to see if a correct
130 #request is coming right behind it.
132 #Sleep just in case the erroneous request came first
133 #this allows a proper request to be processed first
134 debug_write(['Hack maybe erroneous request, sleeping.', '\n'])
135 time.sleep(.25)
137 #5. scrolling in a folder
138 #This could be a request to exit a folder
139 #or scroll up or down within the folder
140 #First we have to figure out if we are scrolling
141 if 'AnchorOffset' in query:
142 debug_write(['Hack Anchor offset was in query. leftAnchor needs to match ', "/".join(path), '\n'])
143 if leftAnchor == str("/".join(path)):
144 debug_write(['Hack leftAnchor matched.', '\n'])
145 query['Container'] = ["/".join(path)]
146 filePath = self.get_local_path(handler, query)
147 files, total, start = self.get_files(handler, query, VideoFileFilter)
148 debug_write(['Hack saved page is= ', state['page'], ' top returned file is= ', files[0], '\n'])
149 #If the first file returned equals the top of the page
150 #then we haven't scrolled pages
151 if files[0] != str(state['page']):
152 debug_write(['Hack this is scrolling within a folder.', '\n'])
153 filePath = self.get_local_path(handler, query)
154 files, total, start = self.get_files(handler, query, VideoFileFilter)
155 state['page'] = files[0]
156 return query, path
158 #The only remaining options are exiting a folder or
159 #this is a erroneous second request.
161 #6. this an extraneous request
162 #this came within a second of a valid request
163 #just use that request.
164 if (int(time.time()) - state['time']) <= 1:
165 debug_write(['Hack erroneous request, send a 302 error', '\n'])
166 filePath = self.get_local_path(handler, query)
167 files, total, start = self.get_files(handler, query, VideoFileFilter)
168 return None, path
169 #7. this is a request to exit a folder
170 #this request came by itself it must be to exit a folder
171 else:
172 debug_write(['Hack over 1 second, must be request to exit folder', '\n'])
173 path.pop()
174 downQuery = {}
175 downQuery['Command'] = query['Command']
176 downQuery['SortOrder'] = query['SortOrder']
177 downQuery['ItemCount'] = query['ItemCount']
178 downQuery['Filter'] = query['Filter']
179 downQuery['Container'] = ["/".join(path)]
180 state['query'] = downQuery
181 return None, path
183 #just in case we missed something.
184 debug_write(['Hack ERROR, should not have made it here. Trying to recover.', '\n'])
185 return state['query'], path
187 def send_file(self, handler, container, name):
189 #No longer a 'cheep' hack :p
190 if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-':
191 handler.send_response(206)
192 handler.send_header('Connection', 'close')
193 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
194 handler.send_header('Transfer-Encoding', 'chunked')
195 handler.send_header('Server', 'TiVo Server/1.4.257.475')
196 handler.end_headers()
197 handler.wfile.write("\x30\x0D\x0A")
198 return
200 tsn = handler.headers.getheader('tsn', '')
202 o = urlparse("http://fake.host" + handler.path)
203 path = unquote_plus(o[2])
204 handler.send_response(200)
205 handler.end_headers()
206 transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile, tsn)
209 def __isdir(self, full_path):
210 return os.path.isdir(full_path)
212 def __duration(self, full_path):
213 return transcode.video_info(full_path)[4]
215 def __est_size(self, full_path):
216 #Size is estimated by taking audio and video bit rate adding 2%
218 if transcode.tivo_compatable(full_path): # Is TiVo compatible mpeg2
219 return int(os.stat(full_path).st_size)
220 else: # Must be re-encoded
221 audioBPS = strtod(config.getAudioBR())
222 videoBPS = strtod(config.getVideoBR())
223 bitrate = audioBPS + videoBPS
224 return int((self.__duration(full_path)/1000)*(bitrate * 1.02 / 8))
226 def __getMetadataFromTxt(self, full_path):
227 metadata = {}
229 default_file = os.path.join(os.path.split(full_path)[0], 'default.txt')
230 description_file = full_path + '.txt'
232 metadata.update(self.__getMetadataFromFile(default_file))
233 metadata.update(self.__getMetadataFromFile(description_file))
235 return metadata
237 def __getMetadataFromFile(self, file):
238 metadata = {}
240 if os.path.exists(file):
241 for line in open(file):
242 if line.strip().startswith('#'):
243 continue
244 if not ':' in line:
245 continue
247 key, value = line.split(':', 1)
248 key = key.strip()
249 value = value.strip()
251 if key.startswith('v'):
252 if key in metadata:
253 metadata[key].append(value)
254 else:
255 metadata[key] = [value]
256 else:
257 metadata[key] = value
259 return metadata
261 def __metadata(self, full_path):
263 metadata = {}
265 base_path, title = os.path.split(full_path)
266 now = datetime.now()
267 originalAirDate = datetime.fromtimestamp(os.stat(full_path).st_ctime)
268 duration = self.__duration(full_path)
269 duration_delta = timedelta(milliseconds = duration)
271 metadata['title'] = '.'.join(title.split('.')[:-1])
272 metadata['seriesTitle'] = os.path.split(base_path)[1]
273 metadata['originalAirDate'] = originalAirDate.isoformat()
274 metadata['time'] = now.isoformat()
275 metadata['startTime'] = now.isoformat()
276 metadata['stopTime'] = (now + duration_delta).isoformat()
278 metadata.update( self.__getMetadataFromTxt(full_path) )
280 metadata['size'] = self.__est_size(full_path)
281 metadata['duration'] = duration
283 min = duration_delta.seconds / 60
284 sec = duration_delta.seconds % 60
285 hours = min / 60
286 min = min % 60
287 metadata['iso_durarion'] = 'P' + str(duration_delta.days) + 'DT' + str(hours) + 'H' + str(min) + 'M' + str(sec) + 'S'
289 return metadata
291 def QueryContainer(self, handler, query):
293 subcname = query['Container'][0]
295 ##If you are running 8.3 software you want to enable hack83 in the config file
296 if hack83:
297 print '========================================================================='
298 query, hackPath = self.hack(handler, query, subcname)
299 print 'Tivo said: ' + subcname + ' || Hack said: ' + "/".join(hackPath)
300 debug_write(['Hack Tivo said: ', subcname, ' || Hack said: ' , "/".join(hackPath), '\n'])
301 subcname = "/".join(hackPath)
303 if not query:
304 debug_write(['Hack sending 302 redirect page', '\n'])
305 handler.send_response(302)
306 handler.send_header('Location ', 'http://' + handler.headers.getheader('host') + '/TiVoConnect?Command=QueryContainer&AnchorItem=Hack8.3&Container=' + "/".join(hackPath))
307 handler.end_headers()
308 return
309 #End Hack mess
311 cname = subcname.split('/')[0]
313 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
314 handler.send_response(404)
315 handler.end_headers()
316 return
318 def video_file_filter(file, type = None):
319 full_path = file
320 if os.path.isdir(full_path):
321 return True
322 return transcode.suported_format(full_path)
324 files, total, start = self.get_files(handler, query, video_file_filter)
326 videos = []
327 for file in files:
328 video = VideoDetails()
329 video['name'] = os.path.split(file)[1]
330 video['path'] = file
331 video['title'] = os.path.split(file)[1]
332 video['is_dir'] = self.__isdir(file)
333 if not video['is_dir']:
334 video.update(self.__metadata(file))
336 videos.append(video)
338 handler.send_response(200)
339 handler.end_headers()
340 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
341 t.name = subcname
342 t.total = total
343 t.start = start
344 t.videos = videos
345 t.quote = quote
346 t.escape = escape
347 handler.wfile.write(t)
349 def TVBusQuery(self, handler, query):
351 file = query['File'][0]
352 path = self.get_local_path(handler, query)
353 file_path = os.path.join(path, file)
355 file_info = VideoDetails()
356 file_info.update(self.__metadata(file_path))
358 handler.send_response(200)
359 handler.end_headers()
360 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
361 t.video = file_info
362 t.escape = escape
363 handler.wfile.write(t)
365 class VideoDetails(DictMixin):
367 def __init__(self, d = None):
368 if d:
369 self.d = d
370 else:
371 self.d = {}
373 def __getitem__(self, key):
374 if key not in self.d:
375 self.d[key] = self.default(key)
376 return self.d[key]
378 def __contains__(self, key):
379 return True
381 def __setitem__(self, key, value):
382 self.d[key] = value
384 def __delitem__(self):
385 del self.d[key]
387 def keys(self):
388 return self.d.keys()
390 def __iter__(self):
391 return self.d.__iter__()
393 def iteritems(self):
394 return self.d.iteritems()
396 def default(self, key):
397 defaults = {
398 'showingBits' : '0',
399 'episodeNumber' : '0',
400 'displayMajorNumber' : '0',
401 'displayMinorNumber' : '0',
402 'isEpisode' : 'true',
403 'colorCode' : ('COLOR', '4'),
404 'showType' : ('SERIES', '5'),
405 'tvRating' : ('NR', '7'),
407 if key in defaults:
408 return defaults[key]
409 elif key.startswith('v'):
410 return []
411 else:
412 return ''
415 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
416 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
417 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
418 def strtod(value):
419 prefixes = {"y":-24,"z":-21,"a":-18,"f":-15,"p":-12,"n":-9,"u":-6,"m":-3,"c":-2,"d":-1,"h":2,"k":3,"K":3,"M":6,"G":9,"T":12,"P":15,"E":18,"Z":21,"Y":24}
420 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
421 m = p.match(value)
422 if m is None:
423 raise SyntaxError('Invalid bit value syntax')
424 (coef, prefix, power, byte) = m.groups()
425 if prefix is None:
426 value = float(coef)
427 else:
428 exponent = float(prefixes[prefix])
429 if power == "i":
430 # Use powers of 2
431 value = float(coef) * pow(2.0, exponent/0.3)
432 else:
433 # Use powers of 10
434 value = float(coef) * pow(10.0, exponent)
435 if byte == "B": # B==Byte, b=bit
436 value *= 8;
437 return value