-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
180 lines (145 loc) · 6.93 KB
/
app.js
File metadata and controls
180 lines (145 loc) · 6.93 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import axios from "axios";
import express from "express";
import { readFile , writeFile } from 'fs/promises';
const app = express();
app.use(express.json());
app.get("/randProblem" , async (req,res) =>{
try{
const response = await axios.get("https://codeforces.com/api/problemset.problems", {
headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" }
});
const AllProblems = response.data.result;
var randid = Math.floor(Math.random() * AllProblems.problems.length);
const currentProblem = AllProblems.problems[randid];
var solvedCount = null
if(currentProblem.contestId != null){
solvedCount = AllProblems.problemStatistics.find(p =>
p.contestId == currentProblem.contestId
&& p.index === currentProblem.index
)
solvedCount = solvedCount.solvedCount;
/*const contests = await axios.get(`https://codeforces.com/api/contest.standings?contestId=${currentProblem.contestId}&asManager=false&from=1&count=5&showUnofficial=true`);
res.json(contests.data);*/
}
const formattedProblem = {
id :`${currentProblem.contestId}-${currentProblem.index}` ,
title: currentProblem.name,
difficulty: currentProblem.rating ? // original difficulty of the question
(currentProblem.rating < 1600 ? "Easy" : currentProblem.rating < 2000 ? "Medium" : "Hard")
: "Unknown",
rating : currentProblem.rating,
url: `https://codeforces.com/contest/${currentProblem.contestId}/problem/${currentProblem.index}`,
topics :currentProblem.tags ,
solved: false,
solvedCount:solvedCount,
solvedAt : null
};
res.json(formattedProblem);
}catch(error){
console.log("Error fetching problems:", error.response ? error.response.data : error.message);
res.status(500).json({error: "Failed to fetch problems", details: error.response ? error.response.data : error.message});
}
})
app.post("/userDetails", async (req, res) => {
const { handler } = req.body;
const ProgressPath = './data/progress.json'
const file2 = await readFile(ProgressPath , 'utf-8');
const progress = JSON.parse(file2);
const problemsPath = './data/problems.json'
const file = await readFile(problemsPath , 'utf-8');
const problemsFile = JSON.parse(file);
try {
const response = await axios.get(
`https://codeforces.com/api/user.status?handle=${handler}`
);
const problems = response.data.result;
var probRating = 0
var problemCount = 0
const questionsList = []
problems.forEach(question => {
//if(question.verdict == "OK" || question.verdict == "PARTIAL" ){
const formattedProblem = {
id :`${question.problem.contestId}-${question.problem.index}` ,
title: question.problem.name,
creationTime : question.creationTimeSeconds,
difficulty: question.problem.rating ? // original difficulty of the question
(question.problem.rating < 1600 ? "Easy" : question.problem.rating < 2000 ? "Medium" : "Hard")
: "Unknown",
rating : question.problem.rating,
url: `https://codeforces.com/contest/${question.problem.contestId}/problem/${question.problem.index}`,
topics :question.problem.tags ,
solved: question.verdict == "OK"? true : false ,
solvedAt : null
};
questionsList.push(formattedProblem);
/*
problemsFile.push(formattedProblem)
if (question.problem.rating) {
if(question.problem.rating <1600){
progress.difficultyCount["Easy"]++
}else if(question.problem.rating <2000){
progress.difficultyCount["Medium"]++
}else{
progress.difficultyCount["Hard"]++
}
probRating += question.problem.rating;
problemCount ++
}else{
progress.difficultyCount["undefined"]++
}
progress.totalSolved ++;
question.problem.tags.forEach(topic => {
if(progress.topicsSolved[topic]){
progress.topicsSolved[topic]++;
} else {
progress.topicsSolved[topic] = 1;
}
});
}*/
});
questionsList.sort((a, b) => a.creationTime - b.creationTime);
//var avgCount = problemCount > 0 ? probRating / problemCount : 0;
//await writeFile(ProgressPath , JSON.stringify(progress , null , 2));
//await writeFile(problemsPath, JSON.stringify(problemsFile, null, 2));
res.json(questionsList);
} catch (error) {
console.error("Error fetching data:", error);
res.status(500).json({ error: "Failed to fetch user data" });
}
});
app.post("/fetchDataset" , async(req,res)=>{
const handlersPath = './data/handlers.json'
const file3 = await readFile(handlersPath , 'utf-8');
const handlersFile = JSON.parse(file3);
try{
const response = await axios.get(
`https://codeforces.com/api/contest.list?gym=true`
);
const contests = response.data.result.slice(0, 500).map(contest => contest.id);
let allHandles = new Set(handlersFile)
for (let i = 0; i < contests.length; i += 5) {
const batch = contests.slice(i, i + 5).map(async contestId => {
try {
const response2 = await axios.get(
`https://codeforces.com/api/contest.standings?contestId=${contestId}&asManager=false&from=1&count=500&showUnofficial=true`
);
const contest = response2.data.result;
contest.rows.forEach(row => {
row.party.members.forEach(member => {
allHandles.add(member.handle);
});
});
} catch (error) {
console.error(`Failed to fetch contest ${contestId}:`, error.message);
}
});
await Promise.all(batch);
}
const updatedHandlers = [...allHandles];
await writeFile(handlersPath, JSON.stringify(updatedHandlers, null, 2));
res.json({ message: "Handlers fetched successfully", count: updatedHandlers.length });
} catch (error) {
res.status(500).json({ error: "Couldn't get contest data" });
}
});
app.listen(3000, () => console.log("Server running on port 3000"));