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
14 SCRIPTDIR
= os
.path
.dirname(__file__
)
18 extfile
= os
.path
.join(SCRIPTDIR
, 'video.ext')
20 extensions
= file(extfile
).read().split()
24 if config
.getHack83():
25 logging
.getLogger('pyTivo.hack83').info('Hack83 is enabled.')
29 CONTENT_TYPE
= 'x-container/tivo-videos'
31 # Used for 8.3's broken requests
35 def pre_cache(self
, full_path
):
36 if Video
.video_file_filter(self
, full_path
):
37 transcode
.supported_format(full_path
)
39 def video_file_filter(self
, full_path
, type=None):
40 if os
.path
.isdir(full_path
):
43 return os
.path
.splitext(full_path
)[1].lower() in extensions
45 return transcode
.supported_format(full_path
)
47 def hack(self
, handler
, query
, subcname
):
48 logger
= logging
.getLogger('pyTivo.hack83')
49 logger
.debug('new request ------------------------')
50 logger
.debug('TiVo request is: \n%s' % query
)
54 tsn
= handler
.headers
.getheader('tsn', '')
58 logger
.debug('this was not a TiVo request. Using default tsn.')
61 # this breaks up the anchor item request into seperate parts
62 if 'AnchorItem' in query
and query
['AnchorItem'] != ['Hack8.3']:
63 queryAnchor
= urllib
.unquote_plus(''.join(query
['AnchorItem']))
64 if queryAnchor
.find('Container=') >= 0:
66 queryAnchor
= queryAnchor
.split('Container=')[-1]
69 queryAnchor
= queryAnchor
.split('/', 1)[-1]
70 leftAnchor
, rightAnchor
= queryAnchor
.rsplit('/', 1)
71 logger
.debug('queryAnchor:%s \n leftAnchor:%s\n rightAnchor: %s' %
72 (queryAnchor
, leftAnchor
, rightAnchor
))
74 path
, state
= self
.request_history
[tsn
]
76 # Never seen this tsn, starting new history
77 logger
.debug('New TSN.')
80 self
.request_history
[tsn
] = (path
, state
)
81 state
['query'] = query
83 state
['time'] = int(time
.time()) + 1000
85 logger
.debug('our saved request is: \n%s' % state
['query'])
87 current_folder
= subcname
.split('/')[-1]
89 # Begin figuring out what the request TiVo sent us means
90 # There are 7 options that can occur
92 # 1. at the root - This request is always accurate
93 if len(subcname
.split('/')) == 1:
94 logger
.debug('we are at the root. Saving query, Clearing state[page].')
95 path
[:] = [current_folder
]
96 state
['query'] = query
100 # 2. entering a new folder
101 # If there is no AnchorItem in the request then we must be
102 # entering a new folder.
103 if 'AnchorItem' not in query
:
104 logger
.debug('we are entering a new folder. Saving query, setting time, setting state[page].')
105 path
[:] = subcname
.split('/')
106 state
['query'] = query
107 state
['time'] = int(time
.time())
108 files
, total
, start
= self
.get_files(handler
, query
,
109 self
.video_file_filter
)
111 state
['page'] = files
[0]
116 # 3. Request a page after pyTivo sent a 302 code
117 # we know this is the proper page
118 if ''.join(query
['AnchorItem']) == 'Hack8.3':
119 logger
.debug('requested page from 302 code. Returning saved query.')
120 return state
['query'], path
122 # 4. this is a request for a file
123 if 'ItemCount' in query
and int(''.join(query
['ItemCount'])) == 1:
124 logger
.debug('requested a file')
125 # Everything in this request is right except the container
126 query
['Container'] = ['/'.join(path
)]
130 # All remaining requests could be a second erroneous request for
131 # each of the following we will pause to see if a correct
132 # request is coming right behind it.
134 # Sleep just in case the erroneous request came first this
135 # allows a proper request to be processed first
136 logger
.debug('maybe erroneous request, sleeping.')
139 # 5. scrolling in a folder
140 # This could be a request to exit a folder or scroll up or down
142 # First we have to figure out if we are scrolling
143 if 'AnchorOffset' in query
:
144 logger
.debug('Anchor offset was in query. leftAnchor needs to match %s' % '/'.join(path
))
145 if leftAnchor
== str('/'.join(path
)):
146 debug_write(__name__
, fn_attr(), ['leftAnchor matched.'])
147 query
['Container'] = ['/'.join(path
)]
148 files
, total
, start
= self
.get_files(handler
, query
,
149 self
.video_file_filter
)
150 logger
.debug('saved page is=%s top returned file is= %s' % (state
['page'], files
[0]))
151 # If the first file returned equals the top of the page
152 # then we haven't scrolled pages
153 if files
[0] != str(state
['page']):
154 logger
.debug('this is scrolling within a folder.')
155 state
['page'] = files
[0]
158 # The only remaining options are exiting a folder or this is a
159 # erroneous second request.
161 # 6. this an extraneous request
162 # this came within a second of a valid request; just use that
164 if (int(time
.time()) - state
['time']) <= 1:
165 logger
.debug('erroneous request, send a 302 error')
168 # 7. this is a request to exit a folder
169 # this request came by itself; it must be to exit a folder
171 logger
.debug('over 1 second must be request to exit folder')
173 state
['query'] = {'Command': query
['Command'],
174 'SortOrder': query
['SortOrder'],
175 'ItemCount': query
['ItemCount'],
176 'Filter': query
['Filter'],
177 'Container': ['/'.join(path
)]}
180 # just in case we missed something.
181 logger
.debug('ERROR, should not have made it here Trying to recover.')
182 return state
['query'], path
184 def send_file(self
, handler
, container
, name
):
185 if handler
.headers
.getheader('Range') and \
186 handler
.headers
.getheader('Range') != 'bytes=0-':
187 handler
.send_response(206)
188 handler
.send_header('Connection', 'close')
189 handler
.send_header('Content-Type', 'video/x-tivo-mpeg')
190 handler
.send_header('Transfer-Encoding', 'chunked')
191 handler
.end_headers()
192 handler
.wfile
.write("\x30\x0D\x0A")
195 tsn
= handler
.headers
.getheader('tsn', '')
197 o
= urlparse("http://fake.host" + handler
.path
)
199 handler
.send_response(200)
200 handler
.end_headers()
201 transcode
.output_video(container
['path'] + path
[len(name
) + 1:],
204 def __isdir(self
, full_path
):
205 return os
.path
.isdir(full_path
)
207 def __duration(self
, full_path
):
208 return transcode
.video_info(full_path
)[4]
210 def __total_items(self
, full_path
):
213 for file in os
.listdir(full_path
):
214 if file.startswith('.'):
216 file = os
.path
.join(full_path
, file)
217 if os
.path
.isdir(file):
220 if os
.path
.splitext(file)[1].lower() in extensions
:
222 elif file in transcode
.info_cache
:
223 if transcode
.supported_format(file):
229 def __est_size(self
, full_path
, tsn
= ''):
230 # Size is estimated by taking audio and video bit rate adding 2%
232 if transcode
.tivo_compatable(full_path
, tsn
):
233 # Is TiVo-compatible mpeg2
234 return int(os
.stat(full_path
).st_size
)
237 if config
.getAudioCodec(tsn
) == None:
238 audioBPS
= config
.getMaxAudioBR(tsn
)*1000
240 audioBPS
= config
.strtod(config
.getAudioBR(tsn
))
241 videoBPS
= config
.strtod(config
.getVideoBR(tsn
))
242 bitrate
= audioBPS
+ videoBPS
243 return int((self
.__duration
(full_path
) / 1000) *
244 (bitrate
* 1.02 / 8))
246 def __getMetadataFromTxt(self
, full_path
):
249 default_meta
= os
.path
.join(os
.path
.split(full_path
)[0], 'default.txt')
250 standard_meta
= full_path
+ '.txt'
251 subdir_meta
= os
.path
.join(os
.path
.dirname(full_path
), '.meta',
252 os
.path
.basename(full_path
)) + '.txt'
254 for metafile
in (default_meta
, standard_meta
, subdir_meta
):
255 metadata
.update(self
.__getMetadataFromFile
(metafile
))
259 def __getMetadataFromFile(self
, file):
262 if os
.path
.exists(file):
263 for line
in open(file):
264 if line
.strip().startswith('#'):
269 key
, value
= line
.split(':', 1)
271 value
= value
.strip()
273 if key
.startswith('v'):
275 metadata
[key
].append(value
)
277 metadata
[key
] = [value
]
279 metadata
[key
] = value
283 def metadata_basic(self
, full_path
):
286 base_path
, title
= os
.path
.split(full_path
)
287 originalAirDate
= datetime
.fromtimestamp(os
.stat(full_path
).st_ctime
)
289 metadata
['title'] = '.'.join(title
.split('.')[:-1])
290 metadata
['seriesTitle'] = metadata
['title'] # default to the filename
291 metadata
['originalAirDate'] = originalAirDate
.isoformat()
293 metadata
.update(self
.__getMetadataFromTxt
(full_path
))
297 def metadata_full(self
, full_path
, tsn
=''):
299 metadata
.update(self
.metadata_basic(full_path
))
301 now
= datetime
.utcnow()
303 duration
= self
.__duration
(full_path
)
304 duration_delta
= timedelta(milliseconds
= duration
)
306 metadata
['time'] = now
.isoformat()
307 metadata
['startTime'] = now
.isoformat()
308 metadata
['stopTime'] = (now
+ duration_delta
).isoformat()
309 metadata
['size'] = self
.__est
_size
(full_path
, tsn
)
310 metadata
['duration'] = duration
312 min = duration_delta
.seconds
/ 60
313 sec
= duration_delta
.seconds
% 60
316 metadata
['iso_duration'] = 'P' + str(duration_delta
.days
) + \
317 'DT' + str(hours
) + 'H' + str(min) + \
321 def QueryContainer(self
, handler
, query
):
322 tsn
= handler
.headers
.getheader('tsn', '')
323 subcname
= query
['Container'][0]
325 # If you are running 8.3 software you want to enable hack83
327 if config
.getHack83():
328 logger
= logging
.getLogger('pyTivo.hack83')
329 logger
.debug('=' * 73)
330 query
, hackPath
= self
.hack(handler
, query
, subcname
)
331 hackPath
= '/'.join(hackPath
)
332 logger
.debug('Tivo said: %s || Hack said: %s' % (subcname
, hackPath
))
336 logger
.debug('sending 302 redirect page')
337 handler
.send_response(302)
338 handler
.send_header('Location ', 'http://' +
339 handler
.headers
.getheader('host') +
340 '/TiVoConnect?Command=QueryContainer&' +
341 'AnchorItem=Hack8.3&Container=' + hackPath
)
342 handler
.end_headers()
347 cname
= subcname
.split('/')[0]
349 if not handler
.server
.containers
.has_key(cname
) or \
350 not self
.get_local_path(handler
, query
):
351 handler
.send_response(404)
352 handler
.end_headers()
355 container
= handler
.server
.containers
[cname
]
356 precache
= container
.get('precache', 'False').lower() == 'true'
358 files
, total
, start
= self
.get_files(handler
, query
,
359 self
.video_file_filter
)
362 local_base_path
= self
.get_local_base_path(handler
, query
)
364 mtime
= datetime
.fromtimestamp(os
.stat(file).st_mtime
)
365 video
= VideoDetails()
366 video
['captureDate'] = hex(int(time
.mktime(mtime
.timetuple())))
367 video
['name'] = os
.path
.split(file)[1]
369 video
['part_path'] = file.replace(local_base_path
, '', 1)
370 video
['title'] = os
.path
.split(file)[1]
371 video
['is_dir'] = self
.__isdir
(file)
373 video
['small_path'] = subcname
+ '/' + video
['name']
374 video
['total_items'] = self
.__total
_items
(file)
376 if precache
or len(files
) == 1 or file in transcode
.info_cache
:
377 video
['valid'] = transcode
.supported_format(file)
379 video
.update(self
.metadata_full(file, tsn
))
381 video
['valid'] = True
382 video
.update(self
.metadata_basic(file))
386 handler
.send_response(200)
387 handler
.end_headers()
388 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
397 t
.guid
= config
.getGUID()
398 t
.tivos
= handler
.tivos
399 handler
.wfile
.write(t
)
401 def TVBusQuery(self
, handler
, query
):
402 tsn
= handler
.headers
.getheader('tsn', '')
403 file = query
['File'][0]
404 path
= self
.get_local_path(handler
, query
)
405 file_path
= path
+ file
407 file_info
= VideoDetails()
408 file_info
['valid'] = transcode
.supported_format(file_path
)
409 if file_info
['valid']:
410 file_info
.update(self
.metadata_full(file_path
, tsn
))
412 handler
.send_response(200)
413 handler
.end_headers()
414 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'TvBus.tmpl'))
417 handler
.wfile
.write(t
)
419 def XSL(self
, handler
, query
):
420 file = open(os
.path
.join(SCRIPTDIR
, 'templates', 'container.xsl'))
421 handler
.send_response(200)
422 handler
.end_headers()
423 handler
.wfile
.write(file.read())
426 def Push(self
, handler
, query
):
427 file = unquote(query
['File'][0])
428 tsn
= query
['tsn'][0]
429 path
= self
.get_local_path(handler
, query
)
430 file_path
= path
+ file
432 file_info
= VideoDetails()
433 file_info
['valid'] = transcode
.supported_format(file_path
)
434 if file_info
['valid']:
435 file_info
.update(self
.metadata_full(file_path
, tsn
))
438 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
439 s
.connect(('tivo.com',123))
440 ip
= s
.getsockname()[0]
441 container
= quote(query
['Container'][0].split('/')[0])
442 port
= config
.getPort()
444 url
= 'http://%s:%s/%s%s' % (ip
, port
, container
, quote(file))
451 description
= file_info
['description'],
452 duration
= file_info
['duration'] / 1000,
453 size
= file_info
['size'],
454 title
= file_info
['title'],
455 subtitle
= file_info
['name'])
458 handler
.send_response(500)
459 handler
.end_headers()
460 handler
.wfile
.write('%s\n\n%s' % (e
, traceback
.format_exc() ))
463 referer
= handler
.headers
.getheader('Referer')
464 handler
.send_response(302)
465 handler
.send_header('Location', referer
)
466 handler
.end_headers()
469 class VideoDetails(DictMixin
):
471 def __init__(self
, d
=None):
477 def __getitem__(self
, key
):
478 if key
not in self
.d
:
479 self
.d
[key
] = self
.default(key
)
482 def __contains__(self
, key
):
485 def __setitem__(self
, key
, value
):
488 def __delitem__(self
):
495 return self
.d
.__iter
__()
498 return self
.d
.iteritems()
500 def default(self
, key
):
503 'episodeNumber' : '0',
504 'displayMajorNumber' : '0',
505 'displayMinorNumber' : '0',
506 'isEpisode' : 'true',
507 'colorCode' : ('COLOR', '4'),
508 'showType' : ('SERIES', '5'),
509 'tvRating' : ('NR', '7')
513 elif key
.startswith('v'):