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 logger
.debug('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
)]}
178 files
, total
, start
= self
.get_files(handler
, state
['query'],
179 self
.video_file_filter
)
181 state
['page'] = files
[0]
186 # just in case we missed something.
187 logger
.debug('ERROR, should not have made it here Trying to recover.')
188 return state
['query'], path
190 def send_file(self
, handler
, container
, name
):
191 if handler
.headers
.getheader('Range') and \
192 handler
.headers
.getheader('Range') != 'bytes=0-':
193 handler
.send_response(206)
194 handler
.send_header('Connection', 'close')
195 handler
.send_header('Content-Type', 'video/x-tivo-mpeg')
196 handler
.send_header('Transfer-Encoding', 'chunked')
197 handler
.end_headers()
198 handler
.wfile
.write("\x30\x0D\x0A")
201 tsn
= handler
.headers
.getheader('tsn', '')
203 o
= urlparse("http://fake.host" + handler
.path
)
205 handler
.send_response(200)
206 handler
.end_headers()
207 transcode
.output_video(container
['path'] + path
[len(name
) + 1:],
210 def __isdir(self
, full_path
):
211 return os
.path
.isdir(full_path
)
213 def __duration(self
, full_path
):
214 return transcode
.video_info(full_path
)['millisecs']
216 def __total_items(self
, full_path
):
219 for file in os
.listdir(full_path
):
220 if file.startswith('.'):
222 file = os
.path
.join(full_path
, file)
223 if os
.path
.isdir(file):
226 if os
.path
.splitext(file)[1].lower() in extensions
:
228 elif file in transcode
.info_cache
:
229 if transcode
.supported_format(file):
235 def __est_size(self
, full_path
, tsn
= ''):
236 # Size is estimated by taking audio and video bit rate adding 2%
238 if transcode
.tivo_compatable(full_path
, tsn
):
239 # Is TiVo-compatible mpeg2
240 return int(os
.stat(full_path
).st_size
)
243 if config
.getAudioCodec(tsn
) == None:
244 audioBPS
= config
.getMaxAudioBR(tsn
)*1000
246 audioBPS
= config
.strtod(config
.getAudioBR(tsn
))
247 videoBPS
= config
.strtod(transcode
.select_videostr(full_path
, tsn
))
248 bitrate
= audioBPS
+ videoBPS
249 return int((self
.__duration
(full_path
) / 1000) *
250 (bitrate
* 1.02 / 8))
252 def __getMetadataFromTxt(self
, full_path
):
255 default_meta
= os
.path
.join(os
.path
.split(full_path
)[0], 'default.txt')
256 standard_meta
= full_path
+ '.txt'
257 subdir_meta
= os
.path
.join(os
.path
.dirname(full_path
), '.meta',
258 os
.path
.basename(full_path
)) + '.txt'
260 for metafile
in (default_meta
, standard_meta
, subdir_meta
):
261 metadata
.update(self
.__getMetadataFromFile
(metafile
))
265 def __getMetadataFromFile(self
, file):
268 if os
.path
.exists(file):
269 for line
in open(file):
270 if line
.strip().startswith('#'):
275 key
, value
= line
.split(':', 1)
277 value
= value
.strip()
279 if key
.startswith('v'):
281 metadata
[key
].append(value
)
283 metadata
[key
] = [value
]
285 metadata
[key
] = value
289 def metadata_basic(self
, full_path
):
292 base_path
, title
= os
.path
.split(full_path
)
293 ctime
= os
.stat(full_path
).st_ctime
294 if (ctime
< 0): ctime
= 0
295 originalAirDate
= datetime
.fromtimestamp(ctime
)
297 metadata
['title'] = '.'.join(title
.split('.')[:-1])
298 metadata
['seriesTitle'] = metadata
['title'] # default to the filename
299 metadata
['originalAirDate'] = originalAirDate
.isoformat()
301 metadata
.update(self
.__getMetadataFromTxt
(full_path
))
305 def metadata_full(self
, full_path
, tsn
=''):
308 now
= datetime
.utcnow()
310 duration
= self
.__duration
(full_path
)
311 duration_delta
= timedelta(milliseconds
= duration
)
313 metadata
['time'] = now
.isoformat()
314 metadata
['startTime'] = now
.isoformat()
315 metadata
['stopTime'] = (now
+ duration_delta
).isoformat()
316 metadata
['size'] = self
.__est
_size
(full_path
, tsn
)
317 metadata
['duration'] = duration
318 metadata
.update(self
.metadata_basic(full_path
))
320 min = duration_delta
.seconds
/ 60
321 sec
= duration_delta
.seconds
% 60
324 metadata
['iso_duration'] = 'P' + str(duration_delta
.days
) + \
325 'DT' + str(hours
) + 'H' + str(min) + \
329 def QueryContainer(self
, handler
, query
):
330 tsn
= handler
.headers
.getheader('tsn', '')
331 subcname
= query
['Container'][0]
333 # If you are running 8.3 software you want to enable hack83
335 if config
.getHack83():
336 logger
= logging
.getLogger('pyTivo.hack83')
337 logger
.debug('=' * 73)
338 query
, hackPath
= self
.hack(handler
, query
, subcname
)
339 hackPath
= '/'.join(hackPath
)
340 logger
.debug('Tivo said: %s || Hack said: %s' % (subcname
, hackPath
))
344 logger
.debug('sending 302 redirect page')
345 handler
.send_response(302)
346 handler
.send_header('Location ', 'http://' +
347 handler
.headers
.getheader('host') +
348 '/TiVoConnect?Command=QueryContainer&' +
349 'AnchorItem=Hack8.3&Container=' + quote(hackPath
))
350 handler
.end_headers()
355 cname
= subcname
.split('/')[0]
357 if not handler
.server
.containers
.has_key(cname
) or \
358 not self
.get_local_path(handler
, query
):
359 handler
.send_response(404)
360 handler
.end_headers()
363 container
= handler
.server
.containers
[cname
]
364 precache
= container
.get('precache', 'False').lower() == 'true'
366 files
, total
, start
= self
.get_files(handler
, query
,
367 self
.video_file_filter
)
370 local_base_path
= self
.get_local_base_path(handler
, query
)
372 mtime
= os
.stat(file).st_mtime
373 if (mtime
< 0): mtime
= 0
374 mtime
= datetime
.fromtimestamp(mtime
)
375 video
= VideoDetails()
376 video
['captureDate'] = hex(int(time
.mktime(mtime
.timetuple())))
377 video
['name'] = os
.path
.split(file)[1]
379 video
['part_path'] = file.replace(local_base_path
, '', 1)
380 if not video
['part_path'].startswith(os
.path
.sep
):
381 video
['part_path'] = os
.path
.sep
+ video
['part_path']
382 video
['title'] = os
.path
.split(file)[1]
383 video
['is_dir'] = self
.__isdir
(file)
385 video
['small_path'] = subcname
+ '/' + video
['name']
386 video
['total_items'] = self
.__total
_items
(file)
388 if precache
or len(files
) == 1 or file in transcode
.info_cache
:
389 video
['valid'] = transcode
.supported_format(file)
391 video
.update(self
.metadata_full(file, tsn
))
393 video
['valid'] = True
394 video
.update(self
.metadata_basic(file))
398 handler
.send_response(200)
399 handler
.end_headers()
400 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
409 t
.guid
= config
.getGUID()
410 t
.tivos
= handler
.tivos
411 handler
.wfile
.write(t
)
413 def TVBusQuery(self
, handler
, query
):
414 tsn
= handler
.headers
.getheader('tsn', '')
415 file = query
['File'][0]
416 path
= self
.get_local_path(handler
, query
)
417 file_path
= path
+ file
419 file_info
= VideoDetails()
420 file_info
['valid'] = transcode
.supported_format(file_path
)
421 if file_info
['valid']:
422 file_info
.update(self
.metadata_full(file_path
, tsn
))
424 handler
.send_response(200)
425 handler
.end_headers()
426 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'TvBus.tmpl'))
429 handler
.wfile
.write(t
)
431 def XSL(self
, handler
, query
):
432 file = open(os
.path
.join(SCRIPTDIR
, 'templates', 'container.xsl'))
433 handler
.send_response(200)
434 handler
.end_headers()
435 handler
.wfile
.write(file.read())
438 def Push(self
, handler
, query
):
439 file = unquote(query
['File'][0])
440 tsn
= query
['tsn'][0]
441 path
= self
.get_local_path(handler
, query
)
442 file_path
= path
+ file
444 file_info
= VideoDetails()
445 file_info
['valid'] = transcode
.supported_format(file_path
)
446 if file_info
['valid']:
447 file_info
.update(self
.metadata_full(file_path
, tsn
))
450 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
451 s
.connect(('tivo.com',123))
452 ip
= s
.getsockname()[0]
453 container
= quote(query
['Container'][0].split('/')[0])
454 port
= config
.getPort()
456 url
= 'http://%s:%s/%s%s' % (ip
, port
, container
, quote(file))
463 description
= file_info
['description'],
464 duration
= file_info
['duration'] / 1000,
465 size
= file_info
['size'],
466 title
= file_info
['title'],
467 subtitle
= file_info
['name'])
470 handler
.send_response(500)
471 handler
.end_headers()
472 handler
.wfile
.write('%s\n\n%s' % (e
, traceback
.format_exc() ))
475 referer
= handler
.headers
.getheader('Referer')
476 handler
.send_response(302)
477 handler
.send_header('Location', referer
)
478 handler
.end_headers()
481 class VideoDetails(DictMixin
):
483 def __init__(self
, d
=None):
489 def __getitem__(self
, key
):
490 if key
not in self
.d
:
491 self
.d
[key
] = self
.default(key
)
494 def __contains__(self
, key
):
497 def __setitem__(self
, key
, value
):
500 def __delitem__(self
):
507 return self
.d
.__iter
__()
510 return self
.d
.iteritems()
512 def default(self
, key
):
515 'episodeNumber' : '0',
516 'displayMajorNumber' : '0',
517 'displayMinorNumber' : '0',
518 'isEpisode' : 'true',
519 'colorCode' : ('COLOR', '4'),
520 'showType' : ('SERIES', '5'),
521 'tvRating' : ('NR', '7')
525 elif key
.startswith('v'):