Fixed error in metadata not passing tsn to est_file size
[pyTivo/krkeegan.git] / plugins / video / video.py
blob531e48e84c7af04046cdeb2f8d6c0c9ede59c4b4
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 video_file_filter(file, type = None):
82 full_path = file
83 if os.path.isdir(full_path):
84 return True
85 return transcode.supported_format(full_path)
87 #Begin figuring out what the request TiVo sent us means
88 #There are 7 options that can occur
90 #1. at the root - This request is always accurate
91 if len(subcname.split('/')) == 1:
92 debug_write(['Hack we are at the root. Saving query, Clearing state[page].', '\n'])
93 path[:] = [current_folder]
94 state['query'] = query
95 state['page'] = ''
96 return state['query'], path
98 #2. entering a new folder
99 #If there is no AnchorItem in the request then we must
100 #be entering a new folder.
101 if 'AnchorItem' not in query:
102 debug_write(['Hack we are entering a new folder. Saving query, setting time, setting state[page].', '\n'])
103 path[:] = subcname.split('/')
104 state['query'] = query
105 state['time'] = int(time.time())
106 filePath = self.get_local_path(handler, state['query'])
107 files, total, start = self.get_files(handler, state['query'], video_file_filter)
108 if len(files) >= 1:
109 state['page'] = files[0]
110 else:
111 state['page'] = ''
112 return state['query'], path
114 #3. Request a page after pyTivo sent a 302 code
115 #we know this is the proper page
116 if "".join(query['AnchorItem']) == 'Hack8.3':
117 debug_write(['Hack requested page from 302 code. Returning saved query, ', '\n'])
118 return state['query'], path
120 #4. this is a request for a file
121 if 'ItemCount' in query and int("".join(query['ItemCount'])) == 1:
122 debug_write(['Hack requested a file', '\n'])
123 #Everything in this request is right except the container
124 query['Container'] = ["/".join(path)]
125 state['page'] = ''
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, video_file_filter)
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, video_file_filter)
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, video_file_filter)
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, tsn = ''):
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(tsn))
222 videoBPS = strtod(config.getVideoBR(tsn))
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, tsn =''):
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'] = metadata['title'] # default to the filename
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, tsn)
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 tsn = handler.headers.getheader('tsn', '')
294 subcname = query['Container'][0]
296 ##If you are running 8.3 software you want to enable hack83 in the config file
297 if hack83:
298 print '========================================================================='
299 query, hackPath = self.hack(handler, query, subcname)
300 print 'Tivo said: ' + subcname + ' || Hack said: ' + "/".join(hackPath)
301 debug_write(['Hack Tivo said: ', subcname, ' || Hack said: ' , "/".join(hackPath), '\n'])
302 subcname = "/".join(hackPath)
304 if not query:
305 debug_write(['Hack sending 302 redirect page', '\n'])
306 handler.send_response(302)
307 handler.send_header('Location ', 'http://' + handler.headers.getheader('host') + '/TiVoConnect?Command=QueryContainer&AnchorItem=Hack8.3&Container=' + "/".join(hackPath))
308 handler.end_headers()
309 return
310 #End Hack mess
312 cname = subcname.split('/')[0]
314 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
315 handler.send_response(404)
316 handler.end_headers()
317 return
319 def video_file_filter(file, type = None):
320 full_path = file
321 if os.path.isdir(full_path):
322 return True
323 return transcode.supported_format(full_path)
325 files, total, start = self.get_files(handler, query, video_file_filter)
327 videos = []
328 for file in files:
329 video = VideoDetails()
330 video['name'] = os.path.split(file)[1]
331 video['path'] = file
332 video['title'] = os.path.split(file)[1]
333 video['is_dir'] = self.__isdir(file)
334 if not video['is_dir']:
335 video.update(self.__metadata(file, tsn))
337 videos.append(video)
339 handler.send_response(200)
340 handler.end_headers()
341 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
342 t.name = subcname
343 t.total = total
344 t.start = start
345 t.videos = videos
346 t.quote = quote
347 t.escape = escape
348 handler.wfile.write(t)
350 def TVBusQuery(self, handler, query):
352 file = query['File'][0]
353 path = self.get_local_path(handler, query)
354 file_path = os.path.join(path, file)
356 file_info = VideoDetails()
357 file_info.update(self.__metadata(file_path, tsn))
359 handler.send_response(200)
360 handler.end_headers()
361 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
362 t.video = file_info
363 t.escape = escape
364 handler.wfile.write(t)
366 class VideoDetails(DictMixin):
368 def __init__(self, d = None):
369 if d:
370 self.d = d
371 else:
372 self.d = {}
374 def __getitem__(self, key):
375 if key not in self.d:
376 self.d[key] = self.default(key)
377 return self.d[key]
379 def __contains__(self, key):
380 return True
382 def __setitem__(self, key, value):
383 self.d[key] = value
385 def __delitem__(self):
386 del self.d[key]
388 def keys(self):
389 return self.d.keys()
391 def __iter__(self):
392 return self.d.__iter__()
394 def iteritems(self):
395 return self.d.iteritems()
397 def default(self, key):
398 defaults = {
399 'showingBits' : '0',
400 'episodeNumber' : '0',
401 'displayMajorNumber' : '0',
402 'displayMinorNumber' : '0',
403 'isEpisode' : 'true',
404 'colorCode' : ('COLOR', '4'),
405 'showType' : ('SERIES', '5'),
406 'tvRating' : ('NR', '7'),
408 if key in defaults:
409 return defaults[key]
410 elif key.startswith('v'):
411 return []
412 else:
413 return ''
416 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
417 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
418 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
419 def strtod(value):
420 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}
421 p = re.compile(r'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
422 m = p.match(value)
423 if m is None:
424 raise SyntaxError('Invalid bit value syntax')
425 (coef, prefix, power, byte) = m.groups()
426 if prefix is None:
427 value = float(coef)
428 else:
429 exponent = float(prefixes[prefix])
430 if power == "i":
431 # Use powers of 2
432 value = float(coef) * pow(2.0, exponent/0.3)
433 else:
434 # Use powers of 10
435 value = float(coef) * pow(10.0, exponent)
436 if byte == "B": # B==Byte, b=bit
437 value *= 8;
438 return value