代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。