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 # Preload the templates
19 tcname
= os
.path
.join(SCRIPTDIR
, 'templates', 'container.tmpl')
20 ttname
= os
.path
.join(SCRIPTDIR
, 'templates', 'TvBus.tmpl')
21 txname
= os
.path
.join(SCRIPTDIR
, 'templates', 'container.xsl')
22 CONTAINER_TEMPLATE
= file(tcname
, 'rb').read()
23 TVBUS_TEMPLATE
= file(ttname
, 'rb').read()
24 XSL_TEMPLATE
= file(txname
, 'rb').read()
26 extfile
= os
.path
.join(SCRIPTDIR
, 'video.ext')
28 extensions
= file(extfile
).read().split()
32 if config
.getHack83():
33 logging
.getLogger('pyTivo.hack83').info('Hack83 is enabled.')
37 CONTENT_TYPE
= 'x-container/tivo-videos'
39 # Used for 8.3's broken requests
43 def pre_cache(self
, full_path
):
44 if Video
.video_file_filter(self
, full_path
):
45 transcode
.supported_format(full_path
)
47 def video_file_filter(self
, full_path
, type=None):
48 if os
.path
.isdir(full_path
):
51 return os
.path
.splitext(full_path
)[1].lower() in extensions
53 return transcode
.supported_format(full_path
)
55 def hack(self
, handler
, query
, subcname
):
56 logger
= logging
.getLogger('pyTivo.hack83')
57 logger
.debug('new request ------------------------')
58 logger
.debug('TiVo request is: \n%s' % query
)
62 tsn
= handler
.headers
.getheader('tsn', '')
66 logger
.debug('this was not a TiVo request. Using default tsn.')
69 # this breaks up the anchor item request into seperate parts
70 if 'AnchorItem' in query
and query
['AnchorItem'] != ['Hack8.3']:
71 queryAnchor
= urllib
.unquote_plus(''.join(query
['AnchorItem']))
72 if queryAnchor
.find('Container=') >= 0:
74 queryAnchor
= queryAnchor
.split('Container=')[-1]
77 queryAnchor
= queryAnchor
.split('/', 1)[-1]
78 leftAnchor
, rightAnchor
= queryAnchor
.rsplit('/', 1)
79 logger
.debug('queryAnchor:%s \n leftAnchor:%s\n rightAnchor: %s' %
80 (queryAnchor
, leftAnchor
, rightAnchor
))
82 path
, state
= self
.request_history
[tsn
]
84 # Never seen this tsn, starting new history
85 logger
.debug('New TSN.')
88 self
.request_history
[tsn
] = (path
, state
)
89 state
['query'] = query
91 state
['time'] = int(time
.time()) + 1000
93 logger
.debug('our saved request is: \n%s' % state
['query'])
95 current_folder
= subcname
.split('/')[-1]
97 # Begin figuring out what the request TiVo sent us means
98 # There are 7 options that can occur
100 # 1. at the root - This request is always accurate
101 if len(subcname
.split('/')) == 1:
102 logger
.debug('we are at the root. Saving query, Clearing state[page].')
103 path
[:] = [current_folder
]
104 state
['query'] = query
108 # 2. entering a new folder
109 # If there is no AnchorItem in the request then we must be
110 # entering a new folder.
111 if 'AnchorItem' not in query
:
112 logger
.debug('we are entering a new folder. Saving query, setting time, setting state[page].')
113 path
[:] = subcname
.split('/')
114 state
['query'] = query
115 state
['time'] = int(time
.time())
116 files
, total
, start
= self
.get_files(handler
, query
,
117 self
.video_file_filter
)
119 state
['page'] = files
[0]
124 # 3. Request a page after pyTivo sent a 302 code
125 # we know this is the proper page
126 if ''.join(query
['AnchorItem']) == 'Hack8.3':
127 logger
.debug('requested page from 302 code. Returning saved query.')
128 return state
['query'], path
130 # 4. this is a request for a file
131 if 'ItemCount' in query
and int(''.join(query
['ItemCount'])) == 1:
132 logger
.debug('requested a file')
133 # Everything in this request is right except the container
134 query
['Container'] = ['/'.join(path
)]
138 # All remaining requests could be a second erroneous request for
139 # each of the following we will pause to see if a correct
140 # request is coming right behind it.
142 # Sleep just in case the erroneous request came first this
143 # allows a proper request to be processed first
144 logger
.debug('maybe erroneous request, sleeping.')
147 # 5. scrolling in a folder
148 # This could be a request to exit a folder or scroll up or down
150 # First we have to figure out if we are scrolling
151 if 'AnchorOffset' in query
:
152 logger
.debug('Anchor offset was in query. leftAnchor needs to match %s' % '/'.join(path
))
153 if leftAnchor
== str('/'.join(path
)):
154 logger
.debug('leftAnchor matched.')
155 query
['Container'] = ['/'.join(path
)]
156 files
, total
, start
= self
.get_files(handler
, query
,
157 self
.video_file_filter
)
158 logger
.debug('saved page is=%s top returned file is= %s' % (state
['page'], files
[0]))
159 # If the first file returned equals the top of the page
160 # then we haven't scrolled pages
161 if files
[0] != str(state
['page']):
162 logger
.debug('this is scrolling within a folder.')
163 state
['page'] = files
[0]
166 # The only remaining options are exiting a folder or this is a
167 # erroneous second request.
169 # 6. this an extraneous request
170 # this came within a second of a valid request; just use that
172 if (int(time
.time()) - state
['time']) <= 1:
173 logger
.debug('erroneous request, send a 302 error')
176 # 7. this is a request to exit a folder
177 # this request came by itself; it must be to exit a folder
179 logger
.debug('over 1 second must be request to exit folder')
181 state
['query'] = {'Command': query
['Command'],
182 'SortOrder': query
['SortOrder'],
183 'ItemCount': query
['ItemCount'],
184 'Filter': query
['Filter'],
185 'Container': ['/'.join(path
)]}
186 files
, total
, start
= self
.get_files(handler
, state
['query'],
187 self
.video_file_filter
)
189 state
['page'] = files
[0]
194 # just in case we missed something.
195 logger
.debug('ERROR, should not have made it here Trying to recover.')
196 return state
['query'], path
198 def send_file(self
, handler
, container
, name
):
199 if handler
.headers
.getheader('Range') and \
200 handler
.headers
.getheader('Range') != 'bytes=0-':
201 handler
.send_response(206)
202 handler
.send_header('Connection', 'close')
203 handler
.send_header('Content-Type', 'video/x-tivo-mpeg')
204 handler
.send_header('Transfer-Encoding', 'chunked')
205 handler
.end_headers()
206 handler
.wfile
.write("\x30\x0D\x0A")
209 tsn
= handler
.headers
.getheader('tsn', '')
211 o
= urlparse("http://fake.host" + handler
.path
)
213 handler
.send_response(200)
214 handler
.end_headers()
215 transcode
.output_video(container
['path'] + path
[len(name
) + 1:],
218 def __isdir(self
, full_path
):
219 return os
.path
.isdir(full_path
)
221 def __duration(self
, full_path
):
222 return transcode
.video_info(full_path
)['millisecs']
224 def __total_items(self
, full_path
):
227 for file in os
.listdir(full_path
):
228 if file.startswith('.'):
230 file = os
.path
.join(full_path
, file)
231 if os
.path
.isdir(file):
234 if os
.path
.splitext(file)[1].lower() in extensions
:
236 elif file in transcode
.info_cache
:
237 if transcode
.supported_format(file):
243 def __est_size(self
, full_path
, tsn
= ''):
244 # Size is estimated by taking audio and video bit rate adding 2%
246 if transcode
.tivo_compatable(full_path
, tsn
)[0]:
247 # Is TiVo-compatible mpeg2
248 return int(os
.stat(full_path
).st_size
)
251 if config
.getAudioCodec(tsn
) == None:
252 audioBPS
= config
.getMaxAudioBR(tsn
)*1000
254 audioBPS
= config
.strtod(config
.getAudioBR(tsn
))
255 videoBPS
= transcode
.select_videostr(full_path
, tsn
)
256 bitrate
= audioBPS
+ videoBPS
257 return int((self
.__duration
(full_path
) / 1000) *
258 (bitrate
* 1.02 / 8))
260 def getMetadataFromTxt(self
, full_path
):
263 default_meta
= os
.path
.join(os
.path
.split(full_path
)[0], 'default.txt')
264 standard_meta
= full_path
+ '.txt'
265 subdir_meta
= os
.path
.join(os
.path
.dirname(full_path
), '.meta',
266 os
.path
.basename(full_path
)) + '.txt'
268 for metafile
in (default_meta
, standard_meta
, subdir_meta
):
269 metadata
.update(self
.__getMetadataFromFile
(metafile
))
273 def __getMetadataFromFile(self
, file):
276 if os
.path
.exists(file):
277 for line
in open(file):
278 if line
.strip().startswith('#'):
283 key
, value
= line
.split(':', 1)
285 value
= value
.strip()
287 if key
.startswith('v'):
289 metadata
[key
].append(value
)
291 metadata
[key
] = [value
]
293 metadata
[key
] = value
297 def metadata_basic(self
, full_path
):
300 base_path
, title
= os
.path
.split(full_path
)
301 ctime
= os
.stat(full_path
).st_ctime
302 if (ctime
< 0): ctime
= 0
303 originalAirDate
= datetime
.fromtimestamp(ctime
)
305 metadata
['title'] = '.'.join(title
.split('.')[:-1])
306 metadata
['seriesTitle'] = metadata
['title'] # default to the filename
307 metadata
['originalAirDate'] = originalAirDate
.isoformat()
309 metadata
.update(self
.getMetadataFromTxt(full_path
))
313 def metadata_full(self
, full_path
, tsn
=''):
316 now
= datetime
.utcnow()
318 duration
= self
.__duration
(full_path
)
319 duration_delta
= timedelta(milliseconds
= duration
)
321 metadata
['time'] = now
.isoformat()
322 metadata
['startTime'] = now
.isoformat()
323 metadata
['stopTime'] = (now
+ duration_delta
).isoformat()
324 metadata
['size'] = self
.__est
_size
(full_path
, tsn
)
325 metadata
['duration'] = duration
326 vInfo
= transcode
.video_info(full_path
)
327 transcode_options
= {}
328 if not transcode
.tivo_compatable(full_path
, tsn
)[0]:
329 transcode_options
= transcode
.transcode(True, full_path
, '', tsn
)
330 metadata
['vHost'] = [str(transcode
.tivo_compatable(full_path
, tsn
)[1])]+\
331 ['SOURCE INFO: ']+["%s=%s" % (k
, v
) for k
, v
in sorted(transcode
.video_info(full_path
).items(), reverse
=True)]+\
332 ['TRANSCODE OPTIONS: ']+["%s" % (v
) for k
, v
in transcode_options
.items()]+\
333 ['SOURCE FILE: ']+[str(os
.path
.split(full_path
)[1])]
334 if not (full_path
[-5:]).lower() == '.tivo':
335 if (int(vInfo
['vHeight']) >= 720 and config
.getTivoHeight
>= 720) or \
336 (int(vInfo
['vWidth']) >= 1280 and config
.getTivoWidth
>= 1280):
337 metadata
['showingBits'] = '4096'
339 metadata
.update(self
.metadata_basic(full_path
))
341 min = duration_delta
.seconds
/ 60
342 sec
= duration_delta
.seconds
% 60
345 metadata
['iso_duration'] = 'P' + str(duration_delta
.days
) + \
346 'DT' + str(hours
) + 'H' + str(min) + \
350 def QueryContainer(self
, handler
, query
):
351 tsn
= handler
.headers
.getheader('tsn', '')
352 subcname
= query
['Container'][0]
354 # If you are running 8.3 software you want to enable hack83
356 if config
.getHack83():
357 logger
= logging
.getLogger('pyTivo.hack83')
358 logger
.debug('=' * 73)
359 query
, hackPath
= self
.hack(handler
, query
, subcname
)
360 hackPath
= '/'.join(hackPath
)
361 logger
.debug('Tivo said: %s || Hack said: %s' % (subcname
, hackPath
))
365 logger
.debug('sending 302 redirect page')
366 handler
.send_response(302)
367 handler
.send_header('Location ', 'http://' +
368 handler
.headers
.getheader('host') +
369 '/TiVoConnect?Command=QueryContainer&' +
370 'AnchorItem=Hack8.3&Container=' + quote(hackPath
))
371 handler
.end_headers()
376 cname
= subcname
.split('/')[0]
378 if not handler
.server
.containers
.has_key(cname
) or \
379 not self
.get_local_path(handler
, query
):
380 handler
.send_response(404)
381 handler
.end_headers()
384 container
= handler
.server
.containers
[cname
]
385 precache
= container
.get('precache', 'False').lower() == 'true'
387 files
, total
, start
= self
.get_files(handler
, query
,
388 self
.video_file_filter
)
391 local_base_path
= self
.get_local_base_path(handler
, query
)
393 mtime
= os
.stat(file).st_mtime
394 if (mtime
< 0): mtime
= 0
395 mtime
= datetime
.fromtimestamp(mtime
)
396 video
= VideoDetails()
397 video
['captureDate'] = hex(int(time
.mktime(mtime
.timetuple())))
398 video
['name'] = os
.path
.split(file)[1]
400 video
['part_path'] = file.replace(local_base_path
, '', 1)
401 if not video
['part_path'].startswith(os
.path
.sep
):
402 video
['part_path'] = os
.path
.sep
+ video
['part_path']
403 video
['title'] = os
.path
.split(file)[1]
404 video
['is_dir'] = self
.__isdir
(file)
406 video
['small_path'] = subcname
+ '/' + video
['name']
407 video
['total_items'] = self
.__total
_items
(file)
409 if precache
or len(files
) == 1 or file in transcode
.info_cache
:
410 video
['valid'] = transcode
.supported_format(file)
412 video
.update(self
.metadata_full(file, tsn
))
414 video
['valid'] = True
415 video
.update(self
.metadata_basic(file))
419 handler
.send_response(200)
420 handler
.end_headers()
421 t
= Template(CONTAINER_TEMPLATE
)
430 t
.guid
= config
.getGUID()
431 t
.tivos
= handler
.tivos
432 t
.tivo_names
= handler
.tivo_names
433 handler
.wfile
.write(t
)
435 def TVBusQuery(self
, handler
, query
):
436 tsn
= handler
.headers
.getheader('tsn', '')
437 file = query
['File'][0]
438 path
= self
.get_local_path(handler
, query
)
439 file_path
= path
+ file
441 file_info
= VideoDetails()
442 file_info
['valid'] = transcode
.supported_format(file_path
)
443 if file_info
['valid']:
444 file_info
.update(self
.metadata_full(file_path
, tsn
))
446 handler
.send_response(200)
447 handler
.end_headers()
448 t
= Template(TVBUS_TEMPLATE
)
451 handler
.wfile
.write(t
)
453 def XSL(self
, handler
, query
):
454 handler
.send_response(200)
455 handler
.end_headers()
456 handler
.wfile
.write(XSL_TEMPLATE
)
458 def Push(self
, handler
, query
):
459 file = unquote(query
['File'][0])
461 tsn
= query
['tsn'][0]
462 for key
in handler
.tivo_names
:
463 if handler
.tivo_names
[key
] == tsn
:
467 path
= self
.get_local_path(handler
, query
)
468 file_path
= path
+ file
470 file_info
= VideoDetails()
471 file_info
['valid'] = transcode
.supported_format(file_path
)
472 if file_info
['valid']:
473 file_info
.update(self
.metadata_full(file_path
, tsn
))
476 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
477 s
.connect(('tivo.com',123))
478 ip
= s
.getsockname()[0]
479 container
= quote(query
['Container'][0].split('/')[0])
480 port
= config
.getPort()
482 url
= 'http://%s:%s/%s%s' % (ip
, port
, container
, quote(file))
489 description
= file_info
['description'],
490 duration
= file_info
['duration'] / 1000,
491 size
= file_info
['size'],
492 title
= file_info
['title'],
493 subtitle
= file_info
['name'])
496 handler
.send_response(500)
497 handler
.end_headers()
498 handler
.wfile
.write('%s\n\n%s' % (e
, traceback
.format_exc() ))
501 referer
= handler
.headers
.getheader('Referer')
502 handler
.send_response(302)
503 handler
.send_header('Location', referer
)
504 handler
.end_headers()
507 class VideoDetails(DictMixin
):
509 def __init__(self
, d
=None):
515 def __getitem__(self
, key
):
516 if key
not in self
.d
:
517 self
.d
[key
] = self
.default(key
)
520 def __contains__(self
, key
):
523 def __setitem__(self
, key
, value
):
526 def __delitem__(self
):
533 return self
.d
.__iter
__()
536 return self
.d
.iteritems()
538 def default(self
, key
):
541 'episodeNumber' : '0',
542 'displayMajorNumber' : '0',
543 'displayMinorNumber' : '0',
544 'isEpisode' : 'true',
545 'colorCode' : ('COLOR', '4'),
546 'showType' : ('SERIES', '5'),
547 'tvRating' : ('NR', '7')
551 elif key
.startswith('v'):