Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import time
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
BASE_URL = "https://codeforces.com/api/"
|
| 6 |
+
|
| 7 |
+
def fetch_all_users():
|
| 8 |
+
"""Fetch all users by combining the user.ratedList and user.status endpoints."""
|
| 9 |
+
print("Fetching all rated users...")
|
| 10 |
+
response = requests.get(f"{BASE_URL}user.ratedList?activeOnly=false")
|
| 11 |
+
if response.status_code != 200:
|
| 12 |
+
print("Failed to fetch rated users.")
|
| 13 |
+
return []
|
| 14 |
+
users = response.json().get("result", [])
|
| 15 |
+
return [user["handle"] for user in users]
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def fetch_user_data(handle):
|
| 19 |
+
"""Fetch all available data for a single user."""
|
| 20 |
+
print(f"Fetching data for user: {handle}")
|
| 21 |
+
data = {}
|
| 22 |
+
# Fetch basic user info
|
| 23 |
+
user_info = requests.get(f"{BASE_URL}user.info?handles={handle}")
|
| 24 |
+
if user_info.status_code == 200:
|
| 25 |
+
data["info"] = user_info.json().get("result", [])
|
| 26 |
+
# Fetch user submissions
|
| 27 |
+
user_status = requests.get(f"{BASE_URL}user.status?handle={handle}")
|
| 28 |
+
if user_status.status_code == 200:
|
| 29 |
+
data["submissions"] = user_status.json().get("result", [])
|
| 30 |
+
# Fetch user rating changes
|
| 31 |
+
user_rating = requests.get(f"{BASE_URL}user.rating?handle={handle}")
|
| 32 |
+
if user_rating.status_code == 200:
|
| 33 |
+
data["ratings"] = user_rating.json().get("result", [])
|
| 34 |
+
return data
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def save_data_to_json(all_data, filename="all_users_data.json"):
|
| 38 |
+
"""Save fetched data to a JSON file."""
|
| 39 |
+
with open(filename, "w", encoding="utf-8") as f:
|
| 40 |
+
json.dump(all_data, f, indent=4)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def fetch_all_user_data():
|
| 44 |
+
"""Fetch all kinds of data for every user and save after each one."""
|
| 45 |
+
users = fetch_all_users()
|
| 46 |
+
all_data = {}
|
| 47 |
+
for i, user in enumerate(users):
|
| 48 |
+
try:
|
| 49 |
+
user_data = fetch_user_data(user)
|
| 50 |
+
all_data[user] = user_data
|
| 51 |
+
# Save after each user
|
| 52 |
+
save_data_to_json(all_data)
|
| 53 |
+
print(f"Data for user {user} saved.")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Error fetching data for user {user}: {e}")
|
| 56 |
+
# Fetch data for a limited number of users for testing purposes (uncomment below)
|
| 57 |
+
# if i > 10: break
|
| 58 |
+
return all_data
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
# Fetch all data for all users and save after each one
|
| 63 |
+
fetch_all_user_data()
|
| 64 |
+
print("Data fetching complete. All data saved to 'all_users_data.json'.")
|