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
11 SCRIPTDIR
= os
.path
.dirname(__file__
)
15 extfile
= os
.path
.join(SCRIPTDIR
, 'video.ext')
17 extensions
= file(extfile
).read().split()
23 CONTENT_TYPE
= 'x-container/tivo-videos'
25 def pre_cache(self
, full_path
):
26 if Video
.video_file_filter(self
, full_path
):
27 transcode
.supported_format(full_path
)
29 def video_file_filter(self
, full_path
, type=None):
30 if os
.path
.isdir(full_path
):
33 return os
.path
.splitext(full_path
)[1].lower() in extensions
35 return transcode
.supported_format(full_path
)
37 def send_file(self
, handler
, container
, name
):
38 if handler
.headers
.getheader('Range') and \
39 handler
.headers
.getheader('Range') != 'bytes=0-':
40 handler
.send_response(206)
41 handler
.send_header('Connection', 'close')
42 handler
.send_header('Content-Type', 'video/x-tivo-mpeg')
43 handler
.send_header('Transfer-Encoding', 'chunked')
45 handler
.wfile
.write("\x30\x0D\x0A")
48 tsn
= handler
.headers
.getheader('tsn', '')
50 o
= urlparse("http://fake.host" + handler
.path
)
52 handler
.send_response(200)
54 transcode
.output_video(container
['path'] + path
[len(name
) + 1:],
57 def __isdir(self
, full_path
):
58 return os
.path
.isdir(full_path
)
60 def __duration(self
, full_path
):
61 return transcode
.video_info(full_path
)[4]
63 def __est_size(self
, full_path
, tsn
= ''):
64 # Size is estimated by taking audio and video bit rate adding 2%
66 if transcode
.tivo_compatable(full_path
, tsn
):
67 # Is TiVo-compatible mpeg2
68 return int(os
.stat(full_path
).st_size
)
71 audioBPS
= config
.strtod(config
.getAudioBR(tsn
))
72 videoBPS
= config
.strtod(config
.getVideoBR(tsn
))
73 bitrate
= audioBPS
+ videoBPS
74 return int((self
.__duration
(full_path
) / 1000) *
77 def __getMetadataFromTxt(self
, full_path
):
80 default_file
= os
.path
.join(os
.path
.split(full_path
)[0], 'default.txt')
81 description_file
= full_path
+ '.txt'
83 metadata
.update(self
.__getMetadataFromFile
(default_file
))
84 metadata
.update(self
.__getMetadataFromFile
(description_file
))
88 def __getMetadataFromFile(self
, file):
91 if os
.path
.exists(file):
92 for line
in open(file):
93 if line
.strip().startswith('#'):
98 key
, value
= line
.split(':', 1)
100 value
= value
.strip()
102 if key
.startswith('v'):
104 metadata
[key
].append(value
)
106 metadata
[key
] = [value
]
108 metadata
[key
] = value
112 def __metadata_basic(self
, full_path
):
115 base_path
, title
= os
.path
.split(full_path
)
116 originalAirDate
= datetime
.fromtimestamp(os
.stat(full_path
).st_ctime
)
118 metadata
['title'] = '.'.join(title
.split('.')[:-1])
119 metadata
['seriesTitle'] = metadata
['title'] # default to the filename
120 metadata
['originalAirDate'] = originalAirDate
.isoformat()
122 metadata
.update(self
.__getMetadataFromTxt
(full_path
))
126 def __metadata_full(self
, full_path
, tsn
=''):
128 metadata
.update(self
.__metadata
_basic
(full_path
))
130 now
= datetime
.utcnow()
132 duration
= self
.__duration
(full_path
)
133 duration_delta
= timedelta(milliseconds
= duration
)
135 metadata
['time'] = now
.isoformat()
136 metadata
['startTime'] = now
.isoformat()
137 metadata
['stopTime'] = (now
+ duration_delta
).isoformat()
139 metadata
.update( self
.__getMetadataFromTxt
(full_path
) )
141 metadata
['size'] = self
.__est
_size
(full_path
, tsn
)
142 metadata
['duration'] = duration
144 min = duration_delta
.seconds
/ 60
145 sec
= duration_delta
.seconds
% 60
148 metadata
['iso_duration'] = 'P' + str(duration_delta
.days
) + \
149 'DT' + str(hours
) + 'H' + str(min) + \
153 def QueryContainer(self
, handler
, query
):
154 tsn
= handler
.headers
.getheader('tsn', '')
155 subcname
= query
['Container'][0]
156 cname
= subcname
.split('/')[0]
158 if not handler
.server
.containers
.has_key(cname
) or \
159 not self
.get_local_path(handler
, query
):
160 handler
.send_response(404)
161 handler
.end_headers()
164 container
= handler
.server
.containers
[cname
]
165 precache
= container
.get('precache', 'False').lower() == 'true'
167 files
, total
, start
= self
.get_files(handler
, query
,
168 self
.video_file_filter
)
171 local_base_path
= self
.get_local_base_path(handler
, query
)
173 video
= VideoDetails()
174 video
['name'] = os
.path
.split(file)[1]
176 video
['part_path'] = file.replace(local_base_path
, '', 1)
177 video
['title'] = os
.path
.split(file)[1]
178 video
['is_dir'] = self
.__isdir
(file)
180 video
['small_path'] = subcname
+ '/' + video
['name']
182 if precache
or len(files
) == 1 or file in transcode
.info_cache
:
183 video
['valid'] = transcode
.supported_format(file)
185 video
.update(self
.__metadata
_full
(file, tsn
))
187 video
['valid'] = True
188 video
.update(self
.__metadata
_basic
(file))
192 handler
.send_response(200)
193 handler
.end_headers()
194 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
203 t
.guid
= config
.getGUID()
204 handler
.wfile
.write(t
)
206 def TVBusQuery(self
, handler
, query
):
207 tsn
= handler
.headers
.getheader('tsn', '')
208 file = query
['File'][0]
209 path
= self
.get_local_path(handler
, query
)
210 file_path
= path
+ file
212 file_info
= VideoDetails()
213 file_info
['valid'] = transcode
.supported_format(file_path
)
214 if file_info
['valid']:
215 file_info
.update(self
.__metadata
_full
(file_path
, tsn
))
217 handler
.send_response(200)
218 handler
.end_headers()
219 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'TvBus.tmpl'))
222 handler
.wfile
.write(t
)
224 class VideoDetails(DictMixin
):
226 def __init__(self
, d
=None):
232 def __getitem__(self
, key
):
233 if key
not in self
.d
:
234 self
.d
[key
] = self
.default(key
)
237 def __contains__(self
, key
):
240 def __setitem__(self
, key
, value
):
243 def __delitem__(self
):
250 return self
.d
.__iter
__()
253 return self
.d
.iteritems()
255 def default(self
, key
):
258 'episodeNumber' : '0',
259 'displayMajorNumber' : '0',
260 'displayMinorNumber' : '0',
261 'isEpisode' : 'true',
262 'colorCode' : ('COLOR', '4'),
263 'showType' : ('SERIES', '5'),
264 'tvRating' : ('NR', '7')
268 elif key
.startswith('v'):