mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-14 07:51:14 +08:00
Add robot
This commit is contained in:
43
automation/models/lcproblems.go
Normal file
43
automation/models/lcproblems.go
Normal file
@ -0,0 +1,43 @@
|
||||
package models
|
||||
|
||||
type LeetCodeProblemAll struct {
|
||||
UserName string `json:"user_name"`
|
||||
NumSolved int32 `json:"num_solved"`
|
||||
NumTotal int32 `json:"num_total"`
|
||||
AcEasy int32 `json:"ac_easy"`
|
||||
AcMedium int32 `json:"ac_medium"`
|
||||
AcHard int32 `json:"ac_hard"`
|
||||
StatStatusPairs []StatStatusPairs `json:"stat_status_pairs"`
|
||||
FrequencyHigh int32 `json:"frequency_high"`
|
||||
FrequencyMid int32 `json:"frequency_mid"`
|
||||
CategorySlug string `json:"category_slug"`
|
||||
}
|
||||
|
||||
type StatStatusPairs struct {
|
||||
Stat Stat `json:"stat"`
|
||||
Difficulty Difficulty `json:"difficulty"`
|
||||
PaidOnly bool `json:"paid_only"`
|
||||
IsFavor bool `json:"is_favor"`
|
||||
Frequency float64 `json:"frequency"`
|
||||
Progress float64 `json:"progress"`
|
||||
}
|
||||
|
||||
type Stat struct {
|
||||
QuestionTitle string `json:"question__title"`
|
||||
QuestionTitleSlug string `json:"question__title_slug"`
|
||||
TotalAcs float64 `json:"total_acs"`
|
||||
TotalSubmitted float64 `json:"total_submitted"`
|
||||
Acceptance string
|
||||
Difficulty string
|
||||
FrontendQuestionId int32 `json:"frontend_question_id"`
|
||||
}
|
||||
|
||||
type Difficulty struct {
|
||||
Level int32 `json:"level"`
|
||||
}
|
||||
|
||||
var DifficultyMap = map[int32]string{
|
||||
1: "Easy",
|
||||
2: "Medium",
|
||||
3: "Hard",
|
||||
}
|
26
automation/models/mdrow.go
Normal file
26
automation/models/mdrow.go
Normal file
@ -0,0 +1,26 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Mdrow struct {
|
||||
FrontendQuestionId string `json:"question_id"`
|
||||
QuestionTitle string `json:"question__title"`
|
||||
QuestionTitleSlug string `json:"question__title_slug"`
|
||||
SolutionPath string `json:"solution_path"`
|
||||
Acceptance string `json:"acceptance"`
|
||||
Difficulty string `json:"difficulty"`
|
||||
Frequency string `json:"frequency"`
|
||||
}
|
||||
|
||||
// SortByQuestionId define
|
||||
type SortByQuestionId []Mdrow
|
||||
|
||||
func (a SortByQuestionId) Len() int { return len(a) }
|
||||
func (a SortByQuestionId) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a SortByQuestionId) Less(i, j int) bool {
|
||||
first, _ := strconv.Atoi(a[i].FrontendQuestionId)
|
||||
second, _ := strconv.Atoi(a[j].FrontendQuestionId)
|
||||
return first < second
|
||||
}
|
75
automation/render.go
Normal file
75
automation/render.go
Normal file
@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
m "github.com/halfrost/LeetCode-Go/automation/models"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
resp, err := http.Get("https://leetcode.com/api/problems/all/")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result []m.StatStatusPairs
|
||||
var lpa m.LeetCodeProblemAll
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &lpa)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
result = lpa.StatStatusPairs
|
||||
//fmt.Println(result)
|
||||
mdrows := []m.Mdrow{}
|
||||
for i := 0; i < len(result); i++ {
|
||||
mdrows = append(mdrows, convertModel(result[i]))
|
||||
}
|
||||
sort.Sort(m.SortByQuestionId(mdrows))
|
||||
res, _ := json.Marshal(mdrows)
|
||||
write(res)
|
||||
//fmt.Println(resp.StatusCode)
|
||||
|
||||
if resp.StatusCode == 200 {
|
||||
fmt.Println("ok")
|
||||
}
|
||||
}
|
||||
|
||||
func write(content []byte) {
|
||||
file, err := os.OpenFile("leetcode_problem", os.O_RDWR|os.O_CREATE, 0777)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(content)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println("write file successful")
|
||||
}
|
||||
|
||||
func convertModel(ssp m.StatStatusPairs) m.Mdrow {
|
||||
res := m.Mdrow{}
|
||||
res.FrontendQuestionId = strconv.FormatInt(int64(ssp.Stat.FrontendQuestionId), 10)
|
||||
res.QuestionTitle = ssp.Stat.QuestionTitle
|
||||
res.QuestionTitleSlug = ssp.Stat.QuestionTitleSlug
|
||||
// res.SolutionPath
|
||||
res.Acceptance = fmt.Sprintf("%.1f%%", (ssp.Stat.TotalAcs/ssp.Stat.TotalSubmitted)*100)
|
||||
res.Difficulty = m.DifficultyMap[ssp.Difficulty.Level]
|
||||
res.Frequency = fmt.Sprintf("%f", ssp.Frequency)
|
||||
return res
|
||||
}
|
Reference in New Issue
Block a user