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
13 SCRIPTDIR
= os
.path
.dirname(__file__
)
17 debug
= config
.getDebug()
18 hack83
= config
.getHack83()
19 def debug_write(data
):
22 debug_out
.append('Video.py - ')
24 debug_out
.append(str(x
))
25 fdebug
= open('debug.txt', 'a')
26 fdebug
.write(' '.join(debug_out
))
29 debug_write(['Hack83 is enabled.\n'])
33 CONTENT_TYPE
= 'x-container/tivo-videos'
35 # Used for 8.3's broken requests
39 def hack(self
, handler
, query
, subcname
):
40 debug_write(['Hack new request ------------------------', '\n'])
41 debug_write(['Hack TiVo request is: \n', query
, '\n'])
45 tsn
= handler
.headers
.getheader('tsn', '')
49 debug_write(['Hack this was not a TiVo request.', '\n'])
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:
56 queryAnchor
= unquote_plus("".join(query
['AnchorItem'])).split('Container=')[-1]
57 (leftAnchor
, rightAnchor
) = queryAnchor
.rsplit('/', 1)
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'])
65 path
, state
, = self
.request_history
[tsn
]
67 #Never seen this tsn, starting new history
68 debug_write(['New TSN.', '\n'])
71 self
.request_history
[tsn
] = (path
, state
)
72 state
['query'] = query
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
):
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
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
)
110 state
['page'] = files
[0]
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
)]
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'])
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]
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
)
169 #7. this is a request to exit a folder
170 #this request came by itself it must be to exit a folder
172 debug_write(['Hack over 1 second, must be request to exit folder', '\n'])
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
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")
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
):
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
))
237 def __getMetadataFromFile(self
, file):
240 if os
.path
.exists(file):
241 for line
in open(file):
242 if line
.strip().startswith('#'):
247 key
, value
= line
.split(':', 1)
249 value
= value
.strip()
251 if key
.startswith('v'):
253 metadata
[key
].append(value
)
255 metadata
[key
] = [value
]
257 metadata
[key
] = value
261 def __metadata(self
, full_path
):
265 base_path
, title
= os
.path
.split(full_path
)
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
287 metadata
['iso_durarion'] = 'P' + str(duration_delta
.days
) + 'DT' + str(hours
) + 'H' + str(min) + 'M' + str(sec
) + 'S'
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
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
)
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()
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()
318 def video_file_filter(file, type = None):
320 if os
.path
.isdir(full_path
):
322 return transcode
.suported_format(full_path
)
324 files
, total
, start
= self
.get_files(handler
, query
, video_file_filter
)
328 video
= VideoDetails()
329 video
['name'] = os
.path
.split(file)[1]
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))
338 handler
.send_response(200)
339 handler
.end_headers()
340 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
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'))
363 handler
.wfile
.write(t
)
365 class VideoDetails(DictMixin
):
367 def __init__(self
, d
= None):
373 def __getitem__(self
, key
):
374 if key
not in self
.d
:
375 self
.d
[key
] = self
.default(key
)
378 def __contains__(self
, key
):
381 def __setitem__(self
, key
, value
):
384 def __delitem__(self
):
391 return self
.d
.__iter
__()
394 return self
.d
.iteritems()
396 def default(self
, key
):
399 'episodeNumber' : '0',
400 'displayMajorNumber' : '0',
401 'displayMinorNumber' : '0',
402 'isEpisode' : 'true',
403 'colorCode' : ('COLOR', '4'),
404 'showType' : ('SERIES', '5'),
405 'tvRating' : ('NR', '7'),
409 elif key
.startswith('v'):
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
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])?$')
423 raise SyntaxError('Invalid bit value syntax')
424 (coef
, prefix
, power
, byte
) = m
.groups()
428 exponent
= float(prefixes
[prefix
])
431 value
= float(coef
) * pow(2.0, exponent
/0.3)
434 value
= float(coef
) * pow(10.0, exponent
)
435 if byte
== "B": # B==Byte, b=bit