1 import transcode
, os
, socket
, re
, urllib
, zlib
2 from Cheetah
.Template
import Template
3 from plugin
import Plugin
, quote
, unquote
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 SCRIPTDIR
= os
.path
.dirname(__file__
)
16 extfile
= os
.path
.join(SCRIPTDIR
, 'video.ext')
18 extensions
= file(extfile
).read().split()
22 debug
= config
.getDebug()
23 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
))
36 debug_write(['Hack83 is enabled.\n'])
40 CONTENT_TYPE
= 'x-container/tivo-videos'
42 # Used for 8.3's broken requests
46 def pre_cache(self
, full_path
):
47 if Video
.video_file_filter(self
, full_path
):
48 transcode
.supported_format(full_path
)
50 def video_file_filter(self
, full_path
, type=None):
51 if os
.path
.isdir(full_path
):
54 return os
.path
.splitext(full_path
)[1].lower() in extensions
56 return transcode
.supported_format(full_path
)
58 def hack(self
, handler
, query
, subcname
):
59 debug_write(['Hack new request ------------------------\n'])
60 debug_write(['Hack TiVo request is: \n', query
, '\n'])
64 tsn
= handler
.headers
.getheader('tsn', '')
68 debug_write(['Hack this was not a TiVo request.\n'])
71 # this breaks up the anchor item request into seperate parts
72 if 'AnchorItem' in query
and query
['AnchorItem'] != ['Hack8.3']:
73 queryAnchor
= urllib
.unquote_plus(''.join(query
['AnchorItem']))
74 if queryAnchor
.find('Container=') >= 0:
76 queryAnchor
= queryAnchor
.split('Container=')[-1]
79 queryAnchor
= queryAnchor
.split('/', 1)[-1]
80 leftAnchor
, rightAnchor
= queryAnchor
.rsplit('/', 1)
81 debug_write(['Hack queryAnchor: ', queryAnchor
,
82 ' leftAnchor: ', leftAnchor
,
83 ' rightAnchor: ', rightAnchor
, '\n'])
85 path
, state
= self
.request_history
[tsn
]
87 # Never seen this tsn, starting new history
88 debug_write(['New TSN.\n'])
91 self
.request_history
[tsn
] = (path
, state
)
92 state
['query'] = query
94 state
['time'] = int(time
.time()) + 1000
96 debug_write(['Hack our saved request is: \n', state
['query'], '\n'])
98 current_folder
= subcname
.split('/')[-1]
100 # Begin figuring out what the request TiVo sent us means
101 # There are 7 options that can occur
103 # 1. at the root - This request is always accurate
104 if len(subcname
.split('/')) == 1:
105 debug_write(['Hack we are at the root.',
106 'Saving query, Clearing state[page].\n'])
107 path
[:] = [current_folder
]
108 state
['query'] = query
112 # 2. entering a new folder
113 # If there is no AnchorItem in the request then we must be
114 # entering a new folder.
115 if 'AnchorItem' not in query
:
116 debug_write(['Hack we are entering a new folder.',
117 'Saving query, setting time, setting state[page].\n'])
118 path
[:] = subcname
.split('/')
119 state
['query'] = query
120 state
['time'] = int(time
.time())
121 files
, total
, start
= self
.get_files(handler
, query
,
122 self
.video_file_filter
)
124 state
['page'] = files
[0]
129 # 3. Request a page after pyTivo sent a 302 code
130 # we know this is the proper page
131 if ''.join(query
['AnchorItem']) == 'Hack8.3':
132 debug_write(['Hack requested page from 302 code.',
133 'Returning saved query,\n'])
134 return state
['query'], path
136 # 4. this is a request for a file
137 if 'ItemCount' in query
and int(''.join(query
['ItemCount'])) == 1:
138 debug_write(['Hack requested a file', '\n'])
139 # Everything in this request is right except the container
140 query
['Container'] = ['/'.join(path
)]
144 # All remaining requests could be a second erroneous request for
145 # each of the following we will pause to see if a correct
146 # request is coming right behind it.
148 # Sleep just in case the erroneous request came first this
149 # allows a proper request to be processed first
150 debug_write(['Hack maybe erroneous request, sleeping.\n'])
153 # 5. scrolling in a folder
154 # This could be a request to exit a folder or scroll up or down
156 # First we have to figure out if we are scrolling
157 if 'AnchorOffset' in query
:
158 debug_write(['Hack Anchor offset was in query.',
159 'leftAnchor needs to match ', '/'.join(path
), '\n'])
160 if leftAnchor
== str('/'.join(path
)):
161 debug_write(['Hack leftAnchor matched.', '\n'])
162 query
['Container'] = ['/'.join(path
)]
163 files
, total
, start
= self
.get_files(handler
, query
,
164 self
.video_file_filter
)
165 debug_write(['Hack saved page is= ', state
['page'],
166 ' top returned file is= ', files
[0], '\n'])
167 # If the first file returned equals the top of the page
168 # then we haven't scrolled pages
169 if files
[0] != str(state
['page']):
170 debug_write(['Hack this is scrolling within a folder.\n'])
171 state
['page'] = files
[0]
174 # The only remaining options are exiting a folder or this is a
175 # erroneous second request.
177 # 6. this an extraneous request
178 # this came within a second of a valid request; just use that
180 if (int(time
.time()) - state
['time']) <= 1:
181 debug_write(['Hack erroneous request, send a 302 error', '\n'])
184 # 7. this is a request to exit a folder
185 # this request came by itself; it must be to exit a folder
187 debug_write(['Hack over 1 second,',
188 'must be request to exit folder\n'])
190 state
['query'] = {'Command': query
['Command'],
191 'SortOrder': query
['SortOrder'],
192 'ItemCount': query
['ItemCount'],
193 'Filter': query
['Filter'],
194 'Container': ['/'.join(path
)]}
197 # just in case we missed something.
198 debug_write(['Hack ERROR, should not have made it here. ',
199 'Trying to recover.\n'])
200 return state
['query'], path
202 def send_file(self
, handler
, container
, name
):
203 if handler
.headers
.getheader('Range') and \
204 handler
.headers
.getheader('Range') != 'bytes=0-':
205 handler
.send_response(206)
206 handler
.send_header('Connection', 'close')
207 handler
.send_header('Content-Type', 'video/x-tivo-mpeg')
208 handler
.send_header('Transfer-Encoding', 'chunked')
209 handler
.end_headers()
210 handler
.wfile
.write("\x30\x0D\x0A")
213 tsn
= handler
.headers
.getheader('tsn', '')
215 o
= urlparse("http://fake.host" + handler
.path
)
217 handler
.send_response(200)
218 handler
.end_headers()
219 transcode
.output_video(container
['path'] + path
[len(name
) + 1:],
222 def __isdir(self
, full_path
):
223 return os
.path
.isdir(full_path
)
225 def __duration(self
, full_path
):
226 return transcode
.video_info(full_path
)[4]
228 def __est_size(self
, full_path
, tsn
= ''):
229 # Size is estimated by taking audio and video bit rate adding 2%
231 if transcode
.tivo_compatable(full_path
, tsn
):
232 # Is TiVo-compatible mpeg2
233 return int(os
.stat(full_path
).st_size
)
236 audioBPS
= config
.strtod(config
.getAudioBR(tsn
))
237 videoBPS
= config
.strtod(config
.getVideoBR(tsn
))
238 bitrate
= audioBPS
+ videoBPS
239 return int((self
.__duration
(full_path
) / 1000) *
240 (bitrate
* 1.02 / 8))
242 def __getMetadataFromTxt(self
, full_path
):
245 default_file
= os
.path
.join(os
.path
.split(full_path
)[0], 'default.txt')
246 description_file
= full_path
+ '.txt'
248 metadata
.update(self
.__getMetadataFromFile
(default_file
))
249 metadata
.update(self
.__getMetadataFromFile
(description_file
))
253 def __getMetadataFromFile(self
, file):
256 if os
.path
.exists(file):
257 for line
in open(file):
258 if line
.strip().startswith('#'):
263 key
, value
= line
.split(':', 1)
265 value
= value
.strip()
267 if key
.startswith('v'):
269 metadata
[key
].append(value
)
271 metadata
[key
] = [value
]
273 metadata
[key
] = value
277 def __metadata_basic(self
, full_path
):
280 base_path
, title
= os
.path
.split(full_path
)
281 originalAirDate
= datetime
.fromtimestamp(os
.stat(full_path
).st_ctime
)
283 metadata
['title'] = '.'.join(title
.split('.')[:-1])
284 metadata
['seriesTitle'] = metadata
['title'] # default to the filename
285 metadata
['originalAirDate'] = originalAirDate
.isoformat()
287 metadata
.update(self
.__getMetadataFromTxt
(full_path
))
291 def __metadata_full(self
, full_path
, tsn
=''):
293 metadata
.update(self
.__metadata
_basic
(full_path
))
295 now
= datetime
.utcnow()
297 duration
= self
.__duration
(full_path
)
298 duration_delta
= timedelta(milliseconds
= duration
)
300 metadata
['time'] = now
.isoformat()
301 metadata
['startTime'] = now
.isoformat()
302 metadata
['stopTime'] = (now
+ duration_delta
).isoformat()
304 metadata
.update( self
.__getMetadataFromTxt
(full_path
) )
306 metadata
['size'] = self
.__est
_size
(full_path
, tsn
)
307 metadata
['duration'] = duration
309 min = duration_delta
.seconds
/ 60
310 sec
= duration_delta
.seconds
% 60
313 metadata
['iso_duration'] = 'P' + str(duration_delta
.days
) + \
314 'DT' + str(hours
) + 'H' + str(min) + \
318 def QueryContainer(self
, handler
, query
):
319 tsn
= handler
.headers
.getheader('tsn', '')
320 subcname
= query
['Container'][0]
322 # If you are running 8.3 software you want to enable hack83
327 query
, hackPath
= self
.hack(handler
, query
, subcname
)
328 hackPath
= '/'.join(hackPath
)
329 print 'Tivo said:', subcname
, '|| Hack said:', hackPath
330 debug_write(['Hack Tivo said: ', subcname
, ' || Hack said: ',
335 debug_write(['Hack sending 302 redirect page', '\n'])
336 handler
.send_response(302)
337 handler
.send_header('Location ', 'http://' +
338 handler
.headers
.getheader('host') +
339 '/TiVoConnect?Command=QueryContainer&' +
340 'AnchorItem=Hack8.3&Container=' + hackPath
)
341 handler
.end_headers()
346 cname
= subcname
.split('/')[0]
348 if not handler
.server
.containers
.has_key(cname
) or \
349 not self
.get_local_path(handler
, query
):
350 handler
.send_response(404)
351 handler
.end_headers()
354 container
= handler
.server
.containers
[cname
]
355 precache
= container
.get('precache', 'False').lower() == 'true'
357 files
, total
, start
= self
.get_files(handler
, query
,
358 self
.video_file_filter
)
361 local_base_path
= self
.get_local_base_path(handler
, query
)
363 video
= VideoDetails()
364 video
['name'] = os
.path
.split(file)[1]
366 video
['part_path'] = file.replace(local_base_path
, '', 1)
367 video
['title'] = os
.path
.split(file)[1]
368 video
['is_dir'] = self
.__isdir
(file)
370 video
['small_path'] = subcname
+ '/' + video
['name']
372 if precache
or len(files
) == 1 or file in transcode
.info_cache
:
373 video
['valid'] = transcode
.supported_format(file)
375 video
.update(self
.__metadata
_full
(file, tsn
))
377 video
['valid'] = True
378 video
.update(self
.__metadata
_basic
(file))
382 handler
.send_response(200)
383 handler
.end_headers()
384 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
393 t
.guid
= config
.getGUID()
394 handler
.wfile
.write(t
)
396 def TVBusQuery(self
, handler
, query
):
397 tsn
= handler
.headers
.getheader('tsn', '')
398 file = query
['File'][0]
399 path
= self
.get_local_path(handler
, query
)
400 file_path
= path
+ file
402 file_info
= VideoDetails()
403 file_info
['valid'] = transcode
.supported_format(file_path
)
404 if file_info
['valid']:
405 file_info
.update(self
.__metadata
_full
(file_path
, tsn
))
407 handler
.send_response(200)
408 handler
.end_headers()
409 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'TvBus.tmpl'))
412 handler
.wfile
.write(t
)
414 class VideoDetails(DictMixin
):
416 def __init__(self
, d
=None):
422 def __getitem__(self
, key
):
423 if key
not in self
.d
:
424 self
.d
[key
] = self
.default(key
)
427 def __contains__(self
, key
):
430 def __setitem__(self
, key
, value
):
433 def __delitem__(self
):
440 return self
.d
.__iter
__()
443 return self
.d
.iteritems()
445 def default(self
, key
):
448 'episodeNumber' : '0',
449 'displayMajorNumber' : '0',
450 'displayMinorNumber' : '0',
451 'isEpisode' : 'true',
452 'colorCode' : ('COLOR', '4'),
453 'showType' : ('SERIES', '5'),
454 'tvRating' : ('NR', '7')
458 elif key
.startswith('v'):