Created
February 6, 2019 04:30
-
-
Save bcbwilla/f2792067c7f525155eebb29fce708fe4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import operator | |
import requests | |
from bs4 import BeautifulSoup | |
def get_data_for_category(soup, data_category_id): | |
output_data = {} | |
data_rows = soup.find_all("div", attrs={"data-category-id": data_category_id})[0] | |
for row in data_rows.find_all("div", "ProgressBar-textWrapper"): | |
hero = row.find("div", "ProgressBar-title").string | |
value = row.find("div", "ProgressBar-description").string | |
output_data[hero] = value | |
return output_data | |
def get_seconds(time_str): | |
time_chunks = time_str.split(':') | |
if len(time_chunks) == 3: | |
h, m, s = time_chunks | |
return int(h) * 3600 + int(m) * 60 + int(s) | |
elif len(time_chunks) == 2: | |
m, s = time_chunks | |
return int(m) * 60 + int(s) | |
else: | |
s = time_chunks | |
return int(s) | |
def normalize(val, vals): | |
return 2*((val - min(vals)) / (max(vals) - min(vals))) - 1 | |
data_category_ids = { | |
"time_played": "0x0860000000000021", | |
"games_won": "0x0860000000000039" | |
} | |
def main(gamer_tag): | |
r = requests.get('https://playoverwatch.com/en-gb/career/pc/{}'.format(gamer_tag)) | |
soup = BeautifulSoup(r.content, 'html.parser') | |
time_played_raw = get_data_for_category(soup, data_category_ids["time_played"]) | |
games_won_raw = get_data_for_category(soup, data_category_ids["games_won"]) | |
hours_played = {k: get_seconds(v)/3600.0 for k, v in time_played_raw.items()} | |
games_won = {k: int(v) for k, v in games_won_raw.items()} | |
hero_score = {} | |
for hero, wins in games_won.items(): | |
hero_score[hero] = wins / float(hours_played[hero]) | |
hero_index = {} | |
for hero, score in hero_score.items(): | |
hero_index[hero] = normalize(score, hero_score.values()) | |
for hero, index in sorted(hero_index.items(), key=operator.itemgetter(1), reverse=True): | |
print(u'{:14s} {: 4.3f}'.format(hero, index)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment