forked from xalanq/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statis.go
104 lines (95 loc) · 2.36 KB
/
statis.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
package client
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
"github.com/fatih/color"
)
// StatisInfo statis information
type StatisInfo struct {
ID string
Name string
IO string
Limit string
Passed string
State string
}
func findStatisBlock(body []byte) ([]byte, error) {
reg := regexp.MustCompile(`class="problems"[\s\S]+?</tr>([\s\S]+?)</table>`)
tmp := reg.FindSubmatch(body)
if tmp == nil {
return nil, errors.New("Cannot find any problem statis")
}
return tmp[1], nil
}
func findProblems(body []byte) ([]StatisInfo, error) {
reg := regexp.MustCompile(`<tr[\s\S]*?>`)
tmp := reg.FindAllIndex(body, -1)
if tmp == nil {
return nil, errors.New("Cannot find any problem")
}
ret := []StatisInfo{}
scr := regexp.MustCompile(`<script[\s\S]*?>[\s\S]*?</script>`)
cls := regexp.MustCompile(`class="(.+?)"`)
rep := regexp.MustCompile(`<[\s\S]+?>`)
ton := regexp.MustCompile(`<\s+`)
rmv := regexp.MustCompile(`<+`)
tmp = append(tmp, []int{len(body), 0})
for i := 1; i < len(tmp); i++ {
state := ""
if x := cls.FindSubmatch(body[tmp[i-1][0]:tmp[i-1][1]]); x != nil {
state = string(x[1])
}
b := scr.ReplaceAll(body[tmp[i-1][0]:tmp[i][0]], []byte{})
b = rep.ReplaceAll(b, []byte("<"))
b = ton.ReplaceAll(b, []byte("<"))
b = rmv.ReplaceAll(b, []byte("<"))
data := strings.Split(string(b), "<")
tot := []string{}
for j := 0; j < len(data); j++ {
s := strings.TrimSpace(data[j])
if s != "" {
tot = append(tot, s)
}
}
if len(tot) >= 5 {
tot[4] = strings.ReplaceAll(tot[4], "x", "")
tot[4] = strings.ReplaceAll(tot[4], " ", "")
if tot[4] == "" {
tot[4] = "0"
}
ret = append(ret, StatisInfo{
tot[0], tot[1], tot[2], tot[3],
tot[4], state,
})
}
}
return ret, nil
}
// StatisContest get contest problems statis
func (c *Client) StatisContest(contestID string) (problems []StatisInfo, err error) {
color.Cyan("Get statis in contest %v\n", contestID)
statisURL := fmt.Sprintf("https://codeforces.com/contest/%v", contestID)
client := &http.Client{Jar: c.Jar}
resp, err := client.Get(statisURL)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = checkLogin(c.Username, body)
if err != nil {
return
}
block, err := findStatisBlock(body)
if err != nil {
return
}
return findProblems(block)
}