-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstreamer.py
221 lines (175 loc) · 7.39 KB
/
streamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'''
Copyright (C) 2014-2016 ddurdle
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
import re
import urllib, urllib2
import xbmc, xbmcaddon, xbmcgui, xbmcplugin
class MyHTTPServer(HTTPServer):
def __init__(self, *args, **kw):
HTTPServer.__init__(self, *args, **kw)
self.ready = True
def setFile(self, playbackURL, chunksize, playbackFile, response, fileSize, url, service):
self.playbackURL = playbackURL
self.chunksize = chunksize
self.playbackFile = playbackFile
self.response = response
self.fileSize = fileSize
self.url = url
self.service = service
self.ready = True
self.state = 0
self.lock = 0
def setAccount(self, service, domain):
self.service = service
self.domain = domain
self.ready = True
class myStreamer(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
# debug - print headers in log
headers = str(self.headers)
print(headers)
# passed a kill signal?
if self.path == '/kill':
self.server.ready = False
return
# redirect url to output
else:
url = str(self.server.domain) + str(self.path)
print 'GET ' + url + "\n"
req = urllib2.Request(url, None, self.server.service.getHeadersList())
try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
if e.code == 403 or e.code == 401:
print "ERROR\n"
self.server.service.refreshToken()
req = urllib2.Request(url, None, self.server.service.getHeadersList())
try:
response = urllib2.urlopen(req)
except:
return
else:
return
self.send_response(200)
#print str(response.info()) + "\n"
self.send_header('Content-Type',response.info().getheader('Content-Type'))
self.send_header('Content-Length',response.info().getheader('Content-Length'))
self.send_header('Cache-Control',response.info().getheader('Cache-Control'))
self.send_header('Date',response.info().getheader('Date'))
#self.send_header('ETag',response.info().getheader('ETag'))
#self.send_header('Server',response.info().getheader('Server'))
self.end_headers()
## may want to add more granular control over chunk fetches
self.wfile.write(response.read())
#response_data = response.read()
response.close()
print "DONE"
#TO DELETE
def do_GET2(self):
# passed a kill signal?
if self.path == '/kill':
self.server.ready = False
return
# debug - print headers in log
headers = str(self.headers)
print(headers)
# client passed a range of bytes to fetch
start = ''
end = ''
count = 0
for r in re.finditer('Range\:\s+bytes\=(\d+)\-' ,
headers, re.DOTALL):
start = int(r.group(1))
break
for r in re.finditer('Range\:\s+bytes\=\d+\-(\d+)' ,
headers, re.DOTALL):
end = int(r.group(1))
if end == 0:
end = ''
break
# pass back the appropriate headers
if start == '':
self.send_response(200)
self.send_header('Content-Length',self.server.fileSize)
else:
self.send_response(206)
if start > 0:
count = int(start/int(self.server.chunksize))
self.send_header('Content-Length',str(self.server.fileSize-(count*int(self.server.chunksize))))
self.send_header('Content-Range','bytes ' + str(start) + '-' + str(self.server.fileSize-1)+'/'+str(self.server.fileSize))
req = urllib2.Request(self.server.url, None, self.server.service.getHeadersList(additionalHeader='Range', additionalValue='bytes '+ str(start) + '-' + str(end)))
try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
print "error " + str(e.code) + ' header Range' + str(start) + '-' + str(end)
self.server.service.refreshToken()
req = urllib2.Request(self.server.url, None, self.server.service.getHeadersList(additionalHeader='Range', additionalValue='bytes '+ str(start) + '-' + str(end)))
try:
response = urllib2.urlopen(req)
except urllib2.URLError, e:
print "error " + str(e.code)
return
self.send_header('Content-type','video/mp4')
self.send_header('Accept-Ranges','bytes')
self.end_headers()
#while self.server.state == 2:
# self.server.state = 3
#while self.server.state == 3:
# xbmc.sleep(10)
# is streamer ready to serve packets?
if self.server.state == 0:
## fetch the entire stream?
#self.server.state = 2
#try:
if count == 0:
with open(self.server.playbackURL, "rb") as f:
while True:
chunk = f.read(self.server.chunksize)
if chunk:
self.wfile.write(chunk)
count = count + 1
else:
break
f.close()
#fi = open(self.server.playbackFile, 'ab')
#self.server.state = 1
if self.server.lock != 0:
self.server.lock = 2
xbmc.sleep(1000)
self.server.lock = 1
while self.server.lock ==1:#self.server.state == 2:
chunk = self.server.response.read(self.server.chunksize)
if not chunk: break
fi = open(self.server.playbackFile, 'wb')
fi.seek(self.server.chunksize*count,0)
fi.write(chunk)
fi.close()
with open(self.server.playbackURL, "rb") as f:
f.seek(self.server.chunksize*count,0)
chunk = f.read(self.server.chunksize)
self.wfile.write(chunk)
f.close()
count = count + 1
self.server.lock = 0
#fi.close()
#except: pass
#if self.server.state == 2:
# self.server.ready = False
#self.server.state = 0
self.server.ready = False
# else:
# self.server.state = 1
# self.server.ready = False
return