forked from faisal-shohag/leetcode_api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode.js
More file actions
57 lines (50 loc) · 1.31 KB
/
leetcode.js
File metadata and controls
57 lines (50 loc) · 1.31 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
const fetch = require("cross-fetch");
//graphql query
const query = `
query userContestRankingInfo($username: String!) {
userContestRankingHistory(username: $username) {
rating
ranking
contest {
title
startTime
}
finishTimeInSeconds
}
}
`
// format data
const formatData = (data) => {
const historyObjects = data.userContestRankingHistory;
for (let i = 0; i < historyObjects.length; i++) {
if (historyObjects[i].ranking != 0) {
historyObjects.splice(0, Math.max(i - 1, 0));
break;
}
}
return historyObjects;
}
//fetching the data
exports.leetcode = (req, res) => {
let user = req.params.id;
fetch('https://leetcode.com/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Referer': 'https://leetcode.com'
},
body: JSON.stringify({query: query, variables: {username: user}, operationName: "userContestRankingInfo"}),
})
.then(result => result.json())
.then(data => {
if(data.errors){
res.send(data);
}else {
res.send(formatData(data.data));
}
})
.catch(err=>{
console.error('Error', err);
res.send(err);
});
}