forked from xalanq/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
submit.go
79 lines (68 loc) · 1.82 KB
/
submit.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
package client
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"github.com/fatih/color"
)
func findErrorSource(body []byte) ([]byte, error) {
reg := regexp.MustCompile(`"error\sfor__source">(.*?)</span>`)
tmp := reg.FindSubmatch(body)
if tmp == nil {
return nil, errors.New("Cannot find error")
}
return tmp[1], nil
}
// SubmitContest submit problem in contest (and block util pending)
func (c *Client) SubmitContest(contestID, problemID, langID, source string) (err error) {
color.Cyan("Submit %v %v %v", contestID, problemID, Langs[langID])
submitURL := fmt.Sprintf("https://codeforces.com/contest/%v/submit", contestID)
client := &http.Client{Jar: c.Jar}
resp, err := client.Get(submitURL)
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
}
fmt.Printf("Current user: %v\n", c.Username)
csrf, err := findCsrf(body)
if err != nil {
return
}
resp, err = client.PostForm(fmt.Sprintf("%v?csrf=%v", submitURL, csrf), url.Values{
"csrf_token": {csrf},
"ftaa": {c.Ftaa},
"bfaa": {c.Bfaa},
"action": {"submitSolutionFormSubmitted"},
"submittedProblemIndex": {problemID},
"programTypeId": {langID},
"source": {source},
"tabSize": {"4"},
"_tta": {"594"},
"sourceCodeConfirmed": {"true"},
})
if err != nil {
return err
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
sourceError, err := findErrorSource(body)
if err == nil {
return errors.New(string(sourceError))
}
color.Green("Submitted")
return c.WatchSubmission(fmt.Sprintf("https://codeforces.com/contest/%v/my", contestID), 1, true)
}