File size: 2,440 Bytes
49beea7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7863770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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


import gradio as gr

def fetch_file():
    return "all_users_data.json"

# Gradio Interface
interface = gr.Interface(
    fn=fetch_file, 
    inputs=[], 
    outputs=gr.File(label="Download Fetched Data")
)

interface.launch()

fetch_all_user_data()
print("Data fetching complete. All data saved to 'all_users_data.json'.")