Skip to content

Commit b7cc898

Browse files
committed
Add robot
1 parent fd9d3fd commit b7cc898

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

automation/models/lcproblems.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package models
2+
3+
type LeetCodeProblemAll struct {
4+
UserName string `json:"user_name"`
5+
NumSolved int32 `json:"num_solved"`
6+
NumTotal int32 `json:"num_total"`
7+
AcEasy int32 `json:"ac_easy"`
8+
AcMedium int32 `json:"ac_medium"`
9+
AcHard int32 `json:"ac_hard"`
10+
StatStatusPairs []StatStatusPairs `json:"stat_status_pairs"`
11+
FrequencyHigh int32 `json:"frequency_high"`
12+
FrequencyMid int32 `json:"frequency_mid"`
13+
CategorySlug string `json:"category_slug"`
14+
}
15+
16+
type StatStatusPairs struct {
17+
Stat Stat `json:"stat"`
18+
Difficulty Difficulty `json:"difficulty"`
19+
PaidOnly bool `json:"paid_only"`
20+
IsFavor bool `json:"is_favor"`
21+
Frequency float64 `json:"frequency"`
22+
Progress float64 `json:"progress"`
23+
}
24+
25+
type Stat struct {
26+
QuestionTitle string `json:"question__title"`
27+
QuestionTitleSlug string `json:"question__title_slug"`
28+
TotalAcs float64 `json:"total_acs"`
29+
TotalSubmitted float64 `json:"total_submitted"`
30+
Acceptance string
31+
Difficulty string
32+
FrontendQuestionId int32 `json:"frontend_question_id"`
33+
}
34+
35+
type Difficulty struct {
36+
Level int32 `json:"level"`
37+
}
38+
39+
var DifficultyMap = map[int32]string{
40+
1: "Easy",
41+
2: "Medium",
42+
3: "Hard",
43+
}

automation/models/mdrow.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package models
2+
3+
import (
4+
"strconv"
5+
)
6+
7+
type Mdrow struct {
8+
FrontendQuestionId string `json:"question_id"`
9+
QuestionTitle string `json:"question__title"`
10+
QuestionTitleSlug string `json:"question__title_slug"`
11+
SolutionPath string `json:"solution_path"`
12+
Acceptance string `json:"acceptance"`
13+
Difficulty string `json:"difficulty"`
14+
Frequency string `json:"frequency"`
15+
}
16+
17+
// SortByQuestionId define
18+
type SortByQuestionId []Mdrow
19+
20+
func (a SortByQuestionId) Len() int { return len(a) }
21+
func (a SortByQuestionId) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
22+
func (a SortByQuestionId) Less(i, j int) bool {
23+
first, _ := strconv.Atoi(a[i].FrontendQuestionId)
24+
second, _ := strconv.Atoi(a[j].FrontendQuestionId)
25+
return first < second
26+
}

automation/render.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
m "github.com/halfrost/LeetCode-Go/automation/models"
7+
"io/ioutil"
8+
"net/http"
9+
"os"
10+
"sort"
11+
"strconv"
12+
)
13+
14+
func main() {
15+
resp, err := http.Get("https://leetcode.com/api/problems/all/")
16+
if err != nil {
17+
fmt.Println(err)
18+
return
19+
}
20+
defer resp.Body.Close()
21+
22+
var result []m.StatStatusPairs
23+
var lpa m.LeetCodeProblemAll
24+
body, err := ioutil.ReadAll(resp.Body)
25+
if err != nil {
26+
fmt.Println(err)
27+
return
28+
}
29+
30+
err = json.Unmarshal(body, &lpa)
31+
if err != nil {
32+
fmt.Println(err)
33+
return
34+
}
35+
result = lpa.StatStatusPairs
36+
//fmt.Println(result)
37+
mdrows := []m.Mdrow{}
38+
for i := 0; i < len(result); i++ {
39+
mdrows = append(mdrows, convertModel(result[i]))
40+
}
41+
sort.Sort(m.SortByQuestionId(mdrows))
42+
res, _ := json.Marshal(mdrows)
43+
write(res)
44+
//fmt.Println(resp.StatusCode)
45+
46+
if resp.StatusCode == 200 {
47+
fmt.Println("ok")
48+
}
49+
}
50+
51+
func write(content []byte) {
52+
file, err := os.OpenFile("leetcode_problem", os.O_RDWR|os.O_CREATE, 0777)
53+
if err != nil {
54+
fmt.Println(err)
55+
}
56+
defer file.Close()
57+
58+
_, err = file.Write(content)
59+
if err != nil {
60+
fmt.Println(err)
61+
}
62+
fmt.Println("write file successful")
63+
}
64+
65+
func convertModel(ssp m.StatStatusPairs) m.Mdrow {
66+
res := m.Mdrow{}
67+
res.FrontendQuestionId = strconv.FormatInt(int64(ssp.Stat.FrontendQuestionId), 10)
68+
res.QuestionTitle = ssp.Stat.QuestionTitle
69+
res.QuestionTitleSlug = ssp.Stat.QuestionTitleSlug
70+
// res.SolutionPath
71+
res.Acceptance = fmt.Sprintf("%.1f%%", (ssp.Stat.TotalAcs/ssp.Stat.TotalSubmitted)*100)
72+
res.Difficulty = m.DifficultyMap[ssp.Difficulty.Level]
73+
res.Frequency = fmt.Sprintf("%f", ssp.Frequency)
74+
return res
75+
}

0 commit comments

Comments
 (0)