-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.mjs
More file actions
126 lines (114 loc) · 3.57 KB
/
api.mjs
File metadata and controls
126 lines (114 loc) · 3.57 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
import express from 'express';
import axios from 'axios';
import cors from 'cors';
const app = express();
app.use(cors());
let integer = 0
const fetchCFContests = async () => {
const response = await axios.get('https://codeforces.com/api/contest.list');
const now = Date.now();
const oneYearAgo = now - (365 * 24 * 60 * 60 * 1000);
return response.data.result
.map(contest => ({
key: integer++,
id: contest.id.toString(),
name: contest.name.toString(),
startTime: new Date(contest.startTimeSeconds * 1000).toISOString(),
url: `https://codeforces.com/contest/${contest.id}`,
duration: (contest.durationSeconds / 60).toString(),
platform: 'codeforces'
}))
.filter(contest => {
const startTime = new Date(contest.startTime).getTime();
return startTime >= oneYearAgo && startTime <= now;
});
};
const fetchCCContests = async () => {
const response = await axios.get("https://www.codechef.com/api/list/contests/all");
const allContests = [
...response.data.future_contests,
...response.data.past_contests
];
return allContests.map(contest => ({
key: integer++,
id: contest.contest_code,
name: contest.contest_name,
url: `https://www.codechef.com/${contest.contest_code}`,
startTime: contest.contest_start_date,
duration: contest.contest_duration.toString(),
platform: "codechef"
}));
};
const fetchLCContests = async () => {
const response = await axios.post('https://leetcode.com/graphql', {
query: `
query {
allContests {
title
startTime
duration
titleSlug
}
}
`
});
const now = Date.now();
const oneYearAgo = now - (365 * 24 * 60 * 60 * 1000);
return response.data.data.allContests
.map(contest => ({
key: integer++,
id: contest.titleSlug,
name: contest.title,
startTime: new Date(contest.startTime * 1000).toISOString(),
url: `https://leetcode.com/contest/${contest.titleSlug}`,
duration: (contest.duration / 60).toString(),
platform: 'leetcode'
}))
.filter(contest => {
const startTime = new Date(contest.startTime).getTime();
return startTime >= oneYearAgo && startTime <= now;
});
};
export const fetchContests = async () => {
const cfContests = await fetchCFContests();
const ccContests = await fetchCCContests();
const lcContests = await fetchLCContests();
const contests = [...cfContests,...ccContests, ...lcContests];
return contests;
}
app.get('/contests', async (req, res) => {
try {
const contests = await fetchContests();
return res.json(contests);
} catch (err) {
res.status(500).json({ message: 'Failed to fetch contests' });
}
});
app.get('/contests/:platform', async (req, res) => {
const platform = req.params.platform;
try {
let contests;
if (platform === 'codeforces') {
contests = await fetchCFContests();
res.send('Codeforces API is currently not available');
} else if (platform === 'codechef') {
contests = await fetchCCContests();
} else if (platform === 'leetcode') {
contests = await fetchLCContests();
} else {
res.status(404).json({ message: 'Platform not found' });
}
res.json(contests.data);
} catch (err) {
res.status(500).json({ message: 'Failed to fetch contests',error:err});
}
});
app.get('/', (req, res) => {
res.send('Hello, the server is working!');
});
app.listen(3001, () => {
console.log('Server is running on port 3001');
});
fetchContests().then(contests => {
console.log(contests); // should log an array of contests
});