-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathGetSubmissionStatsCN.go
112 lines (97 loc) · 3.03 KB
/
GetSubmissionStatsCN.go
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
package src
import (
"context"
"math"
"github.com/machinebox/graphql"
)
func GetSubmissionStatsCN(username string) (SubmissionData, error) {
client1 := graphql.NewClient("https://leetcode.cn/graphql")
req1 := graphql.NewRequest(`
query userQuestionProgress($userSlug: String!) {
userProfileUserQuestionProgress(userSlug: $userSlug) {
numAcceptedQuestions {
count
}
numFailedQuestions {
count
}
numUntouchedQuestions {
count
}
}
}
`)
req1.Var("userSlug", username)
ctx1 := context.Background()
var respData1 UserQuestionCount
if err := client1.Run(ctx1, req1, &respData1); err != nil {
return SubmissionData{}, err
}
client2 := graphql.NewClient("https://leetcode.cn/graphql/noj-go/")
req2 := graphql.NewRequest(`
query problemSolvedBeatsStats($userSlug: String!) {
problemsSolvedBeatsStats(userSlug: $userSlug) {
percentage
}
}
`)
req2.Var("userSlug", username)
ctx2 := context.Background()
var respData2 UserProblemsSolved
if err := client2.Run(ctx2, req2, &respData2); err != nil {
return SubmissionData{}, err
}
var respData SubmissionData
respData.AllQuestionsCount = []struct {
Count int `json:"count"`
}{
{
Count: 0,
},
{
Count: 0,
},
{
Count: 0,
},
{
Count: 0,
},
}
for i := 0; i < 3; i++ {
respData.AllQuestionsCount[i+1].Count = respData1.UserProfileUserQuestionProgress.NumAcceptedQuestions[i].Count + respData1.UserProfileUserQuestionProgress.NumFailedQuestions[i].Count + respData1.UserProfileUserQuestionProgress.NumUntouchedQuestions[i].Count
}
respData.AllQuestionsCount[0].Count = respData.AllQuestionsCount[1].Count + respData.AllQuestionsCount[2].Count + respData.AllQuestionsCount[3].Count
respData.MatchedUser.ProblemsSolvedBeatsStats = respData2.ProblemsSolvedBeatsStats
for i := 0; i < len(respData.MatchedUser.ProblemsSolvedBeatsStats); i++ {
respData.MatchedUser.ProblemsSolvedBeatsStats[i].Percentage = math.Round(respData.MatchedUser.ProblemsSolvedBeatsStats[i].Percentage*10) / 10
}
respData.MatchedUser.SubmitStatsGlobal.AcSubmissionNum = []struct {
Count int `json:"count"`
Percentage float64 `json:"percentage"`
}{
{
Count: 0,
Percentage: 0,
},
{
Count: 0,
Percentage: 0,
},
{
Count: 0,
Percentage: 0,
},
{
Count: 0,
Percentage: 0,
},
}
for i := 0; i < 3; i++ {
respData.MatchedUser.SubmitStatsGlobal.AcSubmissionNum[i+1].Count = respData1.UserProfileUserQuestionProgress.NumAcceptedQuestions[i].Count
percentage := float64(respData1.UserProfileUserQuestionProgress.NumAcceptedQuestions[i].Count) / float64(respData.AllQuestionsCount[i+1].Count)
respData.MatchedUser.SubmitStatsGlobal.AcSubmissionNum[i+1].Percentage = math.Round(percentage*1000000) / 10000
}
respData.MatchedUser.SubmitStatsGlobal.AcSubmissionNum[0].Count = respData.MatchedUser.SubmitStatsGlobal.AcSubmissionNum[1].Count + respData.MatchedUser.SubmitStatsGlobal.AcSubmissionNum[2].Count + respData.MatchedUser.SubmitStatsGlobal.AcSubmissionNum[3].Count
return respData, nil
}