-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathleetcode.js
105 lines (101 loc) · 2.66 KB
/
leetcode.js
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
//graphql query
const query = `
query getUserProfile($username: String!) {
allQuestionsCount {
difficulty
count
}
matchedUser(username: $username) {
contributions {
points
}
profile {
reputation
ranking
}
submissionCalendar
submitStats {
acSubmissionNum {
difficulty
count
submissions
}
totalSubmissionNum {
difficulty
count
submissions
}
}
}
recentSubmissionList(username: $username) {
title
titleSlug
timestamp
statusDisplay
lang
__typename
}
matchedUserStats: matchedUser(username: $username) {
submitStats: submitStatsGlobal {
acSubmissionNum {
difficulty
count
submissions
__typename
}
totalSubmissionNum {
difficulty
count
submissions
__typename
}
__typename
}
}
}
`;
// format data
const formatData = (data) => {
let sendData = {
totalSolved: data.matchedUser.submitStats.acSubmissionNum[0].count,
totalSubmissions: data.matchedUser.submitStats.totalSubmissionNum,
totalQuestions: data.allQuestionsCount[0].count,
easySolved: data.matchedUser.submitStats.acSubmissionNum[1].count,
totalEasy: data.allQuestionsCount[1].count,
mediumSolved: data.matchedUser.submitStats.acSubmissionNum[2].count,
totalMedium: data.allQuestionsCount[2].count,
hardSolved: data.matchedUser.submitStats.acSubmissionNum[3].count,
totalHard: data.allQuestionsCount[3].count,
ranking: data.matchedUser.profile.ranking,
contributionPoint: data.matchedUser.contributions.points,
reputation: data.matchedUser.profile.reputation,
submissionCalendar: JSON.parse(data.matchedUser.submissionCalendar),
recentSubmissions: data.recentSubmissionList,
matchedUserStats: data.matchedUser.submitStats
}
return sendData;
}
//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}}),
})
.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);
});
}