Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,19 @@
|
|
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
|
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]
|
@@ -19,58 +23,80 @@ 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 |
-
|
35 |
-
|
36 |
|
37 |
-
|
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
|
44 |
-
"""Fetch all kinds of data for every user and save
|
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 |
-
|
51 |
-
|
52 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
def fetch_file():
|
64 |
-
return "all_users_data.json"
|
65 |
|
66 |
# Gradio Interface
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
interface.launch()
|
74 |
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
import time
|
3 |
import json
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
|
7 |
BASE_URL = "https://codeforces.com/api/"
|
8 |
+
DATA_FILE = "all_users_data.json"
|
9 |
+
|
10 |
|
11 |
def fetch_all_users():
|
12 |
+
"""Fetch all rated users."""
|
13 |
print("Fetching all rated users...")
|
14 |
response = requests.get(f"{BASE_URL}user.ratedList?activeOnly=false")
|
15 |
if response.status_code != 200:
|
16 |
+
print(f"Failed to fetch rated users. Status Code: {response.status_code}, Response: {response.text}")
|
17 |
return []
|
18 |
users = response.json().get("result", [])
|
19 |
return [user["handle"] for user in users]
|
|
|
23 |
"""Fetch all available data for a single user."""
|
24 |
print(f"Fetching data for user: {handle}")
|
25 |
data = {}
|
26 |
+
|
27 |
# Fetch basic user info
|
28 |
user_info = requests.get(f"{BASE_URL}user.info?handles={handle}")
|
29 |
if user_info.status_code == 200:
|
30 |
data["info"] = user_info.json().get("result", [])
|
31 |
+
else:
|
32 |
+
print(f"Warning: Failed to fetch user info for {handle}. Status Code: {user_info.status_code}, Response: {user_info.text}")
|
33 |
+
|
34 |
# Fetch user submissions
|
35 |
user_status = requests.get(f"{BASE_URL}user.status?handle={handle}")
|
36 |
if user_status.status_code == 200:
|
37 |
data["submissions"] = user_status.json().get("result", [])
|
38 |
+
else:
|
39 |
+
print(f"Warning: Failed to fetch submissions for {handle}. Status Code: {user_status.status_code}, Response: {user_status.text}")
|
40 |
+
|
41 |
# Fetch user rating changes
|
42 |
user_rating = requests.get(f"{BASE_URL}user.rating?handle={handle}")
|
43 |
if user_rating.status_code == 200:
|
44 |
data["ratings"] = user_rating.json().get("result", [])
|
45 |
+
else:
|
46 |
+
print(f"Warning: Failed to fetch rating changes for {handle}. Status Code: {user_rating.status_code}, Response: {user_rating.text}")
|
47 |
|
48 |
+
return data
|
|
|
|
|
|
|
49 |
|
50 |
|
51 |
+
def fetch_and_save_data():
|
52 |
+
"""Fetch all kinds of data for every user and save to a file."""
|
53 |
users = fetch_all_users()
|
54 |
+
if not users:
|
55 |
+
return "Failed to fetch users. Check logs for errors."
|
56 |
+
|
57 |
all_data = {}
|
58 |
+
|
59 |
for i, user in enumerate(users):
|
60 |
try:
|
61 |
user_data = fetch_user_data(user)
|
62 |
+
if user_data:
|
63 |
+
all_data[user] = user_data
|
64 |
+
time.sleep(0.5) # Respect API rate limit
|
|
|
65 |
except Exception as e:
|
66 |
print(f"Error fetching data for user {user}: {e}")
|
|
|
|
|
|
|
67 |
|
68 |
+
# Save data to JSON file
|
69 |
+
with open(DATA_FILE, "w", encoding="utf-8") as f:
|
70 |
+
json.dump(all_data, f, indent=4)
|
71 |
|
72 |
+
return f"Data fetching complete. Saved to {DATA_FILE}."
|
73 |
+
|
74 |
+
|
75 |
+
def serve_file():
|
76 |
+
"""Provide the saved file for download."""
|
77 |
+
if os.path.exists(DATA_FILE):
|
78 |
+
return DATA_FILE
|
79 |
+
else:
|
80 |
+
return None
|
81 |
|
|
|
|
|
82 |
|
83 |
# Gradio Interface
|
84 |
+
def fetch_and_download():
|
85 |
+
"""Fetch the data and provide a download link."""
|
86 |
+
message = fetch_and_save_data()
|
87 |
+
file_path = serve_file()
|
88 |
+
return message, file_path
|
89 |
|
|
|
90 |
|
91 |
+
with gr.Blocks() as interface:
|
92 |
+
gr.Markdown("# Codeforces User Data Fetcher")
|
93 |
+
gr.Markdown("This tool fetches data for all users from Codeforces and provides a downloadable file.")
|
94 |
+
|
95 |
+
with gr.Row():
|
96 |
+
fetch_button = gr.Button("Fetch Data")
|
97 |
+
output_message = gr.Textbox(label="Status")
|
98 |
+
download_file = gr.File(label="Download Data", visible=False)
|
99 |
+
|
100 |
+
fetch_button.click(fetch_and_download, inputs=[], outputs=[output_message, download_file])
|
101 |
+
|
102 |
+
interface.launch()
|