Last active
April 26, 2022 02:09
-
-
Save tdenewiler/3dc2ee12d0e3c57914107d0b2cba3934 to your computer and use it in GitHub Desktop.
Find ROS Answers users with most karma from answering questions
This file contains hidden or 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 python | |
"""Get interesting information from http://answers.ros.org.""" | |
from operator import itemgetter | |
import requests | |
from requests.exceptions import ConnectionError as ReqConnectionError | |
from tqdm import tqdm | |
from tabulate import tabulate | |
# pylint: disable=invalid-name | |
base_url = "http://answers.ros.org/api/v1/users" | |
rep_thresh = 3000 | |
resp = requests.get(base_url) | |
if resp.status_code != 200: | |
print(f"GET /api/v1/users/ {resp.status_code}") | |
num_users = resp.json()["count"] | |
pages = resp.json()["pages"] | |
print( | |
f"There are {num_users} users on {pages} pages. " | |
f"Looking at users with reputation greater than {rep_thresh}." | |
) | |
seen = set() | |
recruits = [] | |
session = requests.Session() | |
for i in tqdm(range(pages - 1)): | |
url = base_url + f"/?sort=reputation&page={i + 1}" | |
try: | |
resp = session.get(url).json() | |
except ReqConnectionError as exc: | |
print(f"Request failed: {exc}") | |
continue | |
for user in resp["users"]: | |
if user["username"] in seen: | |
continue | |
seen.add(user["username"]) | |
if user["reputation"] > rep_thresh and user["gold"] > 0: | |
ratio = user["reputation"] / user["gold"] | |
info = { | |
"ratio": round(ratio), | |
"username": user["username"], | |
"reputation": user["reputation"], | |
"gold": user["gold"], | |
} | |
recruits.append(info) | |
recruits_sorted = sorted(recruits, key=itemgetter("reputation"), reverse=True) | |
print(f"Found {len(recruits)} users.") | |
print( | |
tabulate( | |
recruits_sorted, | |
headers="keys", | |
showindex="always", | |
tablefmt="pretty", | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment