1 import transcode
, os
, socket
, re
2 from Cheetah
.Template
import Template
3 from plugin
import Plugin
4 from urllib
import unquote_plus
, quote
, unquote
5 from urlparse
import urlparse
6 from xml
.sax
.saxutils
import escape
7 from lrucache
import LRUCache
8 from UserDict
import DictMixin
9 from datetime
import datetime
, timedelta
12 SCRIPTDIR
= os
.path
.dirname(__file__
)
18 CONTENT_TYPE
= 'x-container/tivo-videos'
20 def send_file(self
, handler
, container
, name
):
22 #No longer a 'cheep' hack :p
23 if handler
.headers
.getheader('Range') and not handler
.headers
.getheader('Range') == 'bytes=0-':
24 handler
.send_response(206)
25 handler
.send_header('Connection', 'close')
26 handler
.send_header('Content-Type', 'video/x-tivo-mpeg')
27 handler
.send_header('Transfer-Encoding', 'chunked')
28 handler
.send_header('Server', 'TiVo Server/1.4.257.475')
30 handler
.wfile
.write("\x30\x0D\x0A")
33 tsn
= handler
.headers
.getheader('tsn', '')
35 o
= urlparse("http://fake.host" + handler
.path
)
36 path
= unquote_plus(o
[2])
37 handler
.send_response(200)
39 transcode
.output_video(container
['path'] + path
[len(name
)+1:], handler
.wfile
, tsn
)
42 def __isdir(self
, full_path
):
43 return os
.path
.isdir(full_path
)
45 def __duration(self
, full_path
):
46 return transcode
.video_info(full_path
)[4]
48 def __est_size(self
, full_path
, tsn
= ''):
49 #Size is estimated by taking audio and video bit rate adding 2%
51 if transcode
.tivo_compatable(full_path
): # Is TiVo compatible mpeg2
52 return int(os
.stat(full_path
).st_size
)
53 else: # Must be re-encoded
54 audioBPS
= strtod(config
.getAudioBR())
55 videoBPS
= strtod(config
.getVideoBR())
56 bitrate
= audioBPS
+ videoBPS
57 return int((self
.__duration
(full_path
)/1000)*(bitrate
* 1.02 / 8))
59 def __getMetadataFromTxt(self
, full_path
):
62 default_file
= os
.path
.join(os
.path
.split(full_path
)[0], 'default.txt')
63 description_file
= full_path
+ '.txt'
65 metadata
.update(self
.__getMetadataFromFile
(default_file
))
66 metadata
.update(self
.__getMetadataFromFile
(description_file
))
70 def __getMetadataFromFile(self
, file):
73 if os
.path
.exists(file):
74 for line
in open(file):
75 if line
.strip().startswith('#'):
80 key
, value
= line
.split(':', 1)
84 if key
.startswith('v'):
86 metadata
[key
].append(value
)
88 metadata
[key
] = [value
]
94 def __metadata(self
, full_path
):
98 base_path
, title
= os
.path
.split(full_path
)
100 originalAirDate
= datetime
.fromtimestamp(os
.stat(full_path
).st_ctime
)
101 duration
= self
.__duration
(full_path
)
102 duration_delta
= timedelta(milliseconds
= duration
)
104 metadata
['title'] = '.'.join(title
.split('.')[:-1])
105 metadata
['seriesTitle'] = os
.path
.split(base_path
)[1]
106 metadata
['originalAirDate'] = originalAirDate
.isoformat()
107 metadata
['time'] = now
.isoformat()
108 metadata
['startTime'] = now
.isoformat()
109 metadata
['stopTime'] = (now
+ duration_delta
).isoformat()
111 metadata
.update( self
.__getMetadataFromTxt
(full_path
) )
113 metadata
['size'] = self
.__est
_size
(full_path
)
114 metadata
['duration'] = duration
116 min = duration_delta
.seconds
/ 60
117 sec
= duration_delta
.seconds
% 60
120 metadata
['iso_durarion'] = 'P' + str(duration_delta
.days
) + 'DT' + str(hours
) + 'H' + str(min) + 'M' + str(sec
) + 'S'
124 def QueryContainer(self
, handler
, query
):
126 tsn
= handler
.headers
.getheader('tsn', '')
127 subcname
= query
['Container'][0]
128 cname
= subcname
.split('/')[0]
130 if not handler
.server
.containers
.has_key(cname
) or not self
.get_local_path(handler
, query
):
131 handler
.send_response(404)
132 handler
.end_headers()
135 def video_file_filter(file, type = None):
137 if os
.path
.isdir(full_path
):
139 return transcode
.suported_format(full_path
)
141 files
, total
, start
= self
.get_files(handler
, query
, video_file_filter
)
145 video
= VideoDetails()
146 video
['name'] = os
.path
.split(file)[1]
148 video
['title'] = os
.path
.split(file)[1]
149 video
['is_dir'] = self
.__isdir
(file)
150 if not video
['is_dir']:
151 video
.update(self
.__metadata
(file))
155 handler
.send_response(200)
156 handler
.end_headers()
157 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
164 handler
.wfile
.write(t
)
166 def TVBusQuery(self
, handler
, query
):
168 file = query
['File'][0]
169 path
= self
.get_local_path(handler
, query
)
170 file_path
= os
.path
.join(path
, file)
172 file_info
= VideoDetails()
173 file_info
.update(self
.__metadata
(file_path
))
175 handler
.send_response(200)
176 handler
.end_headers()
177 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'TvBus.tmpl'))
180 handler
.wfile
.write(t
)
182 class VideoDetails(DictMixin
):
184 def __init__(self
, d
= None):
190 def __getitem__(self
, key
):
191 if key
not in self
.d
:
192 self
.d
[key
] = self
.default(key
)
195 def __contains__(self
, key
):
198 def __setitem__(self
, key
, value
):
201 def __delitem__(self
):
208 return self
.d
.__iter
__()
211 return self
.d
.iteritems()
213 def default(self
, key
):
216 'episodeNumber' : '0',
217 'displayMajorNumber' : '0',
218 'displayMinorNumber' : '0',
219 'isEpisode' : 'true',
220 'colorCode' : ('COLOR', '4'),
221 'showType' : ('SERIES', '5'),
222 'tvRating' : ('NR', '7'),
226 elif key
.startswith('v'):
232 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
233 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
234 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
236 prefixes
= {"y":-24,"z":-21,"a":-18,"f":-15,"p":-12,"n":-9,"u":-6,"m":-3,"c":-2,"d":-1,"h":2,"k":3,"K":3,"M":6,"G":9,"T":12,"P":15,"E":18,"Z":21,"Y":24}
237 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
240 raise SyntaxError('Invalid bit value syntax')
241 (coef
, prefix
, power
, byte
) = m
.groups()
245 exponent
= float(prefixes
[prefix
])
248 value
= float(coef
) * pow(2.0, exponent
/0.3)
251 value
= float(coef
) * pow(10.0, exponent
)
252 if byte
== "B": # B==Byte, b=bit