Spaces:
Sleeping
Sleeping
import requests | |
import time | |
import json | |
BASE_URL = "https://codeforces.com/api/" | |
def fetch_all_users(): | |
"""Fetch all users by combining the user.ratedList and user.status endpoints.""" | |
print("Fetching all rated users...") | |
response = requests.get(f"{BASE_URL}user.ratedList?activeOnly=false") | |
if response.status_code != 200: | |
print("Failed to fetch rated users.") | |
return [] | |
users = response.json().get("result", []) | |
return [user["handle"] for user in users] | |
def fetch_user_data(handle): | |
"""Fetch all available data for a single user.""" | |
print(f"Fetching data for user: {handle}") | |
data = {} | |
# Fetch basic user info | |
user_info = requests.get(f"{BASE_URL}user.info?handles={handle}") | |
if user_info.status_code == 200: | |
data["info"] = user_info.json().get("result", []) | |
# Fetch user submissions | |
user_status = requests.get(f"{BASE_URL}user.status?handle={handle}") | |
if user_status.status_code == 200: | |
data["submissions"] = user_status.json().get("result", []) | |
# Fetch user rating changes | |
user_rating = requests.get(f"{BASE_URL}user.rating?handle={handle}") | |
if user_rating.status_code == 200: | |
data["ratings"] = user_rating.json().get("result", []) | |
return data | |
def save_data_to_json(all_data, filename="all_users_data.json"): | |
"""Save fetched data to a JSON file.""" | |
with open(filename, "w", encoding="utf-8") as f: | |
json.dump(all_data, f, indent=4) | |
def fetch_all_user_data(): | |
"""Fetch all kinds of data for every user and save after each one.""" | |
users = fetch_all_users() | |
all_data = {} | |
for i, user in enumerate(users): | |
try: | |
user_data = fetch_user_data(user) | |
all_data[user] = user_data | |
# Save after each user | |
save_data_to_json(all_data) | |
print(f"Data for user {user} saved.") | |
except Exception as e: | |
print(f"Error fetching data for user {user}: {e}") | |
# Fetch data for a limited number of users for testing purposes (uncomment below) | |
# if i > 10: break | |
return all_data | |
if __name__ == "__main__": | |
# Fetch all data for all users and save after each one | |
fetch_all_user_data() | |
print("Data fetching complete. All data saved to 'all_users_data.json'.") |