forked from xalanq/cf-tool
-
Notifications
You must be signed in to change notification settings - Fork 7
/
submit.go
94 lines (80 loc) · 2.04 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package client
import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"regexp"
"github.com/fatih/color"
)
// SaveSubmission save it in session
type SaveSubmission struct {
ContestID string `json:"contest_id"`
SubmissionID string `json:"submission_id"`
}
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])
URL := ToGym(fmt.Sprintf("https://codeforces.com/contest/%v/submit", contestID), contestID)
resp, err := c.client.Get(URL)
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 = c.client.PostForm(fmt.Sprintf("%v?csrf=%v", URL, 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
}
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")
submissions, err := c.WatchSubmission(contestID, 1, true)
if err != nil {
return
}
c.LastSubmission = &SaveSubmission{
ContestID: contestID,
SubmissionID: submissions[0].ParseID(),
}
c.save()
return
}