Exemplo n.º 1
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())