def lookup_username(name): if not valid_minecraft_username(name): return MCName.build_slack_attachment(name, None, valid_name=False) response = get_player_uuid_response(name) return MCName.build_slack_attachment(name, response)
def scrape_stats(player_name): """ Scrapes Overcast Network Player Stats Based on code by @McSpider https://github.com/McSpider/Overcast-IRC-Bot/blob/master/_functions/player.py """ result = {} result['name'] = player_name result['error'] = '' if not valid_minecraft_username(player_name): result['error'] = ':warning: Invalid player name!' return build_slack_attachment(result) r = requests.get(get_player_link(player_name)) if r.status_code != requests.codes.ok: if r.status_code == 404: result['error'] = '404 - User not found' if r.status_code == 522: result['error'] = '522 - Request Timed Out' else: result['error'] = 'Request Exception - Code: ' + str(r.status_code) else: soup = BeautifulSoup(r.text) if soup.find("h4", text=["Account Suspended"]): result['error'] = 'User Account Suspended' elif soup.find("p", text=["Page Exploded"]): result['error'] = '404 - User not found' elif soup.find("small", text=["server joins"]): result['kills'] = soup.find("small", text=["kills"]).findParent('h2').contents[0].strip('\n') result['deaths'] = soup.find("small", text=["deaths"]).findParent('h2').contents[0].strip('\n') result['friends'] = soup.find("small", text=["friends"]).findParent('h2').contents[0].strip('\n') result['kd_ratio'] = soup.find("small", text=["kd ratio"]).findParent('h2').contents[0].strip('\n') result['kk_ratio'] = soup.find("small", text=["kk ratio"]).findParent('h2').contents[0].strip('\n') result['joins'] = soup.find("small", text=["server joins"]).findParent('h2').contents[0].strip('\n') result['raindrops'] = soup.find("small", text=["raindrops"]).findParent('h2').contents[0].strip('\n') result['wools'] = "0" result['cores'] = "0" result['monuments'] = "0" wools_element = soup.find("small", text=["wools placed"]) if wools_element: result['wools'] = wools_element.findParent('h2').contents[0].strip('\n') cores_element = soup.find("small", text=["cores leaked"]) if cores_element: result['cores'] = cores_element.findParent('h2').contents[0].strip('\n') monuments_element = soup.find("small", text=["monuments destroyed"]) if monuments_element: result['monuments'] = monuments_element.findParent('h2').contents[0].strip('\n') else: result['error'] = 'Invalid user!' return build_slack_attachment(result)
def lookup_username(name): if not valid_minecraft_username(name): return build_slack_attachment(name, None, valid_name=False) payload = json.dumps(name) header = {'Content-type': 'application/json'} r = requests.post(mojang_profile_link, headers=header, data=payload) if r.status_code != requests.codes.ok: print "Can't get lookup Minecraft username %s!" % name return else: return build_slack_attachment(name, r.json())
def scrape_stats(player_name): """ Scrapes Overcast Network Player Stats Based on code by @McSpider https://github.com/McSpider/Overcast-IRC-Bot/blob/master/_functions/player.py Updated to work with new Overcast Network ranked profiles """ result = { 'name': player_name, } if not valid_minecraft_username(player_name): raise PluginException(':warning: Invalid player name!') r = requests.get(get_player_link(player_name)) if r.status_code != requests.codes.ok: if r.status_code == 404: raise PluginException('404 - User not found') if r.status_code == 522: raise PluginException('522 - Request Timed Out') else: raise PluginException('Request Exception - Code: %s' % str(r.status_code)) else: soup = BeautifulSoup(r.text) if soup.find("h4", text=["Account Suspended"]): result['error'] = 'User Account Suspended' elif soup.find("p", text=["Page Exploded"]): result['error'] = '404 - User not found' elif soup.find("div", {'class': 'stats'}): # Check if user is unrated if soup.find('div', {'class': 'unqualified'}): result['rank'] = 'N/A' result['rating'] = 'N/A' # Else get their rank and rating else: rank = soup.find('div', text=['rank']).findNext('a').contents result['rank'] = '%s%s' % (rank[0].strip(), rank[1].text.strip()) result['rating'] = soup.find('div', text=['rating']).findNext('div').text.strip() # Get kills, deaths, and calculate kd ratio result['kills'] = soup.find('div', text=['kills']).findNext('div')['title'].split(' ')[0].strip() result['deaths'] = soup.find('div', text=['deaths']).findNext('div')['title'].split(' ')[0].strip() result['kd_ratio'] = '%.2f' % (float(result['kills']) / max(1, float(result['deaths']))) # Get objectives if there are any if soup.find('p', text=['No objectives completed']): result['wools'] = 0 result['cores'] = 0 result['monuments'] = 0 else: objectives = soup.find('div', {'id': 'objectives'}) result['wools'] = objectives.find('small', text=['wools placed']).parent.contents[0].strip() result['monuments'] = objectives.find('small', text=['monuments destroyed']).parent.contents[ 0].strip() result['cores'] = objectives.find('small', text=['cores leaked']).parent.contents[0].strip() else: raise PluginException('Invalid user!') return PlayerStats.build_slack_attachment(result)