Exemplo n.º 1
0
	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
Exemplo n.º 2
0
	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())
Exemplo n.º 3
0
	def __init__(self, downloadPart, downloadFileControl):
		#threading.Thread.__init__(self)
		self.toContinue = True
		#self.conf = Config()
		self.log = Log()
		self.downloadFileControl = downloadFileControl
		self.byteDownloaded = 0
		self.downloadPart = downloadPart
		self.fileObj = None
		self.reportedTime = 0
		self.toDelete = False
		self.inUse = True
		
		self.curlClass = CurlClass(self.downloadPart.getId())
		self.log.debug('DownloadPartControl, Created curl object', self.downloadPart.getId())
		self.curl = None
Exemplo n.º 4
0
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())
Exemplo n.º 5
0
class DownloadPartControl:

	def __init__(self, downloadPart, downloadFileControl):
		#threading.Thread.__init__(self)
		self.toContinue = True
		#self.conf = Config()
		self.log = Log()
		self.downloadFileControl = downloadFileControl
		self.byteDownloaded = 0
		self.downloadPart = downloadPart
		self.fileObj = None
		self.reportedTime = 0
		self.toDelete = False
		self.inUse = True
		
		self.curlClass = CurlClass(self.downloadPart.getId())
		self.log.debug('DownloadPartControl, Created curl object', self.downloadPart.getId())
		self.curl = None

		
	def getDownloadPart(self):
		return self.downloadPart
		
		
	def setDownloadPart(self, downloadPart):
		self.toContinue = True
		self.byteDownloaded = 0
		self.downloadPart = downloadPart
		self.fileObj = None
		self.reportedTime = 0
		self.toDelete = False
		self.inUse = True
		self.curlClass.setDownloadFileId(self.downloadPart.getId())
		
	def stop(self):
		self.toContinue = False
		
	def delete(self):
		if Config.checkExistence(self.downloadPart.getTmpFileName(), TYPE_FILE) > 0 :
			os.remove(self.downloadPart.getTmpFileName())
		self.toDelete = True

	def run(self):
			
		try:
		
			#self.log.debug('DownloadPartControl run')
						
			self.downloadPart.setRange()
			
			if self.downloadPart.isCompleted() :
				self.log.debug('DownloadPartControl THIS PART IS COMPLETED, QUIT', self.downloadPart.getId())
				#self.downloadFileControl.finishPart(self.downloadPart)
				return
			
			tmpFileName = self.downloadPart.getTmpFileName()
			partNo = self.downloadPart.getPartNo()
			currentSize = None
			
			if self.downloadPart.isResuming() :								
				#self.log.debug('RESUMING DOWNLOAD', self.downloadPart.getId())
				self.fileObj = open(tmpFileName, 'r+')
				self.fileObj.seek(self.downloadPart.getFileSeekPos())				
			else:
				#self.log.debug('DOWNLOAD NEW', self.downloadPart.getId())
				self.fileObj = open(tmpFileName, 'w')

			self.curlClass.setDownloadRange(self.downloadPart.getDownloadRange())
			self.curlClass.setFormInfo(self.downloadPart.getFileURL())
			
			if Config.checkServerURL(self.downloadPart.getFileURL(), RAPIDSHARE):
				#print 'DownloadPartControl Setting cookie ', self.downloadPart.getId()
				self.curlClass.setCookie()
				
			self.curlClass.setProcessBody(self.writeBody)
			self.curlClass.setProgress(self.progressBody)
			#self.curlClass.setControlType(CURL_DLPART)
			self.curlClass.setPartNo(self.downloadPart.getPartNo())
			#print 'Perform now'
			self.curl = self.curlClass.getCurlObject()
			
			#print 'DownloadPartControl adding curlObject ', self.curl
			self.downloadFileControl.addCurlObjectToControl(self.curl)
			#print 'DownloadPartControl added curlObject ', self.downloadPart.getId()
		
		except pycurl.error, e:
			self.log.debug('DownloadPartControl Exception pycurl.error', pycurl.error, e, self.downloadPart.getId(),)
			self.fileObj.flush()
			self.fileObj.close()
				
			#print 'DownloadPartControl pycurl.error:', e
			error = str(e)
			errorCode = int(error[error.find('(') + 1 : error.find(',')])		
			if errorCode == 23 or errorCode == 42 :
				if self.toDelete :
					os.remove(self.downloadPart.getTmpFileName())
			else:
				errorStr = error[error.find('"') + 1 : error.rfind(')') - 1]				
				self.downloadFileControl.reportError(errorStr)
		except IOError, e:
			self.log.debug('DownloadPartControl Exception IOError', e, self.downloadPart.getId(),)
			if self.fileObj and not self.fileObj.closed :
				self.fileObj.flush()
				self.fileObj.close()		
			self.downloadFileControl.reportError(str(e))