Merge branch 'master' into subfolders-8.3
[pyTivo.git] / plugins / video / video.py
blob692a7b914c7e8ab63d37568ec373d2ad33f31cc8
1 import transcode, os, socket, re, urllib
2 from Cheetah.Template import Template
3 from plugin import Plugin
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 if os.path.sep == '/':
13 quote = urllib.quote
14 unquote = urllib.unquote_plus
15 else:
16 quote = lambda x: urllib.quote(x.replace(os.path.sep, '/'))
17 unquote = lambda x: urllib.unquote_plus(x).replace('/', os.path.sep)
19 SCRIPTDIR = os.path.dirname(__file__)
21 CLASS_NAME = 'Video'
23 debug = config.getDebug()
24 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()
34 if hack83:
35 debug_write(['Hack83 is enabled.\n'])
37 class Video(Plugin):
39 CONTENT_TYPE = 'x-container/tivo-videos'
41 # Used for 8.3's broken requests
42 count = 0
43 request_history = {}
45 def hack(self, handler, query, subcname):
46 debug_write(['Hack new request ------------------------', '\n'])
47 debug_write(['Hack TiVo request is: \n', query, '\n'])
48 queryAnchor = ''
49 rightAnchor = ''
50 leftAnchor = ''
51 tsn = handler.headers.getheader('tsn', '')
53 #not a tivo
54 if not tsn:
55 debug_write(['Hack this was not a TiVo request.', '\n'])
56 return query, None
58 #this breaks up the anchor item request into seperate parts
59 if 'AnchorItem' in query and (query['AnchorItem']) != ['Hack8.3']:
60 if "".join(query['AnchorItem']).find('Container=') >= 0:
61 #This is a folder
62 queryAnchor = unquote("".join(query['AnchorItem'])).split('Container=')[-1]
63 (leftAnchor, rightAnchor) = queryAnchor.rsplit(os.path.sep, 1)
64 else:
65 #This is a file
66 queryAnchor = unquote("".join(query['AnchorItem'])).split('/',1)[-1]
67 (leftAnchor, rightAnchor) = queryAnchor.rsplit(os.path.sep, 1)
68 debug_write(['Hack queryAnchor: ', queryAnchor, ' leftAnchor: ', leftAnchor, ' rightAnchor: ', rightAnchor, '\n'])
70 try:
71 path, state, = self.request_history[tsn]
72 except KeyError:
73 #Never seen this tsn, starting new history
74 debug_write(['New TSN.', '\n'])
75 path = []
76 state = {}
77 self.request_history[tsn] = (path, state)
78 state['query'] = query
79 state['page'] = ''
80 state['time'] = int(time.time()) + 1000
82 debug_write(['Hack our saved request is: \n', state['query'], '\n'])
84 current_folder = subcname.split('/')[-1]
86 #Needed to get list of files
87 def video_file_filter(file, type = None):
88 full_path = file
89 if os.path.isdir(full_path):
90 return True
91 return transcode.supported_format(full_path)
93 #Begin figuring out what the request TiVo sent us means
94 #There are 7 options that can occur
96 #1. at the root - This request is always accurate
97 if len(subcname.split('/')) == 1:
98 debug_write(['Hack we are at the root. Saving query, Clearing state[page].', '\n'])
99 path[:] = [current_folder]
100 state['query'] = query
101 state['page'] = ''
102 return state['query'], path
104 #2. entering a new folder
105 #If there is no AnchorItem in the request then we must
106 #be entering a new folder.
107 if 'AnchorItem' not in query:
108 debug_write(['Hack we are entering a new folder. Saving query, setting time, setting state[page].', '\n'])
109 path[:] = subcname.split('/')
110 state['query'] = query
111 state['time'] = int(time.time())
112 filePath = self.get_local_path(handler, state['query'])
113 files, total, start = self.get_files(handler, state['query'], video_file_filter)
114 if len(files) >= 1:
115 state['page'] = files[0]
116 else:
117 state['page'] = ''
118 return state['query'], path
120 #3. Request a page after pyTivo sent a 302 code
121 #we know this is the proper page
122 if "".join(query['AnchorItem']) == 'Hack8.3':
123 debug_write(['Hack requested page from 302 code. Returning saved query, ', '\n'])
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(['Hack requested a file', '\n'])
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
135 #for 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
139 #this allows a proper request to be processed first
140 debug_write(['Hack maybe erroneous request, sleeping.', '\n'])
141 time.sleep(.25)
143 #5. scrolling in a folder
144 #This could be a request to exit a folder
145 #or scroll up or down within the folder
146 #First we have to figure out if we are scrolling
147 if 'AnchorOffset' in query:
148 debug_write(['Hack Anchor offset was in query. leftAnchor needs to match ', "/".join(path), '\n'])
149 if leftAnchor == str("/".join(path)):
150 debug_write(['Hack leftAnchor matched.', '\n'])
151 query['Container'] = ["/".join(path)]
152 filePath = self.get_local_path(handler, query)
153 files, total, start = self.get_files(handler, query, video_file_filter)
154 debug_write(['Hack saved page is= ', state['page'], ' top returned file is= ', files[0], '\n'])
155 #If the first file returned equals the top of the page
156 #then we haven't scrolled pages
157 if files[0] != str(state['page']):
158 debug_write(['Hack this is scrolling within a folder.', '\n'])
159 filePath = self.get_local_path(handler, query)
160 files, total, start = self.get_files(handler, query, video_file_filter)
161 state['page'] = files[0]
162 return query, path
164 #The only remaining options are exiting a folder or
165 #this is a erroneous second request.
167 #6. this an extraneous request
168 #this came within a second of a valid request
169 #just use that request.
170 if (int(time.time()) - state['time']) <= 1:
171 debug_write(['Hack erroneous request, send a 302 error', '\n'])
172 filePath = self.get_local_path(handler, query)
173 files, total, start = self.get_files(handler, query, video_file_filter)
174 return None, path
175 #7. this is a request to exit a folder
176 #this request came by itself it must be to exit a folder
177 else:
178 debug_write(['Hack over 1 second, must be request to exit folder', '\n'])
179 path.pop()
180 downQuery = {}
181 downQuery['Command'] = query['Command']
182 downQuery['SortOrder'] = query['SortOrder']
183 downQuery['ItemCount'] = query['ItemCount']
184 downQuery['Filter'] = query['Filter']
185 downQuery['Container'] = ["/".join(path)]
186 state['query'] = downQuery
187 return None, path
189 #just in case we missed something.
190 debug_write(['Hack ERROR, should not have made it here. Trying to recover.', '\n'])
191 return state['query'], path
193 def send_file(self, handler, container, name):
194 if handler.headers.getheader('Range') and \
195 handler.headers.getheader('Range') != 'bytes=0-':
196 handler.send_response(206)
197 handler.send_header('Connection', 'close')
198 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
199 handler.send_header('Transfer-Encoding', 'chunked')
200 handler.end_headers()
201 handler.wfile.write("\x30\x0D\x0A")
202 return
204 tsn = handler.headers.getheader('tsn', '')
206 o = urlparse("http://fake.host" + handler.path)
207 path = unquote(o[2])
208 handler.send_response(200)
209 handler.end_headers()
210 transcode.output_video(container['path'] + path[len(name) + 1:],
211 handler.wfile, tsn)
213 def __isdir(self, full_path):
214 return os.path.isdir(full_path)
216 def __duration(self, full_path):
217 return transcode.video_info(full_path)[4]
219 def __est_size(self, full_path, tsn = ''):
220 # Size is estimated by taking audio and video bit rate adding 2%
222 if transcode.tivo_compatable(full_path, tsn):
223 # Is TiVo-compatible mpeg2
224 return int(os.stat(full_path).st_size)
225 else:
226 # Must be re-encoded
227 audioBPS = config.strtod(config.getAudioBR(tsn))
228 videoBPS = config.strtod(config.getVideoBR(tsn))
229 bitrate = audioBPS + videoBPS
230 return int((self.__duration(full_path) / 1000) *
231 (bitrate * 1.02 / 8))
233 def __getMetadataFromTxt(self, full_path):
234 metadata = {}
236 default_file = os.path.join(os.path.split(full_path)[0], 'default.txt')
237 description_file = full_path + '.txt'
239 metadata.update(self.__getMetadataFromFile(default_file))
240 metadata.update(self.__getMetadataFromFile(description_file))
242 return metadata
244 def __getMetadataFromFile(self, file):
245 metadata = {}
247 if os.path.exists(file):
248 for line in open(file):
249 if line.strip().startswith('#'):
250 continue
251 if not ':' in line:
252 continue
254 key, value = line.split(':', 1)
255 key = key.strip()
256 value = value.strip()
258 if key.startswith('v'):
259 if key in metadata:
260 metadata[key].append(value)
261 else:
262 metadata[key] = [value]
263 else:
264 metadata[key] = value
266 return metadata
268 def __metadata(self, full_path, tsn =''):
269 metadata = {}
271 base_path, title = os.path.split(full_path)
272 now = datetime.now()
273 originalAirDate = datetime.fromtimestamp(os.stat(full_path).st_ctime)
274 duration = self.__duration(full_path)
275 duration_delta = timedelta(milliseconds = duration)
277 metadata['title'] = '.'.join(title.split('.')[:-1])
278 metadata['seriesTitle'] = metadata['title'] # default to the filename
279 metadata['originalAirDate'] = originalAirDate.isoformat()
280 metadata['time'] = now.isoformat()
281 metadata['startTime'] = now.isoformat()
282 metadata['stopTime'] = (now + duration_delta).isoformat()
284 metadata.update( self.__getMetadataFromTxt(full_path) )
286 metadata['size'] = self.__est_size(full_path, tsn)
287 metadata['duration'] = duration
289 min = duration_delta.seconds / 60
290 sec = duration_delta.seconds % 60
291 hours = min / 60
292 min = min % 60
293 metadata['iso_duration'] = 'P' + str(duration_delta.days) + \
294 'DT' + str(hours) + 'H' + str(min) + \
295 'M' + str(sec) + 'S'
296 return metadata
298 def QueryContainer(self, handler, query):
299 tsn = handler.headers.getheader('tsn', '')
300 subcname = query['Container'][0]
302 ##If you are running 8.3 software you want to enable hack83 in the config file
303 if hack83:
304 print '========================================================================='
305 query, hackPath = self.hack(handler, query, subcname)
306 print 'Tivo said: ' + subcname + ' || Hack said: ' + "/".join(hackPath)
307 debug_write(['Hack Tivo said: ', subcname, ' || Hack said: ' , "/".join(hackPath), '\n'])
308 subcname = "/".join(hackPath)
310 if not query:
311 debug_write(['Hack sending 302 redirect page', '\n'])
312 handler.send_response(302)
313 handler.send_header('Location ', 'http://' + handler.headers.getheader('host') + '/TiVoConnect?Command=QueryContainer&AnchorItem=Hack8.3&Container=' + "/".join(hackPath))
314 handler.end_headers()
315 return
316 #End Hack mess
318 cname = subcname.split('/')[0]
320 if not handler.server.containers.has_key(cname) or \
321 not self.get_local_path(handler, query):
322 handler.send_response(404)
323 handler.end_headers()
324 return
326 def video_file_filter(full_path, type = None):
327 if os.path.isdir(full_path):
328 return True
329 return transcode.supported_format(full_path)
331 files, total, start = self.get_files(handler, query, video_file_filter)
333 videos = []
334 local_base_path = self.get_local_base_path(handler, query)
335 for file in files:
336 video = VideoDetails()
337 video['name'] = os.path.split(file)[1]
338 video['path'] = file
339 video['part_path'] = file.replace(local_base_path, '', 1)
340 video['title'] = os.path.split(file)[1]
341 video['is_dir'] = self.__isdir(file)
342 if not video['is_dir']:
343 video.update(self.__metadata(file, tsn))
345 videos.append(video)
347 handler.send_response(200)
348 handler.end_headers()
349 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
350 t.container = cname
351 t.name = subcname
352 t.total = total
353 t.start = start
354 t.videos = videos
355 t.quote = quote
356 t.escape = escape
357 handler.wfile.write(t)
359 def TVBusQuery(self, handler, query):
360 tsn = handler.headers.getheader('tsn', '')
361 file = query['File'][0]
362 path = self.get_local_path(handler, query)
363 file_path = path + file
365 file_info = VideoDetails()
366 file_info.update(self.__metadata(file_path, tsn))
368 handler.send_response(200)
369 handler.end_headers()
370 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
371 t.video = file_info
372 t.escape = escape
373 handler.wfile.write(t)
375 class VideoDetails(DictMixin):
377 def __init__(self, d=None):
378 if d:
379 self.d = d
380 else:
381 self.d = {}
383 def __getitem__(self, key):
384 if key not in self.d:
385 self.d[key] = self.default(key)
386 return self.d[key]
388 def __contains__(self, key):
389 return True
391 def __setitem__(self, key, value):
392 self.d[key] = value
394 def __delitem__(self):
395 del self.d[key]
397 def keys(self):
398 return self.d.keys()
400 def __iter__(self):
401 return self.d.__iter__()
403 def iteritems(self):
404 return self.d.iteritems()
406 def default(self, key):
407 defaults = {
408 'showingBits' : '0',
409 'episodeNumber' : '0',
410 'displayMajorNumber' : '0',
411 'displayMinorNumber' : '0',
412 'isEpisode' : 'true',
413 'colorCode' : ('COLOR', '4'),
414 'showType' : ('SERIES', '5'),
415 'tvRating' : ('NR', '7')
417 if key in defaults:
418 return defaults[key]
419 elif key.startswith('v'):
420 return []
421 else:
422 return ''