forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadThread.py
More file actions
230 lines (172 loc) · 7.35 KB
/
Copy pathDownloadThread.py
File metadata and controls
230 lines (172 loc) · 7.35 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
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
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
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/>.
@author: RaNaN
"""
from Queue import Queue
from time import sleep, time
from traceback import print_exc
from sys import exc_clear
from pycurl import error
from pyload.plugins.Base import Fail, Retry, Abort
from pyload.plugins.Hoster import Reconnect, SkipDownload
from pyload.plugins.Request import ResponseException
from BaseThread import BaseThread
class DownloadThread(BaseThread):
"""thread for downloading files from 'real' hoster plugins"""
def __init__(self, manager):
"""Constructor"""
BaseThread.__init__(self, manager)
self.queue = Queue() # job queue
self.active = None
self.start()
def run(self):
"""run method"""
pyfile = None
while True:
del pyfile
self.active = self.queue.get()
pyfile = self.active
if self.active == "quit":
self.active = None
self.m.threads.remove(self)
return True
try:
if not pyfile.hasPlugin(): continue
#this pyfile was deleted while queuing
pyfile.plugin.checkForSameFiles(starting=True)
self.log.info(_("Download starts: %s" % pyfile.name))
# start download
self.core.addonManager.downloadPreparing(pyfile)
pyfile.plugin.preprocessing(self)
self.log.info(_("Download finished: %s") % pyfile.name)
self.core.addonManager.downloadFinished(pyfile)
self.core.files.checkPackageFinished(pyfile)
except NotImplementedError:
self.log.error(_("Plugin %s is missing a function.") % pyfile.pluginname)
pyfile.setStatus("failed")
pyfile.error = "Plugin does not work"
self.clean(pyfile)
continue
except Abort:
try:
self.log.info(_("Download aborted: %s") % pyfile.name)
except:
pass
pyfile.setStatus("aborted")
self.clean(pyfile)
continue
except Reconnect:
self.queue.put(pyfile)
#pyfile.req.clearCookies()
while self.m.reconnecting.isSet():
sleep(0.5)
continue
except Retry, e:
reason = e.args[0]
self.log.info(_("Download restarted: %(name)s | %(msg)s") % {"name": pyfile.name, "msg": reason})
self.queue.put(pyfile)
continue
except Fail, e:
msg = e.args[0]
# TODO: activate former skipped downloads
if msg == "offline":
pyfile.setStatus("offline")
self.log.warning(_("Download is offline: %s") % pyfile.name)
elif msg == "temp. offline":
pyfile.setStatus("temp. offline")
self.log.warning(_("Download is temporary offline: %s") % pyfile.name)
else:
pyfile.setStatus("failed")
self.log.warning(_("Download failed: %(name)s | %(msg)s") % {"name": pyfile.name, "msg": msg})
pyfile.error = msg
self.core.addonManager.downloadFailed(pyfile)
self.clean(pyfile)
continue
except error, e:
if len(e.args) == 2:
code, msg = e.args
else:
code = 0
msg = e.args
self.log.debug("pycurl exception %s: %s" % (code, msg))
if code in (7, 18, 28, 52, 56):
self.log.warning(_("Couldn't connect to host or connection reset, waiting 1 minute and retry."))
wait = time() + 60
pyfile.waitUntil = wait
pyfile.setStatus("waiting")
while time() < wait:
sleep(1)
if pyfile.abort:
break
if pyfile.abort:
self.log.info(_("Download aborted: %s") % pyfile.name)
pyfile.setStatus("aborted")
self.clean(pyfile)
else:
self.queue.put(pyfile)
continue
else:
pyfile.setStatus("failed")
self.log.error("pycurl error %s: %s" % (code, msg))
if self.core.debug:
print_exc()
self.writeDebugReport(pyfile.plugin.__name__, pyfile)
self.core.addonManager.downloadFailed(pyfile)
self.clean(pyfile)
continue
except SkipDownload, e:
pyfile.setStatus("skipped")
self.log.info(_("Download skipped: %(name)s due to %(plugin)s")
% {"name": pyfile.name, "plugin": e.message})
self.clean(pyfile)
self.core.files.checkPackageFinished(pyfile)
self.active = False
self.core.files.save()
continue
except Exception, e:
if isinstance(e, ResponseException) and e.code == 500:
pyfile.setStatus("temp. offline")
self.log.warning(_("Download is temporary offline: %s") % pyfile.name)
pyfile.error = _("Internal Server Error")
else:
pyfile.setStatus("failed")
self.log.warning(_("Download failed: %(name)s | %(msg)s") % {"name": pyfile.name, "msg": str(e)})
pyfile.error = str(e)
if self.core.debug:
print_exc()
self.writeDebugReport(pyfile.plugin.__name__, pyfile)
self.core.addonManager.downloadFailed(pyfile)
self.clean(pyfile)
continue
finally:
self.core.files.save()
pyfile.checkIfProcessed()
exc_clear()
#pyfile.plugin.req.clean()
self.active = False
pyfile.finishIfDone()
self.core.files.save()
def getProgress(self):
if self.active:
return self.active.getProgressInfo()
def put(self, job):
"""assign a job to the thread"""
self.queue.put(job)
def clean(self, pyfile):
""" set thread inactive and release pyfile """
self.active = False
pyfile.release()
def stop(self):
"""stops the thread"""
self.put("quit")