forked from xalanq/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpull.go
139 lines (123 loc) · 3.27 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
136
137
138
139
package client
import (
"bytes"
"errors"
"fmt"
"html"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"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\S]*?)"\)`)
tmp := reg.FindAllSubmatch(body, -1)
if tmp != nil {
for _, s := range tmp {
if !bytes.Contains(s[1], []byte("The source code has been copied into the clipboard")) {
return string(s[1]), nil
}
}
}
return "", errors.New("Cannot find any message")
}
// PullCode pull problem's code to path
func (c *Client) PullCode(contestID, submissionID, 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 "", fmt.Errorf("Exists, skip")
}
URL := ToGym(fmt.Sprintf(c.Host+"/contest/%v/submission/%v", contestID, submissionID), contestID)
client := &http.Client{Jar: c.Jar.Copy()}
resp, err := client.Get(URL)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
message, err := findMessage(body)
if err == nil {
return "", fmt.Errorf("%v", message)
}
code, err := findCode(body)
if err != nil {
return
}
err = os.MkdirAll(filepath.Dir(filename), os.ModePerm)
if err != nil {
return
}
err = ioutil.WriteFile(filename, []byte(code), 0644)
return
}
// PullContest pull all latest codes or ac codes of contest's problem
func (c *Client) PullContest(contestID, problemID, rootPath string, ac bool) (err error) {
color.Cyan("Pull code from %v%v, ac: %v", contestID, problemID, ac)
URL := ToGym(fmt.Sprintf(c.Host+"/contest/%v/my", contestID), contestID)
submissions, err := c.getSubmissions(URL, -1)
if err != nil {
return
}
used := []Submission{}
for _, submission := range submissions {
pid := strings.ToLower(strings.Split(submission.name, " ")[0])
if problemID != "" && problemID != pid {
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 problemID == "" {
path = filepath.Join(rootPath, pid, pid)
} else {
path = filepath.Join(rootPath, strings.ToLower(problemID))
}
submissionID := fmt.Sprintf("%v", submission.id)
filename, err := c.PullCode(
contestID,
submissionID,
path,
"."+ext,
true,
)
if err == nil {
color.Green(fmt.Sprintf(`Saved %v`, filename))
used = append(used, submission)
} else {
color.Red(fmt.Sprintf(`Error in %v|%v: %v`, contestID, submissionID, 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
}