Some more quotes. This may not be quite right...
[pyTivo.git] / plugins / video / video.py
blobd1a6c818365217ba51699bc95b7858888b4ba781
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):
195 #No longer a 'cheep' hack :p
196 if handler.headers.getheader('Range') and not handler.headers.getheader('Range') == 'bytes=0-':
197 handler.send_response(206)
198 handler.send_header('Connection', 'close')
199 handler.send_header('Content-Type', 'video/x-tivo-mpeg')
200 handler.send_header('Transfer-Encoding', 'chunked')
201 handler.end_headers()
202 handler.wfile.write("\x30\x0D\x0A")
203 return
205 tsn = handler.headers.getheader('tsn', '')
207 o = urlparse("http://fake.host" + handler.path)
208 path = unquote(o[2])
209 handler.send_response(200)
210 handler.end_headers()
211 transcode.output_video(container['path'] + path[len(name)+1:], handler.wfile, tsn)
214 def __isdir(self, full_path):
215 return os.path.isdir(full_path)
217 def __duration(self, full_path):
218 return transcode.video_info(full_path)[4]
220 def __est_size(self, full_path, tsn = ''):
221 #Size is estimated by taking audio and video bit rate adding 2%
223 if transcode.tivo_compatable(full_path, tsn): # Is TiVo compatible mpeg2
224 return int(os.stat(full_path).st_size)
225 else: # Must be re-encoded
226 audioBPS = config.strtod(config.getAudioBR(tsn))
227 videoBPS = config.strtod(config.getVideoBR(tsn))
228 bitrate = audioBPS + videoBPS
229 return int((self.__duration(full_path)/1000)*(bitrate * 1.02 / 8))
231 def __getMetadataFromTxt(self, full_path):
232 metadata = {}
234 default_file = os.path.join(os.path.split(full_path)[0], 'default.txt')
235 description_file = full_path + '.txt'
237 metadata.update(self.__getMetadataFromFile(default_file))
238 metadata.update(self.__getMetadataFromFile(description_file))
240 return metadata
242 def __getMetadataFromFile(self, file):
243 metadata = {}
245 if os.path.exists(file):
246 for line in open(file):
247 if line.strip().startswith('#'):
248 continue
249 if not ':' in line:
250 continue
252 key, value = line.split(':', 1)
253 key = key.strip()
254 value = value.strip()
256 if key.startswith('v'):
257 if key in metadata:
258 metadata[key].append(value)
259 else:
260 metadata[key] = [value]
261 else:
262 metadata[key] = value
264 return metadata
266 def __metadata(self, full_path, tsn =''):
268 metadata = {}
270 base_path, title = os.path.split(full_path)
271 now = datetime.now()
272 originalAirDate = datetime.fromtimestamp(os.stat(full_path).st_ctime)
273 duration = self.__duration(full_path)
274 duration_delta = timedelta(milliseconds = duration)
276 metadata['title'] = '.'.join(title.split('.')[:-1])
277 metadata['seriesTitle'] = metadata['title'] # default to the filename
278 metadata['originalAirDate'] = originalAirDate.isoformat()
279 metadata['time'] = now.isoformat()
280 metadata['startTime'] = now.isoformat()
281 metadata['stopTime'] = (now + duration_delta).isoformat()
283 metadata.update( self.__getMetadataFromTxt(full_path) )
285 metadata['size'] = self.__est_size(full_path, tsn)
286 metadata['duration'] = duration
288 min = duration_delta.seconds / 60
289 sec = duration_delta.seconds % 60
290 hours = min / 60
291 min = min % 60
292 metadata['iso_duration'] = 'P' + str(duration_delta.days) + 'DT' + str(hours) + 'H' + str(min) + 'M' + str(sec) + 'S'
294 return metadata
296 def QueryContainer(self, handler, query):
298 tsn = handler.headers.getheader('tsn', '')
299 subcname = query['Container'][0]
301 ##If you are running 8.3 software you want to enable hack83 in the config file
302 if hack83:
303 print '========================================================================='
304 query, hackPath = self.hack(handler, query, subcname)
305 print 'Tivo said: ' + subcname + ' || Hack said: ' + "/".join(hackPath)
306 debug_write(['Hack Tivo said: ', subcname, ' || Hack said: ' , "/".join(hackPath), '\n'])
307 subcname = "/".join(hackPath)
309 if not query:
310 debug_write(['Hack sending 302 redirect page', '\n'])
311 handler.send_response(302)
312 handler.send_header('Location ', 'http://' + handler.headers.getheader('host') + '/TiVoConnect?Command=QueryContainer&AnchorItem=Hack8.3&Container=' + "/".join(hackPath))
313 handler.end_headers()
314 return
315 #End Hack mess
317 cname = subcname.split('/')[0]
319 if not handler.server.containers.has_key(cname) or not self.get_local_path(handler, query):
320 handler.send_response(404)
321 handler.end_headers()
322 return
324 def video_file_filter(full_path, type = None):
325 if os.path.isdir(full_path):
326 return True
327 return transcode.supported_format(full_path)
329 files, total, start = self.get_files(handler, query, video_file_filter)
331 videos = []
332 local_base_path = self.get_local_base_path(handler, query)
333 for file in files:
334 video = VideoDetails()
335 video['name'] = os.path.split(file)[1]
336 video['path'] = file
337 video['part_path'] = file.replace(local_base_path, '', 1)
338 video['title'] = os.path.split(file)[1]
339 video['is_dir'] = self.__isdir(file)
340 if not video['is_dir']:
341 video.update(self.__metadata(file, tsn))
343 videos.append(video)
345 handler.send_response(200)
346 handler.end_headers()
347 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'container.tmpl'))
348 t.container = cname
349 t.name = subcname
350 t.total = total
351 t.start = start
352 t.videos = videos
353 t.quote = quote
354 t.escape = escape
355 handler.wfile.write(t)
357 def TVBusQuery(self, handler, query):
359 tsn = handler.headers.getheader('tsn', '')
360 file = query['File'][0]
361 path = self.get_local_path(handler, query)
362 file_path = path + file
364 file_info = VideoDetails()
365 file_info.update(self.__metadata(file_path, tsn))
367 handler.send_response(200)
368 handler.end_headers()
369 t = Template(file=os.path.join(SCRIPTDIR,'templates', 'TvBus.tmpl'))
370 t.video = file_info
371 t.escape = escape
372 handler.wfile.write(t)
374 class VideoDetails(DictMixin):
376 def __init__(self, d = None):
377 if d:
378 self.d = d
379 else:
380 self.d = {}
382 def __getitem__(self, key):
383 if key not in self.d:
384 self.d[key] = self.default(key)
385 return self.d[key]
387 def __contains__(self, key):
388 return True
390 def __setitem__(self, key, value):
391 self.d[key] = value
393 def __delitem__(self):
394 del self.d[key]
396 def keys(self):
397 return self.d.keys()
399 def __iter__(self):
400 return self.d.__iter__()
402 def iteritems(self):
403 return self.d.iteritems()
405 def default(self, key):
406 defaults = {
407 'showingBits' : '0',
408 'episodeNumber' : '0',
409 'displayMajorNumber' : '0',
410 'displayMinorNumber' : '0',
411 'isEpisode' : 'true',
412 'colorCode' : ('COLOR', '4'),
413 'showType' : ('SERIES', '5'),
414 'tvRating' : ('NR', '7'),
416 if key in defaults:
417 return defaults[key]
418 elif key.startswith('v'):
419 return []
420 else:
421 return ''