forked from xalanq/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pull.go
117 lines (104 loc) · 2.77 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
package client
import (
"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 code")
}
return html.UnescapeString(string(tmp[1])), nil
}
// PullCode pull problem's code to path
func (c *Client) PullCode(codeURL, path, ext string) (filename string, err error) {
client := &http.Client{Jar: c.Jar}
resp, err := client.Get(codeURL)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
code, err := findCode(body)
if err != nil {
return
}
filename = path + ext
i := 1
for _, err := os.Stat(filename); err == nil; _, err = os.Stat(filename) {
tmpPath := fmt.Sprintf("%v%v%v", path, i, ext)
fmt.Printf("%v exists. Rename to %v\n", filepath.Base(filename), filepath.Base(tmpPath))
filename = tmpPath
i++
}
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, accepted: %v", contestID, problemID, ac)
submissions, err := c.getSubmissions(fmt.Sprintf("https://codeforces.com/contest/%v/my", contestID), -1)
if err != nil {
return err
}
saved := map[string](map[string]bool){}
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
}
if _, ok = saved[pid]; !ok {
saved[pid] = map[string]bool{}
}
if _, ok = saved[pid][ext]; ok {
continue
}
path := ""
if problemID == "" {
path = filepath.Join(rootPath, pid, pid)
} else {
path = filepath.Join(rootPath, strings.ToLower(problemID))
}
filename, err := c.PullCode(
fmt.Sprintf("https://codeforces.com/contest/%v/submission/%v", contestID, submission.id),
path,
"."+ext,
)
if err == nil {
saved[pid][ext] = true
color.Green(fmt.Sprintf(`Downloaded code of %v %v into %v`, contestID, pid, filepath.Base(filename)))
used = append(used, submission)
}
}
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
}