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
10 SCRIPTDIR
= os
.path
.dirname(__file__
)
16 content_type
= 'x-container/tivo-videos'
19 # Used for 8.3's broken requests
22 def SendFile(self
, handler
, container
, name
):
24 #No longer a 'cheep' hack :p
25 if handler
.headers
.getheader('Range') and not handler
.headers
.getheader('Range') == 'bytes=0-':
26 handler
.send_response(206)
27 handler
.send_header('Connection', 'close')
28 handler
.send_header('Content-Type', 'video/x-tivo-mpeg')
29 handler
.send_header('Transfer-Encoding', 'chunked')
30 handler
.send_header('Server', 'TiVo Server/1.4.257.475')
32 handler
.wfile
.write("\x30\x0D\x0A")
35 tsn
= handler
.headers
.getheader('tsn', '')
37 o
= urlparse("http://fake.host" + handler
.path
)
38 path
= unquote_plus(o
[2])
39 handler
.send_response(200)
41 transcode
.output_video(container
['path'] + path
[len(name
)+1:], handler
.wfile
, tsn
)
43 def hack(self
, handler
, query
, subcname
):
45 tsn
= handler
.headers
.getheader('tsn', '')
52 path
, state
= self
.request_history
[tsn
]
56 self
.request_history
[tsn
] = (path
, state
)
57 state
['query'] = query
58 state
['redirected'] = False
61 current_folder
= subcname
.split('/')[-1]
64 if len(subcname
.split('/')) == 1:
65 path
[:] = [current_folder
]
66 state
['query'] = query
67 state
['redirected'] = 0
69 #entering a new folder
70 elif 'AnchorItem' not in query
:
71 path
.append(current_folder
)
72 state
['query'] = query
75 elif len(path
) > 1 and current_folder
== path
[-2]:
81 if not state
['redirected']:
84 state
['redirected'] = True
87 state
['redirected'] = False
89 print 'Hack says', path
92 def QueryContainer(self
, handler
, query
):
94 subcname
= query
['Container'][0]
96 print '========================================================================='
97 print 'Tivo said' + subcname
99 query
= self
.hack(handler
, query
, subcname
)
102 handler
.send_response(302)
103 handler
.send_header('Location ', 'http://' + handler
.headers
.getheader('host') + handler
.path
)
104 handler
.end_headers()
111 # print k, ':',query[k]
113 cname
= subcname
.split('/')[0]
115 if not handler
.server
.containers
.has_key(cname
) or not self
.get_local_path(handler
, query
):
116 handler
.send_response(404)
117 handler
.end_headers()
120 path
= self
.get_local_path(handler
, query
)
122 return os
.path
.isdir(os
.path
.join(path
, file))
125 full_path
= os
.path
.join(path
, file)
126 return transcode
.video_info(full_path
)[4]
129 full_path
= os
.path
.join(path
, file)
130 #Size is estimated by taking audio and video bit rate adding 2%
132 if transcode
.tivo_compatable(full_path
): # Is TiVo compatible mpeg2
133 return int(os
.stat(full_path
).st_size
)
134 else: # Must be re-encoded
135 audioBPS
= strtod(Config
.getAudioBR())
136 videoBPS
= strtod(Config
.getVideoBR())
137 bitrate
= audioBPS
+ videoBPS
138 return int((duration(file)/1000)*(bitrate
* 1.02 / 8))
140 def VideoFileFilter(file):
141 full_path
= os
.path
.join(path
, file)
143 if os
.path
.isdir(full_path
):
145 return transcode
.suported_format(full_path
)
147 handler
.send_response(200)
148 handler
.end_headers()
149 t
= Template(file=os
.path
.join(SCRIPTDIR
,'templates', 'container.tmpl'))
151 t
.files
, t
.total
, t
.start
= self
.get_files(handler
, query
, VideoFileFilter
)
152 t
.duration
= duration
153 t
.est_size
= est_size
157 handler
.wfile
.write(t
)
160 # Parse a bitrate using the SI/IEEE suffix values as if by ffmpeg
161 # For example, 2K==2000, 2Ki==2048, 2MB==16000000, 2MiB==16777216
162 # Algorithm: http://svn.mplayerhq.hu/ffmpeg/trunk/libavcodec/eval.c
164 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}
165 p
= re
.compile(r
'^(\d+)(?:([yzafpnumcdhkKMGTPEZY])(i)?)?([Bb])?$')
168 raise SyntaxError('Invalid bit value syntax')
169 (coef
, prefix
, power
, byte
) = m
.groups()
173 exponent
= float(prefixes
[prefix
])
176 value
= float(coef
) * pow(2.0, exponent
/0.3)
179 value
= float(coef
) * pow(10.0, exponent
)
180 if byte
== "B": # B==Byte, b=bit