-
Notifications
You must be signed in to change notification settings - Fork 1
/
DownloadFileControl.py
477 lines (388 loc) · 15.5 KB
/
DownloadFileControl.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import socket
import urllib
#import urllib2
import cookielib
import os
import sys
import threading
import time
import pycurl
from math import *
#from getopt import getopt
from LRGParser import LRGParser
#import URLHandlers
from DownloadFile import DownloadFile
from DownloadPart import DownloadPart
from DownloadPartControl import DownloadPartControl
from Log import Log
from Const import *
from ConfigUtils import *
from CurlClass import CurlClass
#class DownloadFileControl(threading.Thread):
class DownloadFileControl:
def __init__(self, control, saveFileControl, downloadFile = None):
#threading.Thread.__init__(self)
self.log = Log()
self.isKilled = False
#self.aLog.info('Starting Download')
self.control = control
self.saveFileControl = saveFileControl
self.downloadFile = downloadFile
if (self.downloadFile != None):
#self.downloadFile.setStatus(STAT_D)
#fileName = Config.getFileNameFromURL(self.downloadFile.getFileURL())
#self.downloadFile.setFileName(fileName)
self.downloadFile.increaseRetry()
#print 'Trying #', self.downloadFile.getRetry(), ' ', self.downloadFile.getId()
self.control.report(self.downloadFile, updateType = [RETRY_COL])
#self.isRapidShareLink = self.downloadFile.checkServerURL(RAPIDSHARE)
self.curlClass = CurlClass(self.downloadFile.getId())
else:
self.curlClass = CurlClass(0)
self.curlObject = None
self.htmlBody = ''
self.busy = False
self.done = False
self.gotJob = True
self.downloadPartControlList = []
self.isChecking = False
#self.
#self.multiCurl = pycurl.CurlMulti()
#self.multiCurl.handles = []
def setDownloadFile(self, downloadFile):
#print 'DownloadFileControl downloadFile is ', downloadFile
self.downloadFile = downloadFile
#self.downloadFile.setStatus(STAT_D)
#fileName = Config.getFileNameFromURL(self.downloadFile.getFileURL())
#self.downloadFile.setFileName(fileName)
self.downloadFile.increaseRetry()
#print 'Trying #', self.downloadFile.getRetry(), ' ', self.downloadFile.getId()
self.control.report(self.downloadFile, updateType = [RETRY_COL])
self.htmlBody = ''
#self.downloadPartControlList = []
self.isChecking = False
self.curlClass = CurlClass(self.downloadFile.getId())
def isBusy(self):
return self.busy
def isDone(self):
return self.done
def continueBuildCurl(self, increaseRetry = False):
self.gotJob = True
#self.done = False
self.busy = True
if (increaseRetry):
self.downloadFile.increaseRetry()
self.control.report(self.downloadFile, updateType = [RETRY_COL])
self.run()
def reset(self):
self.busy = False
self.done = True
self.gotJob = False
def stop(self, status = None, toReset = True):
#self.isKilled = True
if status is None:
self.downloadFile.setStatus(STAT_S)
else:
self.downloadFile.setStatus(status)
for downloadPartControl in self.downloadPartControlList:
downloadPartControl.stop()
#self.busy = False
#self.done = True
#self.gotJob = False
if toReset:
self.reset()
def kill(self):
self.isKilled = True
self.downloadFile.setStatus(STAT_S)
for downloadPartControl in self.downloadPartControlList:
downloadPartControl.stop()
def delete(self):
self.toContinue = False
for downloadPartControl in self.downloadPartControlList:
downloadPartControl.delete()
#self.control.delete(self.downloadFile)
#self.reset()
#def setURL(self, aURL):
#self.aURL = aURL
#self.load()
def resetSettings(self):
self.downloadFile.resetInfo()
self.stop(STAT_Q)
#self.reset()
self.run()
def addCurlObjectToControl(self, curlObject):
self.control.addCurlObject(curlObject)
def run(self):
self.log.debug('DownloadFileControl run')
if self.isKilled:
self.log.debug('DownloadFileControl isKill')
return
if self.downloadFile is None:
self.log.debug('DownloadFileControl no downloadFile')
return
#continue
if self.gotJob:
self.done = False
self.busy = True
try:
self.htmlBody = '';
#print 'DownloadFileControl START ', self.downloadFile.getId()
self.downloadFile.setStatus(STAT_D)
self.control.report(self.downloadFile, updateType = [FILESTATUS_COL])
#rapidShareLink = Config.checkServerURL(self.downloadFile.getFileURL(), RAPIDSHARE)
#if (Config.settings.rapidshareUsername == '' and rapidShareLink):
# self.downloadFile.setStatus(STAT_E)
# self.downloadFile.setErrorStr(MSG_INVALID_USERNAME)
# self.control.report(self.downloadFile, updateType = [FILESTATUS_COL, FILEERROR_COL])
# self.reset()
# return
#print 'downloadFileControl downloadFile is ', self.downloadFile
if Config.checkExistence(self.downloadFile.getDestinationFileName(), TYPE_FILE) > 0 :
self.log.debug('downloadFileControl checkExistence', self.downloadFile.getDestinationFileName(), Config.checkExistence(self.downloadFile.getDestinationFileName(), TYPE_FILE))
self.downloadFile.setStatus(STAT_X)
self.control.report(self.downloadFile, updateType = [FILESTATUS_COL])
self.reset()
return
#curlClass = CurlClass()
formInfo = self.downloadFile.getFormInfo()
if formInfo:
#print 'GOT FORM ', self.downloadFile.getId()
self.curlClass.setFormInfo(self.downloadFile.getFormAction(), formInfo)
else:
#print 'NO FORM ', self.downloadFile.getId()
self.curlClass.setFormInfo(self.downloadFile.getFileURL())
if Config.checkServerURL(self.downloadFile.getFileURL(), RAPIDSHARE) :
self.curlClass.setCookie()
self.curlClass.setProcessHeader(self.processHeader)
self.curlClass.setProcessBody(self.processBody)
self.curlObject = self.curlClass.getCurlObject()
#print 'downloadFileControl adding curl ', self.curlObject
self.addCurlObjectToControl(self.curlObject)
#print 'DownloadFileControl curlObject added', self.downloadFile.getId()
self.gotJob = False
except pycurl.error, e:
self.log.debug('DownloadFileControl pycurl.error:', e, self.downloadFile.getId())
error = str(e)
if error.find('(') == -1 or error.find(',') == -1 :
return
errorCode = int(error[error.find('(') + 1 : error.find(',')])
#print errorCode
if errorCode == 23 :
#print '23 is oK, we return'
return
else:
#print 'NOT 23 may restart'
errorStr = error[error.find('"') + 1 : error.rfind(')') - 1]
self.downloadFile.setErrorStr(errorStr)
self.downloadFile.setStatus(STAT_E)
self.control.report(self.downloadFile, updateType = [FILESTATUS_COL, FILEERROR_COL])
#if (self.downloadFile.isRetryPossible()):
#self.downloadFile.increaseRetry()
#self.run()
except Exception, e:
self.log.debug('DownloadFileControl Exception:', e, self.downloadFile.getId())
#print 'DownloadFileControl Exception: ', self.downloadFile.getId(), ' ', e
#if(self.downloadFile.isRetryPossible()):
#self.downloadFile.increaseRetry()
#self.run()
#sys.exit(0)
#else:
#print 'MAX retry time reached, stop now'
#time.sleep(1.0)
def getDownloadFile(self):
return self.downloadFile
def fileError(self):
self.toContinue = False
self.downloadFile.setStatus(STAT_E)
#WTF is this???
self.downloadFile.setErrorStr('File not found')
self.control.report(self.downloadFile, updateType = [FILESTATUS_COL, FILEERROR_COL])
def reportProgress(self, downloadPart):
self.control.report(self.downloadFile, updateType = [FILESPEED_COL, FILECOMP_COL])
def donePart(self):
pass
def reportError(self, errorStr, downloadPart = None):
self.downloadFile.setErrorStr(errorStr)
self.stop(STAT_E)
self.control.report(self.downloadFile, updateType = [FILESTATUS_COL, FILEERROR_COL])
self.reset()
#if (downloadPart.isRetryPossible()):
# downloadPart.increaseRetry()
# downloadPart.setRange()
# #print 'DownloadFileCOntrol, timeout, create DownloadPartControl thread'
# downloadPartControl = DownloadPartControl(self.log, downloadPart, self)
# downloadPartControl.start()
# self.downloadPartControlList.append(downloadPartControl)
#else:
# print 'MAX RETRY REACHED, abord now ', self.downloadFile.getId()
def resetPart(self, partNo):
self.downloadFile.setStatus(STAT_D)
self.downloadFile.setErrorStr('')
self.downloadFile.increaseRetry()
self.control.report(self.downloadFile, updateType = [FILESTATUS_COL, RETRY_COL, FILEERROR_COL])
for downloadPartControl in self.downloadPartControlList:
if downloadPartControl.getDownloadPart().getPartNo() == partNo :
downloadPartControl.closeTmpFile()
downloadPartControl.run()
break
def finishPart(self, downloadPart):
self.checkFinish()
def checkFinish(self):
for downloadPartControl in self.downloadPartControlList:
if downloadPartControl.isInUse() and not downloadPartControl.isCompleted() :
return
if not self.isChecking :
self.isChecking = True
#print 'DONE DOWNLOADING ', self.downloadFile.getId()
self.downloadFile.setStatus(STAT_C)
self.control.report(self.downloadFile, updateType = [FILESTATUS_COL])
job = (Config.catFile, self.downloadFile, self.doneSaveFile)
self.saveFileControl.addJob(job)
def doneSaveFile(self, results = None):
if results:
result, errors = results
#print 'CALLBACK FROM SaveFileControl ', self.downloadFile.getId(), ' ', result, ' ', errors
for downloadPartControl in self.downloadPartControlList:
downloadPartControl.resetInUse()
if result:
self.downloadFile.setStatus(STAT_Z)
self.control.report(self.downloadFile, updateType = [FILESTATUS_COL])
self.control.finishFile(self.downloadFile)
self.reset()
else:
errorCode, errorStr = errors
self.reportError(errorStr)
def getDownloadPartControl(self):
for downloadPartControl in self.downloadPartControlList:
if not downloadPartControl.isInUse() :
self.log.debug('DownloadFileControl, found DownloadPartControl')
return downloadPartControl
self.log.debug('DownloadFileControl, NO DownloadPartControl CAN BE USE, downloadPartControlList is:', len(self.downloadPartControlList))
#No return means return None
#return None
def processTag(self, valList, linksDict):
formInfo = {}
action = None
#if DEBUG:
# if (len(valList) > 0):
# for atts in valList:
# for key, value in atts.items():
# print key + ': ' + value
if (self.downloadFile.getLinkType() == RAPIDSHARE_FOLDER) or (self.downloadFile.getLinkType() == URLCASH):
if len(linksDict) != 0:
for link in linksDict.values():
self.control.addURL(link)
self.downloadFile.setStatus(STAT_Z)
self.control.finishFile(self.downloadFile)
self.reset()
#return
for item in valList:
for key, value in item.items():
if key.find('action') != -1:
action = value
if (action.find('/cgi-bin/premium.cgi') != -1) or (action.find('/users/') != -1) :
action = 'http://rapidshare.com' + action
#print 'Action is ', action
else:
formInfo[key] = value
#print 'NEW LINKS: ' + action
if action :
self.downloadFile.setFormAction(action)
self.downloadFile.setFormInfo(formInfo)
#print 'DownloadFileControl, processTag, create DownloadFileControl thread'
#download = DownloadFileControl(self.log, self.downloadFile, self.control)
#download.start()
#self.downloadFile.setStatus(STAT_D)
def processHeader(self, buf):
#print 'Header :', buf
if buf.find(CONTENT_TYPE) != -1:
fileType = buf[buf.find(':') + 2 : buf.find(';')]
self.downloadFile.setFileType(fileType)
#print 'processHeader, File type: ', fileType
if buf.find(SET_COOKIE) != -1:
cookie = buf[buf.find(':') + 2 : buf.find(';')]
Config.settings.cookie = cookie
if buf.find(CONTENT_LENGTH) != -1:
fileSize = int(buf[buf.find(':') + 2 :])
self.downloadFile.setFileSize(fileSize)
#print 'processheader, File size: ', fileSize
if buf.find(ACCEPT_RANGE) != 1:
self.downloadFile.setResumable(True)
def processBody(self, buf):
if self.downloadFile.getFileType() == TEXTHTML :
self.htmlBody += buf
if buf.find('</html>') != -1:
#print '================================'
#print self.htmlBody
#print '================================'
toAddPassword = Config.checkServerURL(self.downloadFile.getFileURL(), RAPIDSHARE)
linkType = self.downloadFile.getLinkType()
parser = LRGParser(self, toAddPassword, linkType)
parser.feed(self.htmlBody)
#print 'DownloadFileControl Parser is done, put busy to False'
self.busy = False
#return pycurl.E_WRITE_ERROR
return 1
else:
#return pycurl.E_WRITE_ERROR
#print 'DownloadFileControl DONE ', self.downloadFile.getId()
#self.done = True
#print 'DownloadFileControl OH dear this is: ' + self.downloadFile.getFileType()
desFile = Config.settings.downloadDir + self.downloadFile.getFileName()
if Config.checkExistence(desFile, 'F') > 0 :
#print 'DownloadFileControl FILE EXIST', self.downloadFile.getId()
self.control.finishFile(self.downloadFile)
self.reset()
return 1
#if (self.downloadRange == None):
#print 'DownloadFileControl GOING TO CREATE THREADS ', self.downloadFile.getId()
numberOfPart = self.downloadFile.getNumberOfPart()
#fileSize = self.downloadFile.getFileSize()
#fileName = self.downloadFile.getFileName()
self.control.report(self.downloadFile, updateType = [FILESIZE_COL])
#if (fileSize % numberOfPart == 0):
# partSize = int(fileSize / numberOfPart)
#else:
# partSize = int(floor(fileSize / numberOfPart) + 1)
self.downloadFile.setPartSize()
#self.downloadPartControlList = []
if len(self.downloadFile.getDownloadPartList()) > 0 :
#print 'Got downloadPartList ', self.downloadFile.getId()
for downloadPart in self.downloadFile.getDownloadPartList():
#self.setRange(downloadPart)
#self.downloadFile.addDownloadPart(downloadPart)
#print '>>> PART: ', i, ' SIZE on disk: ', currentSize, ' Rollbacksize: ', Config.getResumeSize(), ' RANGE IS: ', dlrange
#print 'DownloadFileControl, processHeader, create DownloadPartControl Thread'
downloadPartControl = self.getDownloadPartControl()
if downloadPartControl is None:
self.log.debug('DownloadFileControl, no downloadPartControl avail, create new', self.downloadFile.getId())
downloadPartControl = DownloadPartControl(downloadPart, self)
self.downloadPartControlList.append(downloadPartControl)
else:
downloadPartControl.setDownloadPart(downloadPart)
#downloadPartControl.start()
downloadPartControl.run()
else:
#print 'NO downloadPartList ', self.downloadFile.getId()
for i in range(0, numberOfPart):
downloadPart = DownloadPart(self.downloadFile, i)
#self.setRange(downloadPart)
self.downloadFile.addDownloadPart(downloadPart)
#print '>>> PART: ', i, ' SIZE on disk: ', currentSize, ' Rollbacksize: ', Config.getResumeSize(), ' RANGE IS: ', dlrange
#print 'DownloadFileControl, processHeader, create DownloadPartControl Thread'
downloadPartControl = self.getDownloadPartControl()
if downloadPartControl is None:
self.log.debug('DownloadFileControl, no downloadPartControl avail, create new', self.downloadFile.getId())
downloadPartControl = DownloadPartControl(downloadPart, self)
self.downloadPartControlList.append(downloadPartControl)
else:
downloadPartControl.setDownloadPart(downloadPart)
#downloadPartControl.start()
downloadPartControl.run()
#self.downloadPartControlList.append(downloadPartControl)
if self.downloadFile.getStatus() == STAT_S :
self.stop()
self.checkFinish()
#return pycurl.E_WRITE_ERROR
#self.busy = False
return 1