Beispiel #1
0
	def get_profile(self, id, board_num):
		"""
		Get user profile information as dictionary containing another
		with profile information.

		Args:
			id: string profile id found in a player's profile URL
			board_num: int representing timeframe of the info
				0=daily 1=weekly 2=monthly

		Returns:
			Dictionary with key 'player' which contains a dictionary
			of player stats in the given board_num.
		"""

		attempts = 0
		while attempts < 3:
			server = random.choice(self.tp_servers)
			html = helpers.request_server("http://tagpro-" + server + ".koalabeast.com/profile/" + id)
			if html is None:
				# got bad/no response, pick a new server before trying again
				server = random.choice(self.tp_servers)
				attempts += 1
			else:
				break

		plain = html.text
		soup = BeautifulSoup(plain, "html.parser")

		# extract data table
		container_div = soup.find('div', attrs={'id': 'all-stats'})
		table = container_div.find('table')

		# get table data as array
		rows = table.findAll('tr')
		data = self.table_to_array(rows)

		# create formatted results
		profile = {
			'win-percent': data[0][board_num],
			'games': data[2][board_num],
			'wins': data[3][board_num],
			'losses': data[5][board_num],
			'save-percent': data[7][board_num],
			'tags': data[8][board_num],
			'popped': data[9][board_num],
			'grabs': data[10][board_num],
			'captures': data[11][board_num],
			'hold': data[12][board_num],
			'prevent': data[13][board_num],
			'returns': data[14][board_num],
			'support': data[15][board_num],
			'powerup-percent': data[16][board_num],
			'time': data[19][board_num],
			'disconnects': data[20][board_num]
		}

		return profile
Beispiel #2
0
	def get_board(self, t_num):
		"""
		Get leaderboard as dictionary with array of rows.

		Args:
			t_num: int which determines table type
				0=day 1=week 2=month 3=rolling

		Returns:
			Dictionary with key 'board' which is an array of
			dictionaries that each contain info for a row of
			the specified board.
		"""

		# get the data from the server and create holder objects
		attempts = 0
		while attempts < 3:
			server = random.choice(self.tp_servers)
			html = helpers.request_server("http://tagpro-" + server + ".koalabeast.com/boards")
			if html is None:
				# got bad/no response, pick a new server before trying again
				server = random.choice(self.tp_servers)
				attempts += 1
			else:
				break

		plain = html.text
		soup = BeautifulSoup(plain, "html.parser")

		# determine the correct div to load based on t_num
		if t_num == 0:
			div_id = "board-Day"
		elif t_num == 1:
			div_id = "board-Week"
		elif t_num == 2:
			div_id = "board-Month"
		else: # invalid t_num parameter
			return None

		# extract data table
		container_div = soup.find('div', attrs={'id': div_id})
		table = container_div.find('table')
		rows = table.find_all('tr')

		#rows.pop(0) # remove header row

		data = []

		# add data table to array
		for row in rows:
			pid = ''
			if row.find('a'):
				pid = row.find('a')['href'].replace('/profile/', '')

			cols = row.find_all('td')
			cols = [ele.text.strip() for ele in cols]
			stripped_cols = [ele for ele in cols if ele]
			data.append([stripped_cols, pid]) # Get rid of empty values

		data.pop(0) # remove blank element at front of array

		# create json formatted
		results = {'board':[]}

		# create formatted results
		for row in data:
			row[0][1] = row[0][1].replace(",", "&#44;")

			results['board'].append({
				'rank': row[0][0],
				'name': row[0][1],
				'points': row[0][2],
				'id': row[1]
			})

		return results