forked from pyload/pyload
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownload.py
More file actions
208 lines (152 loc) · 6.46 KB
/
Copy pathDownload.py
File metadata and controls
208 lines (152 loc) · 6.46 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
# -*- coding: utf-8 -*-
# @author: RaNaN
import Queue
import os
import sys
import time
import traceback
import pycurl
from pyload.Thread.Plugin import PluginThread
from pyload.plugin.Plugin import Abort, Fail, Reconnect, Retry, SkipDownload
class DownloadThread(PluginThread):
"""thread for downloading files from 'real' hoster plugins"""
def __init__(self, manager):
"""Constructor"""
PluginThread.__init__(self, manager)
self.queue = Queue.Queue() #: job queue
self.active = False
self.start()
#--------------------------------------------------------------------------
def run(self):
"""run method"""
pyfile = None
while True:
del pyfile
self.active = False #: sets the thread inactive when it is ready to get next job
self.active = self.queue.get()
pyfile = self.active
if self.active == "quit":
self.active = False
self.m.threads.remove(self)
return True
try:
if not pyfile.hasPlugin():
continue
# this pyfile was deleted while queueing
pyfile.plugin.checkForSameFiles(starting=True)
self.m.core.log.info(_("Download starts: %s" % pyfile.name))
# start download
self.m.core.addonManager.downloadPreparing(pyfile)
pyfile.error = ""
pyfile.plugin.preprocessing(self)
self.m.core.log.info(_("Download finished: %s") % pyfile.name)
self.m.core.addonManager.downloadFinished(pyfile)
self.m.core.files.checkPackageFinished(pyfile)
except NotImplementedError:
self.m.core.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.m.core.log.info(_("Download aborted: %s") % pyfile.name)
except Exception:
pass
pyfile.setStatus("aborted")
if self.m.core.debug:
traceback.print_exc()
self.clean(pyfile)
continue
except Reconnect:
self.queue.put(pyfile)
# pyfile.req.clearCookies()
while self.m.reconnecting.isSet():
time.sleep(0.5)
continue
except Retry, e:
reason = e.args[0]
self.m.core.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]
if msg == "offline":
pyfile.setStatus("offline")
self.m.core.log.warning(_("Download is offline: %s") % pyfile.name)
elif msg == "temp. offline":
pyfile.setStatus("temp. offline")
self.m.core.log.warning(_("Download is temporary offline: %s") % pyfile.name)
else:
pyfile.setStatus("failed")
self.m.core.log.warning(_("Download failed: %(name)s | %(msg)s") % {"name": pyfile.name, "msg": msg})
pyfile.error = msg
if self.m.core.debug:
traceback.print_exc()
self.m.core.addonManager.downloadFailed(pyfile)
self.clean(pyfile)
continue
except pycurl.error, e:
if len(e.args) == 2:
code, msg = e.args
else:
code = 0
msg = e.args
self.m.core.log.debug("pycurl exception %s: %s" % (code, msg))
if code in (7, 18, 28, 52, 56):
self.m.core.log.warning(_("Couldn't connect to host or connection reset, waiting 1 minute and retry."))
wait = time.time() + 60
pyfile.waitUntil = wait
pyfile.setStatus("waiting")
while time.time() < wait:
time.sleep(1)
if pyfile.abort:
break
if pyfile.abort:
self.m.core.log.info(_("Download aborted: %s") % pyfile.name)
pyfile.setStatus("aborted")
self.clean(pyfile)
else:
self.queue.put(pyfile)
continue
else:
pyfile.setStatus("failed")
self.m.core.log.error("pycurl error %s: %s" % (code, msg))
if self.m.core.debug:
traceback.print_exc()
self.writeDebugReport(pyfile)
self.m.core.addonManager.downloadFailed(pyfile)
self.clean(pyfile)
continue
except SkipDownload, e:
pyfile.setStatus("skipped")
self.m.core.log.info(_("Download skipped: %(name)s due to %(plugin)s") % {"name": pyfile.name, "plugin": e.message})
self.clean(pyfile)
self.m.core.files.checkPackageFinished(pyfile)
self.active = False
self.m.core.files.save()
continue
except Exception, e:
pyfile.setStatus("failed")
self.m.core.log.warning(_("Download failed: %(name)s | %(msg)s") % {"name": pyfile.name, "msg": str(e)})
pyfile.error = str(e)
if self.m.core.debug:
traceback.print_exc()
self.writeDebugReport(pyfile)
self.m.core.addonManager.downloadFailed(pyfile)
self.clean(pyfile)
continue
finally:
self.m.core.files.save()
pyfile.checkIfProcessed()
sys.exc_clear()
# pyfile.plugin.req.clean()
self.active = False
pyfile.finishIfDone()
self.m.core.files.save()
def put(self, job):
"""assing job to thread"""
self.queue.put(job)
def stop(self):
"""stops the thread"""
self.put("quit")