Last active
February 29, 2016 01:22
-
-
Save K-Guan/acb07791dd8775deae33 to your computer and use it in GitHub Desktop.
A Python script which can auto generates the newest result for the Rep Wars.
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
#!/usr/bin/env python3 | |
import re | |
import requests | |
from bs4 import BeautifulSoup | |
from collections import namedtuple | |
def get_results(users): | |
""" | |
Get the targets and the rep of all users who have joined the RepWars. | |
`users` should be a dict, the key is the user's name and the value | |
is the user's id of the user name. | |
Returns a collections.namedtuple. | |
""" | |
# this is the link of stars broad in RepWars room | |
targets_link = 'http://chat.stackoverflow.com/chats/stars/97599' | |
# use BeautifulSoup to parse the HTML | |
soup = BeautifulSoup(requests.get(targets_link).text, "html.parser") | |
# get the text, remove the " - by <someone>", split it to a list, etc. | |
l = [i.extract() for i in soup.find('li')] | |
l = re.split('; *', [i.strip('- \r\n') for i in l if i.strip()][0].strip()) | |
targets_list = [re.split(': *| +|:', i) for i in l if i] | |
# convert it to a `dict` | |
# also remove the `k` and convert the number to float | |
targets_dict = dict((i[0], float(i[1].rstrip('k'))) for i in targets_list) | |
# create the namedtuple | |
Users = namedtuple('Users', ['target', 'rep', 'next_target', 'status']) | |
# the base user chat profile link | |
profile_link = 'http://chat.stackoverflow.com/users/' | |
for user, uid in users.items(): | |
soup = BeautifulSoup(requests.get(profile_link+str(uid)).text, | |
"html.parser") | |
# get the rep which user current has | |
rep = soup.find('span', {'class': "reputation-score"}).text | |
rep = float(rep)/1000 if 'k' not in rep else float(rep.rstrip('k')) | |
# check if user passed or not, or just miss | |
result = rep - targets_dict[user] | |
if result >= 0: | |
status = 'Passed' | |
elif result >= -0.2: | |
status = 'Just Miss' | |
else: | |
status = '' | |
if status != 'Passed': | |
next_target = targets_dict[user] | |
else: | |
if user == 'neferpitou': | |
next_target = targets_dict[user]+0.02 | |
else: | |
next_target = targets_dict[user]+0.5 | |
while (rep+0.1) > next_target: | |
next_target += 0.5 | |
users[user] = Users(str(targets_dict[user])+'k', | |
str(rep)+'k', | |
str(next_target)+'k', | |
status) | |
return users | |
users = {'Kevin': 5299236, | |
'MsYvette': 3956566, | |
'Bhargav': 4099593, | |
'Thaillie': 4050842, | |
'DavidG': 1663001, | |
'Tushar': 2025923, | |
'Tim': 1843331, | |
'Tunaki': 1743880, | |
'Petter': 5292302, | |
'neferpitou': 5217712} | |
results = get_results(users.copy()) | |
print('Name Projected Gained\n') | |
for user, result in sorted(results.items()): | |
print(user.ljust(11, ' '), end='') | |
print(result.target.ljust(13, ' '), end='') | |
print(result.rep.ljust(9, ' '), end='') | |
print(result.status) | |
print() | |
print('; '.join(' '.join((key, value.next_target)) | |
for key, value in sorted(results.items()))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo (users sorted by name):