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
12 if os
.path
.sep
== '/':
14 unquote
= urllib
.unquote_plus
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__
)
23 debug
= config
.getDebug()
24 hack83
= config
.getHack83()
25 def debug_write(data
):
28 debug_out
.append('Video.py - ')
30 debug_out
.append(str(x
))
31 fdebug
= open('debug.txt', 'a')
32 fdebug
.write(' '.join(debug_out
))
35 debug_write(['Hack83 is enabled.\n'])
39 CONTENT_TYPE
= 'x-container/tivo-videos'
41 # Used for 8.3's broken requests
45 def hack(self
, handler
, query
, subcname
):
46 debug_write(['Hack new request ------------------------', '\n'])
47 debug_write(['Hack TiVo request is: \n', query
, '\n'])
51 tsn
= handler
.headers
.getheader('tsn', '')
55 debug_write(['Hack this was not a TiVo request.', '\n'])
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:
62 queryAnchor
= unquote("".join(query
['AnchorItem'])).split('Container=')[-1]
63 (leftAnchor
, rightAnchor
) = queryAnchor
.rsplit(os
.path
.sep
, 1)
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'])
71 path
, state
, = self
.request_history
[tsn
]
73 #Never seen this tsn, starting new history
74 debug_write(['New TSN.', '\n'])
77 self
.request_history
[tsn
] = (path
, state
)
78 state
['query'] = query
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):
89 if os
.path
.isdir(full_path
):
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
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
)
115 state
['page'] = files
[0]
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
)]
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'])
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]
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
)
175 #7. this is a request to exit a folder
176 #this request came by itself it must be to exit a folder
178 debug_write(['Hack over 1 second, must be request to exit folder', '\n'])
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
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")
204 tsn
= handler
.headers
.getheader('tsn', '')
206 o
= urlparse("http://fake.host" + handler
.path
)
208 handler
.send_response(200)
209 handler
.end_headers()
210 transcode
.output_video(container
['path'] + path
[len(name
) + 1:],
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
)
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
):
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
))
244 def __getMetadataFromFile(self
, file):
247 if os
.path
.exists(file):
248 for line
in open(file):
249 if line
.strip().startswith('#'):
254 key
, value
= line
.split(':', 1)
256 value
= value
.strip()
258 if key
.startswith('v'):
260 metadata
[key
].append(value
)
262 metadata
[key
] = [value
]
264 metadata
[key
] = value
268 def __metadata(self
, full_path
, tsn
=''):
271 base_path
, title
= os
.path
.split(full_path
)
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
293 metadata
['iso_duration'] = 'P' + str(duration_delta
.days
) + \
294 'DT' + str(hours
) + 'H' + str(min) + \
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
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
)
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()
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()
326 def video_file_filter(full_path
, type = None):
327 if os
.path
.isdir(full_path
):
329 return transcode
.supported_format(full_path
)
331 files
, total
, start
= self
.get_files(handler
, query
, video_file_filter
)
334 local_base_path
= self
.get_local_base_path(handler
, query
)
336 video
= VideoDetails()
337 video
['name'] = os
.path
.split(file)[1]
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
))
347 handler
.send_response(200)
348 handler
.end_headers()
349 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
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'))
373 handler
.wfile
.write(t
)
375 class VideoDetails(DictMixin
):
377 def __init__(self
, d
=None):
383 def __getitem__(self
, key
):
384 if key
not in self
.d
:
385 self
.d
[key
] = self
.default(key
)
388 def __contains__(self
, key
):
391 def __setitem__(self
, key
, value
):
394 def __delitem__(self
):
401 return self
.d
.__iter
__()
404 return self
.d
.iteritems()
406 def default(self
, key
):
409 'episodeNumber' : '0',
410 'displayMajorNumber' : '0',
411 'displayMinorNumber' : '0',
412 'isEpisode' : 'true',
413 'colorCode' : ('COLOR', '4'),
414 'showType' : ('SERIES', '5'),
415 'tvRating' : ('NR', '7')
419 elif key
.startswith('v'):