-
Notifications
You must be signed in to change notification settings - Fork 548
/
data.go
149 lines (124 loc) · 3.14 KB
/
data.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"sort"
"github.com/aQuaYi/GoKit"
)
func update(categories []string) *leetcode {
newLC := lastest(categories)
oldLC, err := readLeetCodeRecord()
if err != nil {
log.Println("LeetCode 记录读取失败,无法与新记录对比:", err)
} else {
diff(newLC, oldLC)
}
saveLC(newLC)
return newLC
}
func lastest(categories []string) *leetcode {
lc := newLeetCode()
for _, c := range categories {
d := getData(c)
lc.update(d)
}
lc.totalCategory()
lc.getRanking()
sort.Sort(lc.Problems)
return lc
}
func readLeetCodeRecord() (*leetcode, error) {
if !GoKit.Exist(lcFile) {
msg := fmt.Sprintf("%s 不存在", lcFile)
return nil, errors.New(msg)
}
raw := read(lcFile)
lc := leetcode{}
if err := json.Unmarshal(raw, &lc); err != nil {
msg := fmt.Sprintf("获取 %s 失败:%s", lcFile, err)
return nil, errors.New(msg)
}
return &lc, nil
}
func diff(new, old *leetcode) {
// 对比 ranking
nr, or := new.Ranking, old.Ranking
if nr > 0 && or > 0 {
v, delta := "进步了", or-nr
if nr > or {
v, delta = "后退了", nr-or
}
log.Printf("当前排名 %d,%s %d 名", nr, v, delta)
} else {
log.Printf("当前排名 %d", nr)
}
// 对比 已完成的问题
lenNew := len(new.Problems)
lenOld := len(old.Problems)
// TODO: 处理数据错位问题
// isOrderChanged := false
isChanged := false
i := 0
for i < lenOld {
n, o := new.Problems[i], old.Problems[i]
if n.ID != o.ID {
log.Println(n, o)
log.Fatalln("LeetCode 的 Problems 数据出现错位。")
}
if n.IsAccepted == true && o.IsAccepted == false {
log.Printf("新完成 %d %s", n.ID, n.Title)
isChanged = true
}
i++
}
if !isChanged {
log.Println("~ 没有新完成习题 ~")
}
for i < lenNew {
log.Printf("出现新题: %d %s", new.Problems[i].ID, new.Problems[i].Title)
i++
}
}
func saveLC(lc *leetcode) {
raw, err := json.Marshal(lc)
if err != nil {
log.Fatal("无法把Leetcode数据转换成[]bytes: ", err)
}
if err = ioutil.WriteFile(lcFile, raw, 0666); err != nil {
log.Fatal("无法把Marshal后的lc保存到文件: ", err)
}
log.Println("最新的 LeetCode 记录已经保存。")
}
// data 保存API信息
type data struct {
Name string `json:"category_slug"`
User string `json:"user_name"`
ACEasy int `json:"ac_easy"`
ACMedium int `json:"ac_medium"`
ACHard int `json:"ac_hard"`
AC int `json:"num_solved"`
Problems []problemStatus `json:"stat_status_pairs"`
}
type problemStatus struct {
Status string `json:"status"`
State `json:"stat"`
IsFavor bool `json:"is_favor"`
IsPaid bool `json:"paid_only"`
Difficulty `json:"difficulty"`
}
// State 保存单个问题的解答状态
type State struct {
Title string `json:"question__title"`
TitleSlug string `json:"question__title_slug"`
IsNew bool `json:"is_new_question"`
ID int `json:"question_id"`
ACs int `json:"total_acs"`
Submitted int `json:"total_submitted"`
}
// Difficulty 问题的难度
type Difficulty struct {
Level int `json:"level"`
}