-
Notifications
You must be signed in to change notification settings - Fork 1
/
Control.py
426 lines (363 loc) · 16.1 KB
/
Control.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import os
import cPickle
import time
import threading
import pycurl
from ConfigUtils import *
from Const import *
from DownloadFileList import DownloadFileList
from DownloadFileControl import DownloadFileControl
from DownloadFile import DownloadFile
from SaveFileControl import SaveFileControl
from Log import Log
from gui.MainFrame import MainFrame
class Control(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
#Config.load()
self.saveFileControl = SaveFileControl()
self.saveFileControl.start()
self.log = Log()
#self.toContinue = True
self.autoDownload = True
self.isStopped = False
self.queueingCurlObjectList = []
self.curlLock = threading.Lock()
#Check and load download files list
if Config.checkExistence(Config.settings.queueingListFile, TYPE_FILE) > NO_EXIST:
try:
queueingFile = open(Config.settings.queueingListFile, 'rb')
self.downloadFileList = cPickle.load(queueingFile)
queueingFile.close()
except EOFError:
self.downloadFileList = DownloadFileList()
self.log.debug('EXCEPTION Control', EOFError)
#self.log.debug('Queueing file error')
self.downloadFileList.setLock()
self.downloadFileList.resetStatus(STAT_S)
else:
self.downloadFileList = DownloadFileList()
self.downloadFileList.setLock()
#self.mainFrame = MainFrame(None, wx.ID_ANY, "Linux Rapidshare Grabber", (0, 0), (800, 600), self, self.downloadFileList)
self.mainFrame = None
#show the list in GUI
self.downloadFileControlList = []
def setMainFrame(self, mainFrame):
self.mainFrame = mainFrame
for downloadFile in self.downloadFileList.getList():
#print downloadFile
self.mainFrame.addDownloadFileToList(downloadFile, PANEL_TOP)
for downloadFile in self.downloadFileList.getList(True):
#print downloadFile
self.mainFrame.addDownloadFileToList(downloadFile, PANEL_BOT)
#Add an url to the queueing list
def addURL(self, fileURL):
strippedFileURL = str(fileURL).strip()
if (self.isStopped) or (strippedFileURL == ''):
return
if Config.getLinkType(strippedFileURL) == RAPIDSHARE_LINK and strippedFileURL.endswith('.html'):
strippedFileURL = strippedFileURL[0: len(strippedFileURL) - len('.html')]
if not self.downloadFileList.getDownloadFileByFileURL(strippedFileURL) is None:
return MSG_ADD_FILEURL_EXISTED + strippedFileURL
downloadFile = DownloadFile(strippedFileURL)
downloadFile.setId(Config.getId())
self.downloadFileList.addQueueingFile(downloadFile)
self.mainFrame.addDownloadFileToList(downloadFile, PANEL_TOP)
return MSG_ADD_FILEURL_SUCCESS + strippedFileURL
def addURLById(self, id):
downloadFile = self.downloadFileList.getDownloadFileById(id, False)
self.addURL(downloadFile.getFileURL())
#Stop all downloads, save the queueing list then quit
def exit(self):
#self.toContinue = False
self.isStopped = True
for downloadFileControl in self.downloadFileControlList:
#print 'Stopping ', downloadFileControl.getDownloadFile().getId()
downloadFileControl.stop()
#print 'Stopping Done'
self.downloadFileList.unsetLock()
#self.log.debug('downloadFileListLock', self.downloadFileList.downloadFileListLock)
#self.log.debug('completedFileListLock', self.downloadFileList.completedFileListLock)
self.saveQueueingFiles()
#for downloadFile in self.downloadFileList.getList():
#print downloadFile
Config.save()
#this is dangerous!!!
def saveQueueingFiles(self):
queueingFile = open(Config.settings.queueingListFile, 'wb')
#print dir(self.downloadFileList)
cPickle.dump(self.downloadFileList, queueingFile, cPickle.HIGHEST_PROTOCOL)
queueingFile.close()
#add a curl to the curl list
def addCurlObject(self, curlObject):
#lockkkk?
#print 'control addCurlObject getting lock'
self.curlLock.acquire()
#print 'adding ', curlObject
self.queueingCurlObjectList.append(curlObject)
#print 'control addCurlE_OPERATION_TIMEOUTEDObject releasing lock'
self.curlLock.release()
#get a curl from the list
def getCurlObject(self):
#print 'control getCurlObject getting lock'
self.curlLock.acquire()
if len(self.queueingCurlObjectList) != 0:
curlObject = self.queueingCurlObjectList.pop(0)
#print 'control getCurlObject releasing lock'
self.curlLock.release()
return curlObject
else:
#print 'control getCurlObject releasing lock'
self.curlLock.release()
return None
#reset the download file
def resetDownload(self, id = None):
if not (id is None):
found = False
#self.downloadFileList.changeStatus(fileURL, STAT_S)
for downloadFileControl in self.downloadFileControlList:
if downloadFileControl.getDownloadFile().getId() == id:
downloadFileControl.resetSettings()
self.mainFrame.update(downloadFileControl.getDownloadFile(), updateType = [FILESTATUS_COL, RETRY_COL])
found = True
break
if not found:
downloadFile = self.downloadFileList.getDownloadFileById(id)
#downloadFile.setStatus(STAT_S)
downloadFile.resetInfo()
self.mainFrame.update(downloadFile, updateType = [FILENAME_COL, FILESTATUS_COL])
else:
#self.isStopped = True
for downloadFileControl in self.downloadFileControlList:
#if downloadFileControl.isAlive():
#downloadFileControl.getDownloadFile().setStatus(STAT_S)
downloadFileControl.resetSettings()
self.mainFrame.update(downloadFileControl.getDownloadFile(), updateType = [FILESTATUS_COL])
#stop the download
def stopDownload(self, id = None):
if not id is None:
found = False
#self.downloadFileList.changeStatus(fileURL, STAT_S)
for downloadFileControl in self.downloadFileControlList:
if downloadFileControl.getDownloadFile().getId() == id:
downloadFileControl.stop()
self.mainFrame.update(downloadFileControl.getDownloadFile(), updateType = [FILESTATUS_COL])
found = True
downloadFileControl.reset()
break
if not found:
downloadFile = self.downloadFileList.getDownloadFileById(id)
downloadFile.setStatus(STAT_S)
self.mainFrame.update(downloadFile, updateType = [FILESTATUS_COL])
else:
self.autoDownload = False
for downloadFileControl in self.downloadFileControlList:
#if downloadFileControl.isAlive():
#downloadFileControl.getDownloadFile().setStatus(STAT_S)
downloadFileControl.stop()
self.mainFrame.update(downloadFileControl.getDownloadFile(), updateType = [FILESTATUS_COL])
downloadFileControl.reset()
def continueDownload(self, id = None):
if not (id is None):
downloadFile = self.downloadFileList.getDownloadFileById(id)
if downloadFile.getStatus() != STAT_D:
downloadFile.setStatus(STAT_Q)
self.mainFrame.update(downloadFile, updateType = [FILESTATUS_COL])
else:
self.downloadFileList.resetStatus(STAT_Q)
for downloadFile in self.downloadFileList.getList():
self.mainFrame.update(downloadFile, updateType = [FILESTATUS_COL])
self.autoDownload = True
def deleteDownloadTop(self, id = None):
#print 'deleteDownloadTop ', id
if not (id is None):
found = False
for downloadFileControl in self.downloadFileControlList:
if downloadFileControl.getDownloadFile().getId() == id:
found = True
self.mainFrame.deleteDownloadFileFromList(downloadFileControl.getDownloadFile().getId(), PANEL_TOP)
downloadFileControl.delete()
self.downloadFileList.deleteDownloadFileFromDownloadList(downloadFileControl.getDownloadFile().getId())
if not found:
self.downloadFileList.deleteDownloadFileFromDownloadList(id)
self.mainFrame.deleteDownloadFileFromList(id, PANEL_TOP)
else:
self.isStopped = True
for downloadFileControl in self.downloadFileControlList:
self.mainFrame.deleteDownloadFileFromList(downloadFileControl.getDownloadFile().getId(), PANEL_TOP)
downloadFileControl.delete()
self.downloadFileList.deleteDownloadFileFromDownloadList(downloadFileControl.getDownloadFile().getId())
self.downloadFileList.emptyList()
self.mainFrame.emptyList(PANEL_TOP)
def deleteDownloadBot(self, id = None):
#print 'deleteDownloadBot ', id
if not (id is None):
self.mainFrame.deleteDownloadFileFromList(id, PANEL_BOT)
self.downloadFileList.deleteDownloadFileFromCompletedList(id)
else:
self.mainFrame.emptyList(PANEL_BOT)
def run(self):
if self.mainFrame is None:
self.log.debug('No mainFrame found, cannot continue')
sys.exit()
multiHandler = pycurl.CurlMulti()
multiHandler.handles = []
i = 0
#while self.toContinue:
while not self.isStopped:
#noDFile = self.downloadFileList.getNumberOfDownloadingFile()
#noQFile = self.downloadFileList.getNumberOfQueueingFile()
#noQueueingCurlObject = len(self.queueingCurlObjectList)
maxConn = Config.settings.maxConcurrentDownload
num_handles = 0
while (not self.isStopped) and (self.autoDownload) and (self.downloadFileList.getNumberOfQueueingFile() > 0):
if len(self.downloadFileControlList) < maxConn:
#and self.downloadFileList.getNumberOfDownloadingFile() < maxConn):
downloadFile = self.downloadFileList.getQueueingFile()
if not (downloadFile is None):
downloadFile.resetRetry()
self.log.debug('Control, Creating:', i, 'instance downloadFileControl', downloadFile.getId(), downloadFile.getFileURL())
downloadFileControl = DownloadFileControl(self, self.saveFileControl, downloadFile)
downloadFile.setStatus(STAT_D)
#downloadFileControl.start()
downloadFileControl.run()
self.downloadFileControlList.append(downloadFileControl)
i += 1
else:
for downloadFileControl in self.downloadFileControlList:
if downloadFileControl.isDone() and (self.downloadFileList.getNumberOfQueueingFile() > 0):
downloadFile = self.downloadFileList.getQueueingFile()
#print 'Control downloadFile is ', downloadFile
downloadFileControl.setDownloadFile(downloadFile)
self.log.debug('Control, Assign downloadFile for downloadFileControl', downloadFile.getId(), downloadFile.getFileURL())
downloadFileControl.continueBuildCurl()
break
#while True:
while not self.isStopped:
curlObject = self.getCurlObject()
if not (curlObject is None):
multiHandler.add_handle(curlObject)
else:
break
#while True:
while not self.isStopped:
ret, num_handles = multiHandler.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
#print 'Perfoming break', ret, ' ', num_handles
break
#while True:
while not self.isStopped:
num_q, ok_list, err_list = multiHandler.info_read()
for c in ok_list:
#c.close()
multiHandler.remove_handle(c)
#print 'ok_list removed handle ', c
httpCode = int(c.getinfo(pycurl.HTTP_CODE))
#print 'httpCode ', httpCode
for downloadFileControl in self.downloadFileControlList:
#print 'Debug ', downloadFileControl
self.log.debug('Control OK list checking', downloadFileControl.getDownloadFile().getId(), c.downloadFileId)
if downloadFileControl.getDownloadFile().getId() == c.downloadFileId:
if not (c.partNo is None):
self.log.debug('Control curl object belongs to downloadPartControl, checkFinish now')
downloadFileControl.checkFinish()
#downloadFileControl.reset()
break
if httpCode == 404:
self.log.debug('Control OK list 404', downloadFileControl.getDownloadFile().getId())
downloadFileControl.getDownloadFile().setStatus(STAT_E)
downloadFileControl.getDownloadFile().setErrorStr('404 File not found')
self.mainFrame.update(downloadFileControl.getDownloadFile())
downloadFileControl.reset()
elif (not downloadFileControl.isDone()) and (not downloadFileControl.isBusy()):
self.log.debug('Control OK list continueBuildCurl', downloadFileControl.getDownloadFile().getId())
downloadFileControl.continueBuildCurl()
break
#print 'Success: ', c.downloadFileId
for c, errno, errmsg in err_list:
#c.close()
multiHandler.remove_handle(c)
#print 'err_list removed handle ', c
self.log.debug('Control ERROR list error', errno, errmsg)
httpCode = int(c.getinfo(pycurl.HTTP_CODE))
#print 'err_list httpCode ', httpCode
for downloadFileControl in self.downloadFileControlList:
#print 'Debug ', downloadFileControl
if downloadFileControl.getDownloadFile().getId() == c.downloadFileId:
self.log.debug('Control ERROR list checking', downloadFileControl.getDownloadFile().getId(), c.downloadFileId)
if not errno in (pycurl.E_PARTIAL_FILE, pycurl.E_WRITE_ERROR, pycurl.E_ABORTED_BY_CALLBACK):
self.log.debug('Control ERROR list error unexpected', errno, errmsg)
if downloadFileControl.getDownloadFile().isRetryPossible():
self.log.debug('Control ERROR list retrying')
downloadFileControl.getDownloadFile().setStatus(STAT_E)
downloadFileControl.getDownloadFile().setErrorStr(errmsg)
self.mainFrame.update(downloadFileControl.getDownloadFile(), updateType = [FILESTATUS_COL, FILEERROR_COL])
if not (c.partNo is None):
self.log.debug('Control ERROR list downloadPart failed, resetting part')
downloadFileControl.resetPart(c.partNo)
else:
self.log.debug('Control ERROR list downloadFile failed, continueBuildCurl')
downloadFileControl.continueBuildCurl(True)
else:
self.log.debug('Control ERROR list no more retry possible, report error')
downloadFileControl.reportError(errmsg)
break
if httpCode == 404:
self.log.debug('Control ERROR list 404')
downloadFileControl.getDownloadFile().setStatus(STAT_E)
downloadFileControl.getDownloadFile().setErrorStr('404 File not found')
self.mainFrame.update(downloadFileControl.getDownloadFile())
downloadFileControl.reset()
elif (not downloadFileControl.isDone()) and (not downloadFileControl.isBusy()) and (errno != pycurl.E_ABORTED_BY_CALLBACK):
self.log.debug('Control ERROR list continueBuildCurl', downloadFileControl.getDownloadFile().getId())
downloadFileControl.continueBuildCurl()
break
#print 'err_list Failed: ', ' errno ', errno, ' errmsg ', errmsg
#freelist.append(c)
#num_processed = num_processed + len(ok_list) + len(err_list)
if num_q == 0:
break
if num_handles == 0:
#print 'No more connection, I sleep a bit'
time.sleep(0.5)
else:
ret = multiHandler.select(1.0)
if ret == -1:
continue
#while (True):
while not self.isStopped:
ret, num_handles = multiHandler.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
for downloadFileControl in self.downloadFileControlList:
#if (downloadFileControl.isAlive()):
# downloadFileControl.join()
downloadFileControl.kill()
self.mainFrame.quitNow()
self.saveFileControl.kill()
self.saveFileControl.join()
#print 'Control Quit'
def finishFile(self, downloadFile):
if self.isStopped:
return
self.mainFrame.deleteDownloadFileFromList(downloadFile.getId(), PANEL_TOP)
self.mainFrame.addDownloadFileToList(downloadFile, PANEL_BOT)
self.downloadFileList.deleteDownloadFileFromDownloadList(downloadFile.getId(), True)
def report(self, downloadFile, updateType):
if self.isStopped:
return
self.mainFrame.update(downloadFile, updateType)
def printDebug(self):
print '==================================='
print 'Total thread: ', threading.activeCount()
print 'Total downloading File: ', len(self.downloadFileList.getList())
print 'List download File: '
for downloadFile in self.downloadFileList.getList():
print 'id ', downloadFile.getId(), ' status: ', downloadFile.getStatus()
print 'List completed File: ', len(self.downloadFileList.getList(True))
for downloadFile in self.downloadFileList.getList(True):
print 'id ', downloadFile.getId(), ' status: ', downloadFile.getStatus()
print 'List DownloadFileControl: '
for downloadFileControl in self.downloadFileControlList:
downloadFile = downloadFileControl.getDownloadFile()
print 'id ', downloadFile.getId(), ' busy: ', downloadFileControl.busy, ', done: ', downloadFileControl.done, ', gotJob: ', downloadFileControl.gotJob