示例#1
0
class QueueClass:
	def __init__(self):
		global profile_path
		db_file = os.path.join(xbmc.translatePath(profile_path), 'walter.db')
		sql_path = os.path.join(xbmc.translatePath(profile_path + '/resources/database'), '')
		self.DB = DatabaseClass(db_file, sql_path)

	def queue(self, name, url, src, media='', folder=''):
		print "Adding %s: %s to queue" % (media, name)
		try:
			number = self.DB.query("SELECT count(qid) + 1 FROM wt_download_queue WHERE status=0")
			self.DB.execute("INSERT INTO wt_download_queue(type, description, url, src, folder, num) VALUES(?,?,?,?,?,?)", [media, name, url, src, folder, number[0]])
			self.DB.commit()
			return True
		except:
			return False

	def Cancel(self, qid):
		# Implement Stop command here
		row = self.DB.query("SELECT uuid FROM wt_download_queue WHERE qid=?", [qid])
		uuid = row[0]
		abort_file = os.path.join(xbmc.translatePath(profile_path + 'cache/' + uuid), 'abort')
		fp = open(abort_file, 'w')
		fp.close()
		self.DB.execute("UPDATE wt_download_queue SET status=-1 WHERE qid=?", [qid])
		self.DB.commit()
		xbmc.executebuiltin("Container.Refresh")

	def clearFailed(self):
		self.DB.execute("DELETE FROM wt_download_queue WHERE status=3 or status=-1")
		self.DB.commit()
		return True

	def clearCompleted(self):
		self.DB.execute("UPDATE wt_download_queue SET status=4 WHERE status=2")
		self.DB.commit()
		return True

	def getQueue(self):
		rows = self.DB.query("SELECT * from wt_download_queue WHERE status > -1 AND status < 4 ORDER BY  status, num ASC", force_double_array=True)
		return rows

	def getStatus(self):
		global profile_path
		status = {}
		number = self.DB.query("SELECT count(qid) FROM wt_download_queue WHERE status=0")
		status['length'] = number[0]
		try:
			state_file = os.path.join(xbmc.translatePath(profile_path + 'cache'), 'state.cache')
			fp = open(state_file, 'r')
			data = fp.read().strip()
			fp.close()
			data = data.split('|')
			name = self.DB.query('SELECT * FROM wt_download_queue WHERE qid=?', [data[0]]) 
			status['name'] = name[2]
			
			status['cached'] = data[1]
			status['total'] = data[2]
			status['threads'] = data[3]
		except:
			pass

		return status
		
	def getQuickStatus(self):
		global profile_path
		status = {}
		state_file = os.path.join(xbmc.translatePath(profile_path + 'cache'), 'state.cache')
		fp = fp = open(state_file, 'r')
		data = fp.read().strip()
		fp.close()
		data = data.split('|')
		status['cached'] = int(data[1])
		status['total'] = int(data[2])
		status['percent'] = 100 * status['cached'] / status['total'] 
		return status