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 from debug
import debug_write
, fn_attr
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 debug_write(__name__
, fn_attr(), ['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 debug_write(__name__
, fn_attr(), ['new request ------------------------'])
49 debug_write(__name__
, fn_attr(), ['TiVo request is: \n', query
])
53 tsn
= handler
.headers
.getheader('tsn', '')
57 debug_write(__name__
, fn_attr(), ['this was not a TiVo request.',
58 '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 debug_write(__name__
, fn_attr(), ['queryAnchor: ', queryAnchor
,
72 ' leftAnchor: ', leftAnchor
,
73 ' rightAnchor: ', rightAnchor
])
75 path
, state
= self
.request_history
[tsn
]
77 # Never seen this tsn, starting new history
78 debug_write(__name__
, fn_attr(), ['New TSN.'])
81 self
.request_history
[tsn
] = (path
, state
)
82 state
['query'] = query
84 state
['time'] = int(time
.time()) + 1000
86 debug_write(__name__
, fn_attr(), ['our saved request is: \n', state
['query']])
88 current_folder
= subcname
.split('/')[-1]
90 # Begin figuring out what the request TiVo sent us means
91 # There are 7 options that can occur
93 # 1. at the root - This request is always accurate
94 if len(subcname
.split('/')) == 1:
95 debug_write(__name__
, fn_attr(), ['we are at the root.',
96 'Saving query, Clearing state[page].'])
97 path
[:] = [current_folder
]
98 state
['query'] = query
102 # 2. entering a new folder
103 # If there is no AnchorItem in the request then we must be
104 # entering a new folder.
105 if 'AnchorItem' not in query
:
106 debug_write(__name__
, fn_attr(), ['we are entering a new folder.',
107 'Saving query, setting time, setting state[page].'])
108 path
[:] = subcname
.split('/')
109 state
['query'] = query
110 state
['time'] = int(time
.time())
111 files
, total
, start
= self
.get_files(handler
, query
,
112 self
.video_file_filter
)
114 state
['page'] = files
[0]
119 # 3. Request a page after pyTivo sent a 302 code
120 # we know this is the proper page
121 if ''.join(query
['AnchorItem']) == 'Hack8.3':
122 debug_write(__name__
, fn_attr(), ['requested page from 302 code.',
123 'Returning saved query.'])
124 return state
['query'], path
126 # 4. this is a request for a file
127 if 'ItemCount' in query
and int(''.join(query
['ItemCount'])) == 1:
128 debug_write(__name__
, fn_attr(), ['requested a file'])
129 # Everything in this request is right except the container
130 query
['Container'] = ['/'.join(path
)]
134 # All remaining requests could be a second erroneous request for
135 # each of the following we will pause to see if a correct
136 # request is coming right behind it.
138 # Sleep just in case the erroneous request came first this
139 # allows a proper request to be processed first
140 debug_write(__name__
, fn_attr(), ['maybe erroneous request, sleeping.'])
143 # 5. scrolling in a folder
144 # This could be a request to exit a folder or scroll up or down
146 # First we have to figure out if we are scrolling
147 if 'AnchorOffset' in query
:
148 debug_write(__name__
, fn_attr(), ['Anchor offset was in query.',
149 'leftAnchor needs to match ', '/'.join(path
)])
150 if leftAnchor
== str('/'.join(path
)):
151 debug_write(__name__
, fn_attr(), ['leftAnchor matched.'])
152 query
['Container'] = ['/'.join(path
)]
153 files
, total
, start
= self
.get_files(handler
, query
,
154 self
.video_file_filter
)
155 debug_write(__name__
, fn_attr(), ['saved page is= ', state
['page'],
156 ' top returned file is= ', files
[0]])
157 # If the first file returned equals the top of the page
158 # then we haven't scrolled pages
159 if files
[0] != str(state
['page']):
160 debug_write(__name__
, fn_attr(), ['this is scrolling within a folder.'])
161 state
['page'] = files
[0]
164 # The only remaining options are exiting a folder or this is a
165 # erroneous second request.
167 # 6. this an extraneous request
168 # this came within a second of a valid request; just use that
170 if (int(time
.time()) - state
['time']) <= 1:
171 debug_write(__name__
, fn_attr(), ['erroneous request, send a 302 error'])
174 # 7. this is a request to exit a folder
175 # this request came by itself; it must be to exit a folder
177 debug_write(__name__
, fn_attr(), ['over 1 second,',
178 'must be request to exit folder'])
180 state
['query'] = {'Command': query
['Command'],
181 'SortOrder': query
['SortOrder'],
182 'ItemCount': query
['ItemCount'],
183 'Filter': query
['Filter'],
184 'Container': ['/'.join(path
)]}
187 # just in case we missed something.
188 debug_write(__name__
, fn_attr(), ['ERROR, should not have made it here. ',
189 'Trying to recover.'])
190 return state
['query'], path
192 def send_file(self
, handler
, container
, name
):
193 if handler
.headers
.getheader('Range') and \
194 handler
.headers
.getheader('Range') != 'bytes=0-':
195 handler
.send_response(206)
196 handler
.send_header('Connection', 'close')
197 handler
.send_header('Content-Type', 'video/x-tivo-mpeg')
198 handler
.send_header('Transfer-Encoding', 'chunked')
199 handler
.end_headers()
200 handler
.wfile
.write("\x30\x0D\x0A")
203 tsn
= handler
.headers
.getheader('tsn', '')
205 o
= urlparse("http://fake.host" + handler
.path
)
207 handler
.send_response(200)
208 handler
.end_headers()
209 transcode
.output_video(container
['path'] + path
[len(name
) + 1:],
212 def __isdir(self
, full_path
):
213 return os
.path
.isdir(full_path
)
215 def __duration(self
, full_path
):
216 return transcode
.video_info(full_path
)[4]
218 def __total_items(self
, full_path
):
221 for file in os
.listdir(full_path
):
222 if file.startswith('.'):
224 file = os
.path
.join(full_path
, file)
225 if os
.path
.isdir(file):
228 if os
.path
.splitext(file)[1].lower() in extensions
:
230 elif file in transcode
.info_cache
:
231 if transcode
.supported_format(file):
237 def __est_size(self
, full_path
, tsn
= ''):
238 # Size is estimated by taking audio and video bit rate adding 2%
240 if transcode
.tivo_compatable(full_path
, tsn
):
241 # Is TiVo-compatible mpeg2
242 return int(os
.stat(full_path
).st_size
)
245 if config
.getAudioCodec(tsn
) == None:
246 audioBPS
= config
.getMaxAudioBR(tsn
)*1000
248 audioBPS
= config
.strtod(config
.getAudioBR(tsn
))
249 videoBPS
= config
.strtod(config
.getVideoBR(tsn
))
250 bitrate
= audioBPS
+ videoBPS
251 return int((self
.__duration
(full_path
) / 1000) *
252 (bitrate
* 1.02 / 8))
254 def __getMetadataFromTxt(self
, full_path
):
257 default_meta
= os
.path
.join(os
.path
.split(full_path
)[0], 'default.txt')
258 standard_meta
= full_path
+ '.txt'
259 subdir_meta
= os
.path
.join(os
.path
.dirname(full_path
), '.meta',
260 os
.path
.basename(full_path
)) + '.txt'
262 for metafile
in (default_meta
, standard_meta
, subdir_meta
):
263 metadata
.update(self
.__getMetadataFromFile
(metafile
))
267 def __getMetadataFromFile(self
, file):
270 if os
.path
.exists(file):
271 for line
in open(file):
272 if line
.strip().startswith('#'):
277 key
, value
= line
.split(':', 1)
279 value
= value
.strip()
281 if key
.startswith('v'):
283 metadata
[key
].append(value
)
285 metadata
[key
] = [value
]
287 metadata
[key
] = value
291 def __metadata_basic(self
, full_path
):
294 base_path
, title
= os
.path
.split(full_path
)
295 originalAirDate
= datetime
.fromtimestamp(os
.stat(full_path
).st_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
=''):
307 metadata
.update(self
.__metadata
_basic
(full_path
))
309 now
= datetime
.utcnow()
311 duration
= self
.__duration
(full_path
)
312 duration_delta
= timedelta(milliseconds
= duration
)
314 metadata
['time'] = now
.isoformat()
315 metadata
['startTime'] = now
.isoformat()
316 metadata
['stopTime'] = (now
+ duration_delta
).isoformat()
317 metadata
['size'] = self
.__est
_size
(full_path
, tsn
)
318 metadata
['duration'] = duration
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():
337 query
, hackPath
= self
.hack(handler
, query
, subcname
)
338 hackPath
= '/'.join(hackPath
)
339 print 'Tivo said:', subcname
, '|| Hack said:', hackPath
340 debug_write(__name__
, fn_attr(), ['Tivo said: ', subcname
, ' || Hack said: ',
345 debug_write(__name__
, fn_attr(), ['sending 302 redirect page'])
346 handler
.send_response(302)
347 handler
.send_header('Location ', 'http://' +
348 handler
.headers
.getheader('host') +
349 '/TiVoConnect?Command=QueryContainer&' +
350 'AnchorItem=Hack8.3&Container=' + hackPath
)
351 handler
.end_headers()
356 cname
= subcname
.split('/')[0]
358 if not handler
.server
.containers
.has_key(cname
) or \
359 not self
.get_local_path(handler
, query
):
360 handler
.send_response(404)
361 handler
.end_headers()
364 container
= handler
.server
.containers
[cname
]
365 precache
= container
.get('precache', 'False').lower() == 'true'
367 files
, total
, start
= self
.get_files(handler
, query
,
368 self
.video_file_filter
)
371 local_base_path
= self
.get_local_base_path(handler
, query
)
373 mtime
= datetime
.fromtimestamp(os
.stat(file).st_mtime
)
374 video
= VideoDetails()
375 video
['captureDate'] = hex(int(time
.mktime(mtime
.timetuple())))
376 video
['name'] = os
.path
.split(file)[1]
378 video
['part_path'] = file.replace(local_base_path
, '', 1)
379 video
['title'] = os
.path
.split(file)[1]
380 video
['is_dir'] = self
.__isdir
(file)
382 video
['small_path'] = subcname
+ '/' + video
['name']
383 video
['total_items'] = self
.__total
_items
(file)
385 if precache
or len(files
) == 1 or file in transcode
.info_cache
:
386 video
['valid'] = transcode
.supported_format(file)
388 video
.update(self
.__metadata
_full
(file, tsn
))
390 video
['valid'] = True
391 video
.update(self
.__metadata
_basic
(file))
395 handler
.send_response(200)
396 handler
.end_headers()
397 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
406 t
.guid
= config
.getGUID()
407 t
.tivos
= handler
.tivos
408 handler
.wfile
.write(t
)
410 def TVBusQuery(self
, handler
, query
):
411 tsn
= handler
.headers
.getheader('tsn', '')
412 file = query
['File'][0]
413 path
= self
.get_local_path(handler
, query
)
414 file_path
= path
+ file
416 file_info
= VideoDetails()
417 file_info
['valid'] = transcode
.supported_format(file_path
)
418 if file_info
['valid']:
419 file_info
.update(self
.__metadata
_full
(file_path
, tsn
))
421 handler
.send_response(200)
422 handler
.end_headers()
423 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'TvBus.tmpl'))
426 handler
.wfile
.write(t
)
428 def XSL(self
, handler
, query
):
429 file = open(os
.path
.join(SCRIPTDIR
, 'templates', 'container.xsl'))
430 handler
.send_response(200)
431 handler
.end_headers()
432 handler
.wfile
.write(file.read())
435 def Push(self
, handler
, query
):
436 file = unquote(query
['File'][0])
437 tsn
= query
['tsn'][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
))
447 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
448 s
.connect(('tivo.com',123))
449 ip
= s
.getsockname()[0]
450 container
= quote(query
['Container'][0].split('/')[0])
451 port
= config
.getPort()
453 url
= 'http://%s:%s/%s%s' % (ip
, port
, container
, quote(file))
459 username
= config
.getTivoUsername()
460 password
= config
.getTivoPassword()
462 if not username
or not password
:
463 raise Exception("tivo_username and tivo_password required")
466 m
= mind
.Mind(username
, password
, True)
470 description
= file_info
['description'],
471 duration
= file_info
['duration'] / 1000,
472 size
= file_info
['size'],
473 title
= file_info
['title'],
474 subtitle
= file_info
['name'])
477 handler
.send_response(500)
478 handler
.end_headers()
479 handler
.wfile
.write('%s\n\n%s' % (e
, traceback
.format_exc() ))
482 referer
= handler
.headers
.getheader('Referer')
483 handler
.send_response(302)
484 handler
.send_header('Location', referer
)
485 handler
.end_headers()
488 class VideoDetails(DictMixin
):
490 def __init__(self
, d
=None):
496 def __getitem__(self
, key
):
497 if key
not in self
.d
:
498 self
.d
[key
] = self
.default(key
)
501 def __contains__(self
, key
):
504 def __setitem__(self
, key
, value
):
507 def __delitem__(self
):
514 return self
.d
.__iter
__()
517 return self
.d
.iteritems()
519 def default(self
, key
):
522 'episodeNumber' : '0',
523 'displayMajorNumber' : '0',
524 'displayMinorNumber' : '0',
525 'isEpisode' : 'true',
526 'colorCode' : ('COLOR', '4'),
527 'showType' : ('SERIES', '5'),
528 'tvRating' : ('NR', '7')
532 elif key
.startswith('v'):