forked from dianhsu-official/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pull.go
135 lines (116 loc) · 2.92 KB
/
pull.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
package codeforces_client
import (
"errors"
"fmt"
"html"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/Arapak/sio-tool/util"
"github.com/fatih/color"
)
func findCode(body []byte) (string, error) {
reg := regexp.MustCompile(`<pre[\s\S]*?>([\s\S]*?)</pre>`)
tmp := reg.FindSubmatch(body)
if tmp == nil {
return "", errors.New("cannot find any code")
}
return html.UnescapeString(string(tmp[1])), nil
}
func findMessage(body []byte) (string, error) {
reg := regexp.MustCompile(`Codeforces.showMessage\("([^"]*)"\);\s*?Codeforces\.reformatTimes\(\);`)
tmp := reg.FindSubmatch(body)
if tmp != nil {
return string(tmp[1]), nil
}
return "", errors.New("cannot find any message")
}
const ErrorSkip = "exists, skip"
func (c *CodeforcesClient) PullCode(URL, path, ext string, rename bool) (filename string, err error) {
filename = path + ext
if rename {
i := 1
for _, err := os.Stat(filename); err == nil; _, err = os.Stat(filename) {
tmpPath := fmt.Sprintf("%v_%v%v", path, i, ext)
filename = tmpPath
i++
}
} else if _, err := os.Stat(filename); err == nil {
return "", errors.New(ErrorSkip)
}
body, err := util.GetBody(c.client, URL)
if err != nil {
return
}
message, err := findMessage(body)
if err == nil {
return "", errors.New(message)
}
code, err := findCode(body)
if err != nil {
return
}
err = os.MkdirAll(filepath.Dir(filename), os.ModePerm)
if err != nil {
return
}
err = os.WriteFile(filename, []byte(code), 0644)
return
}
func (c *CodeforcesClient) Pull(info Info, rootPath string, ac bool) (err error) {
color.Cyan("Pull " + info.Hint())
URL, err := info.MySubmissionURL(c.host)
if err != nil {
return
}
submissions, err := c.getSubmissions(URL, -1)
if err != nil {
return
}
var used []Submission
for _, submission := range submissions {
problemID := strings.ToLower(strings.Split(submission.name, " ")[0])
if info.ProblemID != "" && strings.ToLower(info.ProblemID) != problemID {
continue
}
if ac && !(strings.Contains(submission.status, "Accepted") || strings.Contains(submission.status, "Pretests passed")) {
continue
}
ext, ok := LangsExt[submission.lang]
if !ok {
continue
}
path := ""
if info.ProblemID == "" {
path = filepath.Join(rootPath, problemID, problemID)
} else {
path = filepath.Join(rootPath, problemID)
}
newInfo := info
newInfo.SubmissionID = fmt.Sprintf("%v", submission.id)
URL, err := newInfo.SubmissionURL(c.host)
if err != nil {
return err
}
filename, err := c.PullCode(
URL,
path,
"."+ext,
true,
)
if err == nil {
color.Green(fmt.Sprintf(`Saved %v`, filename))
used = append(used, submission)
} else {
color.Red(fmt.Sprintf(`Error %v: %v`, newInfo.Hint(), err.Error()))
}
}
if len(used) == 0 {
return errors.New("cannot find any code to save")
}
color.Cyan("These submissions' codes have been saved.")
maxline := 0
display(used, "", true, &maxline, false)
return nil
}