-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubmit.go
179 lines (160 loc) · 4.2 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"golang.org/x/net/html"
)
type probCode struct {
contest int
task string
}
// guess not defined args
func (cf *cf) guessArgs(file *string, prob probCode, lang *int) error {
f, l := *file, *lang
needExt := len(f) < 3
if l == 0 && needExt {
// search over disk
f += "."
var match []string
for k := range cf.config {
if st, err := os.Stat(f + k); err == nil && !st.IsDir() {
match = append(match, k)
}
}
if len(match) == 0 {
return fmt.Errorf("submit: could not found solution file for problem %q", prob.task)
}
if len(match) > 1 {
buf := bytes.NewBufferString("submit: more than one file looks like solution for problem ")
fmt.Fprintf(buf, "%v, candidates (with lang IDs):", prob.task)
for _, k := range match {
fmt.Fprintf(buf, "\n%s%v (%s)", f, k, cf.config[k])
}
return errors.New(buf.String() + "\n\nremove all but one of those files, or\n" +
" remove all but one of those extensions in conf, or\n specify lang at command line")
}
*file = f + match[0]
l, err := strconv.Atoi(cf.config[match[0]])
*lang = l
return err
}
if l != 0 && needExt {
s := strconv.Itoa(l)
for k, v := range cf.config {
if v == s {
*file += "." + k
return nil
}
}
return fmt.Errorf("submit: extension must be specified for lang %q", l)
}
if l == 0 {
ext := filepath.Ext(f)
if len(ext) < 2 {
return errors.New("submit: lang must be specified")
}
s, ok := cf.config[ext[1:]]
if !ok {
return fmt.Errorf("submit: unknown file extension %q, (you may add it in conf)", ext)
}
l, err := strconv.Atoi(s)
if err != nil {
return err
}
*lang = l
}
return nil
}
func (cf *cf) submit(file string, prob probCode, lang int) error {
if _, ok := cf.config["user"]; !ok {
return errors.New("submit: user must be configured (and logged in)")
}
err := cf.guessArgs(&file, prob, &lang)
if err != nil {
return err
}
err = cf.initClient()
if err != nil {
return err
}
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
submitURL := contestURL(prob)
resp, err := cf.client.Get(submitURL)
if err != nil {
return err
}
defer resp.Body.Close()
if !strings.Contains(resp.Request.URL.String(), "/problem/") {
return errors.New("submit: probably not logged in or login expired; was redirected to: " + resp.Request.URL.String())
}
tree, err := html.Parse(resp.Body)
if err != nil {
return err
}
if tree == nil {
return errors.New("submit: could not parse html response for /problem")
}
submitForm := selSubmit.MatchFirst(tree)
if submitForm == nil {
return errors.New("submit: could not match submit form\nnote that you could not submit during system testing (right after the contest)")
}
action := formAction(submitForm)
fields := form(selInput.MatchAll(submitForm))
fields.Del("sourceFile")
fields.Set("submittedProblemIndex", prob.task)
fields.Set("programTypeId", fmt.Sprintf("%d", lang))
buf := &bytes.Buffer{}
w := multipart.NewWriter(buf)
for k, vs := range fields {
for _, v := range vs {
w.WriteField(k, v)
}
}
fw, err := w.CreateFormFile("sourceFile", filepath.Base(file))
if err != nil {
return err
}
if _, err = io.Copy(fw, f); err != nil {
return err
}
w.Close()
// http://codeforces.com/contest/1436/problem/B?csrf_token=1d130cd435e8bf4e59f8888974641a3f
// action starts at '?'
req, err := http.NewRequest("POST", submitURL+action, buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", w.FormDataContentType())
resp, err = cf.client.Do(req)
if err != nil {
return err
}
resp.Body.Close()
if resp.StatusCode == 200 || resp.StatusCode == 100 {
log.Println("code submitted properly, you could safely kill this program from now on (by ^C)")
} else {
log.Printf("code submitted, response HTTP status: %d, you could safely kill this program from now on (by ^C)\n", resp.StatusCode)
}
cf.status(prob)
return nil
}
func contestURL(prob probCode) string {
contestOrGym := "contest"
if prob.contest >= 100000 { // just guessing here
contestOrGym = "gym"
}
return fmt.Sprintf(CFURL+"/%s/%d/problem/%s", contestOrGym, prob.contest, prob.task)
}