forked from puiiyuen/leetcode-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode.js
66 lines (61 loc) · 1.99 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
require('dotenv').config();
const axios = require('axios').default;
const {
LEETCODE_USERNAME: username
} = process.env
const leetcodeURL = 'https://leetcode.com/graphql'
const getOriginalLeetCodeStats = async() => {
const response = await axios.post(leetcodeURL, {
query: `
query getUserProfile($username: String!) {
allQuestionsCount {
difficulty
count
}
matchedUser(username: $username) {
username
submitStats: submitStatsGlobal {
acSubmissionNum {
difficulty
count
submissions
}
totalSubmissionNum {
difficulty
count
submissions
}
}
}
}`,
variables: {
username: username
}
})
return response.data.data
}
exports.getLeetCodeStats = async function getLeetCodeStats() {
const originStat = await getOriginalLeetCodeStats()
const matchedUser = originStat.matchedUser
let result = {
"username": null,
"solved": []
}
if (matchedUser !== null) {
result.username = matchedUser.username
for (let i = 0; i < 4; i++) {
const difficulty = matchedUser.submitStats.acSubmissionNum[i].difficulty
const accepted = matchedUser.submitStats.acSubmissionNum[i].count
const allQuestion = originStat.allQuestionsCount[i].count
const acSubmission = matchedUser.submitStats.acSubmissionNum[i].submissions
const totalSubmission = matchedUser.submitStats.totalSubmissionNum[i].submissions
let question = {
"difficulty": difficulty,
"acceptedRate": totalSubmission == 0 ? 0.0 : (acSubmission / totalSubmission * 100),
"solvedRadio": accepted + "/" + allQuestion
}
result.solved.push(question)
}
}
return result;
}