-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_solutions.py
57 lines (44 loc) · 1.94 KB
/
fetch_solutions.py
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
import requests
import os
def fetch_accepted_solutions(handle, output_dir="accepted_solutions"):
# Base API URL
submissions_url = f"https://codeforces.com/api/user.status?handle={handle}&from=1&count=1000"
try:
# Fetch submissions from the Codeforces API
response = requests.get(submissions_url)
response.raise_for_status()
data = response.json()
if data["status"] != "OK":
print("Failed to fetch submissions!")
return
# Process submissions
submissions = data["result"]
submissions = sorted(submissions,key=lambda x: x['id'],reverse=True)
accepted_solutions = []
total_accepted = 0
# Create output directory if it doesn't exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for submission in submissions:
if submission["verdict"] == "OK":
total_accepted += 1
submission_id = submission["id"]
contest_id = submission["contestId"]
problem_index = submission["problem"]["index"]
# Generate the file name
file_name = f"{submission_id}_{contest_id}_{problem_index}.cpp"
file_path = os.path.join(output_dir, file_name)
# Create an empty .cpp file
with open(file_path, "w", encoding="utf-8") as file:
file.write("") # No content written inside the file
accepted_solutions.append(file_name)
# Print summary
print("\nAccepted Solutions Saved:")
for solution in sorted(accepted_solutions):
print(solution)
print(f"\nTotal Accepted Solutions: {total_accepted}")
except requests.exceptions.RequestException as e:
print(f"Error fetching submissions: {e}")
if __name__ == "__main__":
# Replace with your Codeforces handle
fetch_accepted_solutions("PacemakerX")