Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[0dea54]: plugin.program.super.favourites / download.py Maximize Restore History

Download this file

download.py    183 lines (142 with data), 5.6 kB

  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
'''
Simple XBMC Download Script
Copyright (C) 2013 Sean Poyser (seanpoyser@gmail.com)
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/>.
'''
import urllib2
import xbmc
import xbmcgui
import xbmcplugin
import os
import inspect
def getResponse(url, size):
try:
req = urllib2.Request(url)
if size > 0:
size = int(size)
req.add_header('Range', 'bytes=%d-' % size)
resp = urllib2.urlopen(req, timeout=10)
return resp
except:
return None
def download(url, dest, title=None):
if not title:
title = 'XBMC Download'
script = inspect.getfile(inspect.currentframe())
cmd = 'RunScript(%s, %s, %s, %s)' % (script, url, dest, title)
xbmc.executebuiltin(cmd)
def doDownload(url, dest, title):
file = dest.rsplit(os.sep, 1)[-1]
#workaround bug that causes Frodo to lockup when stop is called
xbmc.executebuiltin('ActivateWindow(%d)' % 10025)
xbmc.Player().stop()
resp = getResponse(url, 0)
if not resp:
xbmcgui.Dialog().ok(title, dest, 'Download failed', 'No response from server')
return
try: content = int(resp.headers['Content-Length'])
except: content = 0
try: resumable = 'bytes' in resp.headers['Accept-Ranges'].lower()
except: resumable = False
#print "Download Header"
#print resp.headers
if resumable:
print "Download is resumable"
if content < 1:
xbmcgui.Dialog().ok(title, file, 'Unknown filesize', 'Unable to download')
return
size = 1024 * 1024
mb = content / (1024 * 1024)
if content < size:
size = content
total = 0
notify = 0
errors = 0
count = 0
resume = 0
sleep = 0
if xbmcgui.Dialog().yesno(title + ' - Confirm Download', file, 'Complete file is %dMB' % mb, 'Continue with download?', 'Confirm', 'Cancel') == 1:
return
f = open(dest, mode='wb')
chunk = None
chunks = []
while True:
downloaded = total
for c in chunks:
downloaded += len(c)
percent = min(100 * downloaded / content, 100)
if percent >= notify:
xbmc.executebuiltin( "XBMC.Notification(%s,%s,%i)" % ( title + ' - Download Progress - ' + str(percent)+'%', dest, 10000))
notify += 10
chunk = None
error = False
try:
chunk = resp.read(size)
if not chunk:
if percent < 99:
error = True
else:
while len(chunks) > 0:
c = chunks.pop(0)
f.write(c)
del c
f.close()
print '%s download complete' % (dest)
xbmcgui.Dialog().ok(title, dest, '' , 'Download finished')
return
except Exception, e:
print str(e)
error = True
sleep = 10
errno = 0
if hasattr(e, 'errno'):
errno = e.errno
if errno == 10035: # 'A non-blocking socket operation could not be completed immediately'
pass
if errno == 10054: #'An existing connection was forcibly closed by the remote host'
errors = 10 #force resume
sleep = 30
if errno == 11001: # 'getaddrinfo failed'
errors = 10 #force resume
sleep = 30
if chunk:
errors = 0
chunks.append(chunk)
if len(chunks) > 5:
c = chunks.pop(0)
f.write(c)
total += len(c)
del c
if error:
errors += 1
count += 1
print '%d Error(s) whilst downloading %s' % (count, dest)
xbmc.sleep(sleep*1000)
if (resumable and errors > 0) or errors >= 10:
if (not resumable and resume >= 10) or resume >= 100:
#Give up!
print '%s download canceled - too many error whilst downloading' % (dest)
xbmcgui.Dialog().ok(title, dest, '' , 'Download failed')
return
resume += 1
errors = 0
if resumable:
chunks = []
#create new response
print 'Download resumed (%d) %s' % (resume, dest)
resp = getResponse(url, total)
else:
#use existing response
pass
if __name__ == '__main__':
if 'download.py' in sys.argv[0]:
doDownload(sys.argv[1], sys.argv[2], sys.argv[3])