Skip to content

Instantly share code, notes, and snippets.

@K-Guan
Last active February 29, 2016 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save K-Guan/acb07791dd8775deae33 to your computer and use it in GitHub Desktop.
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.
#!/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())))
@K-Guan
Copy link
Author

K-Guan commented Jan 17, 2016

Demo (users sorted by name):

Name       Projected    Gained

Bhargav    21.0k        20.3k    
DavidG     28.5k        27.3k    
Kevin      8.0k         8.6k     Passed
MsYvette   6.5k         6.5k     Passed
Thaillie   1.5k         1.0k     
Tim        13.0k        12.9k    Just Miss
Tunaki     33.0k        32.5k    
Tushar     41.0k        41.2k    Passed

Bhargav 21.0k; DavidG 28.5k; Kevin 9.0k; MsYvette 7.0k; Thaillie 1.5k; Tim 13.0k; Tunaki 33.0k; Tushar 41.5k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment