forked from xalanq/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
120 lines (111 loc) · 2.92 KB
/
parse.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
package client
import (
"fmt"
"html"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/fatih/color"
)
func findSample(body []byte) (input [][]byte, output [][]byte, err error) {
irg := regexp.MustCompile(`class="input"[\s\S]*?<pre>([\s\S]*?)</pre>`)
org := regexp.MustCompile(`class="output"[\s\S]*?<pre>([\s\S]*?)</pre>`)
a := irg.FindAllSubmatch(body, -1)
b := org.FindAllSubmatch(body, -1)
if a == nil || b == nil || len(a) != len(b) {
return nil, nil, fmt.Errorf("Cannot parse sample with input %v and output %v", len(a), len(b))
}
newline := regexp.MustCompile(`<[\s/br]+?>`)
filter := func(src []byte) []byte {
src = newline.ReplaceAll(src, []byte("\n"))
s := html.UnescapeString(string(src))
return []byte(strings.TrimSpace(s) + "\n")
}
for i := 0; i < len(a); i++ {
input = append(input, filter(a[i][1]))
output = append(output, filter(b[i][1]))
}
return
}
// ParseProblem parse problem to path
func (c *Client) ParseProblem(problemURL, path string) (samples int, err error) {
client := &http.Client{Jar: c.Jar}
resp, err := client.Get(problemURL)
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
}
input, output, err := findSample(body)
if err != nil {
return
}
for i := 0; i < len(input); i++ {
fileIn := filepath.Join(path, fmt.Sprintf("in%v.txt", i+1))
fileOut := filepath.Join(path, fmt.Sprintf("ans%v.txt", i+1))
e := ioutil.WriteFile(fileIn, input[i], 0644)
if e != nil {
color.Red(e.Error())
}
e = ioutil.WriteFile(fileOut, output[i], 0644)
if e != nil {
color.Red(e.Error())
}
}
return len(input), nil
}
// ParseContestProblem parse contest problem
func (c *Client) ParseContestProblem(contestID, problemID, path string) (samples int, err error) {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return
}
problemURL := fmt.Sprintf("https://codeforces.com/contest/%v/problem/%v", contestID, problemID)
samples, err = c.ParseProblem(problemURL, path)
if err != nil {
return
}
return
}
// ParseContest parse for contest
func (c *Client) ParseContest(contestID, rootPath string) (err error) {
problems, err := c.StatisContest(contestID)
if err != nil {
return
}
wg := sync.WaitGroup{}
wg.Add(len(problems))
mu := sync.Mutex{}
for t := range problems {
problem := problems[t]
go func() {
defer wg.Done()
mu.Lock()
fmt.Printf("Parsing %v %v\n", contestID, problem.ID)
mu.Unlock()
problemID := strings.ToLower(problem.ID)
path := filepath.Join(rootPath, problemID)
samples, err := c.ParseContestProblem(contestID, problem.ID, path)
mu.Lock()
if err != nil {
color.Red("Failed %v %v. Error: %v", contestID, problem.ID, err.Error())
} else {
color.Green("Parsed %v %v with %v samples", contestID, problemID, samples)
}
mu.Unlock()
}()
}
wg.Wait()
return
}