-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcf_submit.py
220 lines (198 loc) · 8.2 KB
/
cf_submit.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import sys
import time
import json
import re
import requests
import colours
""" submissions """
def get_submission_data(handle):
req = requests.get("http://codeforces.com/api/user.status?handle={}&from=1&count=1".format(handle))
content = req.content.decode()
js = json.loads(content)
if "status" not in js or js["status"] != "OK":
print("Connection Error!")
res = js["result"][0]
# check if verdict exists (in queue if not)
if "verdict" not in res:
res["verdict"] = "IN QUEUE"
return res["id"], res["problem"], res["verdict"], res["passedTestCount"], res["timeConsumedMillis"], res["memoryConsumedBytes"]
""" look at last submission """
def peek(handle):
id_, probject, verdict, passedTests, timeCon, memCon = get_submission_data(handle)
problem = str(probject["contestId"])+str(probject["index"])
if verdict == "TESTING":
print("submission "+str(id_) + " to problem "+problem + ": "+verdict)
else:
if verdict != "OK":
print("submission "+str(id_) + " to problem "+problem + ": "+verdict + " on test "+str(1+passedTests) + "\n" + "Time: "+str(timeCon)+"ms \n" + "Memory: "+str(memCon/1024)+"Kb")
else:
print("submission "+str(id_) + " to problem " + problem + ": " + verdict+"! passed all " + str(passedTests) + " tests\n" + "Time: "+str(timeCon)+"ms \n" + "Memory: "+str(memCon/1024)+"Kb")
""" watch last submission """
def watch(handle):
spinner = { 0: "-", 1: "/", 2: "\\" }
count = 0
while True:
id_, probject, verdict, passedTests, timeCon, memCon = get_submission_data(handle)
if "contestId" not in probject:
probject["contestId"] = "guru"
problem = str(probject["contestId"])+str(probject["index"])
if verdict != "TESTING" and verdict != "IN QUEUE":
if verdict != "OK":
sys.stdout.write("\r" + "submission "+str(id_) + " to problem " + problem + ": " + verdict + " on test "+str(1+passedTests) + "\n" + "Time: "+str(timeCon)+"ms \n" + "Memory: "+str(memCon/1024)+"Kb")
else:
sys.stdout.write("\r" + "submission "+str(id_) + " to problem " + problem + ": " + verdict+"! passed all " + str(passedTests) + " tests\n" + "Time: "+str(timeCon)+"ms \n" + "Memory: "+str(memCon/1024)+"Kb")
sys.stdout.flush()
break
else:
sys.stdout.write("\r" + "submission "+str(id_) + " to problem "+problem + ": "+verdict + " ....... "+spinner[count] + (' '*7))
sys.stdout.flush()
count = (count+1) % 3
time.sleep(0.25)
print('\a')
""" submit problem """
def submit_problem(browser, contest, lang, source, guru):
""" get form """
submission = browser.get_form(class_="submit-form")
if submission is None:
print("Cannot find problem")
return False
""" submit form """
submission["sourceFile"] = source
langcode = None
if lang == "cpp":
# GNU G++20 11.2.0 (64 bit)
langcode = "73"
# GNU G++17 9.2.0 (64 bit)
# langcode = "61"
# GNU G++17 7.3.0
# langcode = "54"
# GNU G++14 6.2.0
# langcode = "50"
# GNU G++11 5.1.0
# langcode = "42"
#elif lang == "c++17":
# GNU G++17 7.3.0
# langcode = "54"
elif lang == "c":
# GNU GCC C11 5.1.0
langcode = "43"
elif lang == "d":
langcode = "28"
elif lang == "py":
# python 2.7
# langcode = "7"
# python 3.6
#langcode = "31"
#elif lang == "pypy":
# pypy 3.5
langcode = "41"
elif lang == "rb":
# Ruby 2.0.0p645
langcode = "8"
elif lang == "java":
# Java 1.8.0_112
# langcode = "36"
# Java 11.0.6
langcode = "60"
elif lang == "scala":
langcode = "20"
elif lang == "rs":
langcode = "49"
elif lang == "php":
langcode = "6"
elif lang == "go":
langcode = "32"
elif lang == "text":
langcode = "57"
elif lang == "kt":
langcode = "48"
else:
print("Unknown Language")
return False
submission["programTypeId"] = langcode
# check acmsguru
if guru != -1:
submission["submittedProblemCode"] = guru
browser.submit_form(submission)
""" check if good """
if (guru != -1 and browser.url[-7:] != "/status") or (guru == -1 and browser.url[-3:] != "/my"):
print("Failed to submit code")
print(" @ " + str(browser.url))
return False
print("Code submitted properly")
""" now get time """
countdown_timer = browser.parsed.find_all("span", class_="contest-state-regular countdown before-contest-"+contest+"-finish")
if len(countdown_timer) > 0:
print(colours.bold() + "TIME LEFT: " + str(countdown_timer[0].get_text(strip=True)) + colours.reset())
return True
""" submit problem """
def submit(browser, handle, contest, problem, lang, source, show, guru):
if guru:
print("Submitting to acmsguru " + problem + " as " + handle)
else:
print("Submitting to problem " + contest + problem.upper() + " as " + handle)
pid = -1
if guru:
browser.open("http://codeforces.com/problemsets/acmsguru/submit")
pid = problem
elif len(contest) >= 6:
browser.open("http://codeforces.com/gym/" + contest + "/submit/" + problem.upper())
else:
browser.open("http://codeforces.com/contest/" + contest + "/submit/" + problem.upper())
""" show submission """
if submit_problem(browser, contest, lang, source, pid) and show:
watch(handle)
""" submit, possibly len(args) > 1 """
def submit_files(browser, defaulthandle, defaultcontest, defaultprob, defext, defaultlang, args, show, guru):
""" if len == 0, query for file """
if len(args) == 0:
args.append(raw_input("File to submit: "))
for source in args:
""" split file name """
info = source.split('.')
""" single filename """
if source.find('.') == -1:
info.append(defext)
source += "." + defext
""" check language """
if defaultlang is not None:
info[-1] = defaultlang
""" submit problem """
if defaultprob is not None:
if len(defaultprob) == 1:
""" letter only """
submit(browser, defaulthandle, defaultcontest, defaultprob, info[-1], source, show, guru)
elif len(defaultprob) == 2:
""" letter + number (?) """
submit(browser, defaulthandle, defaultcontest, defaultprob, info[-1], source, show, guru)
else:
""" parse string """
splitted = re.split('(\D+)', defaultprob)
if len(splitted) == 3 and len(splitted[1]) == 1 and len(splitted[2]) == 0:
""" probably a good string """
submit(browser, defaulthandle, splitted[0], splitted[1], info[-1], source, show, guru)
else:
print("cannot understand the problem specified")
elif len(info) == 2:
if guru:
""" ACMSGURU """
submit(browser, defaulthandle, defaultcontest, info[0], info[1], source, show, guru)
else:
""" CODEFORCES """
""" try to parse info[0] """
if info[0][:2].lower() == "cf":
""" remove the cf """
info[0] = info[0][2:]
if len(info[0]) == 1:
""" only the letter, use default contest """
submit(browser, defaulthandle, defaultcontest, info[0], info[1], source, show, guru)
else:
""" contest is included, so parse """
splitted = re.split('(\D+)', info[0])
if len(splitted) == 3 and len(splitted[1]) == 1 and len(splitted[2]) == 0:
""" probably good string ? """
submit(browser, defaulthandle, splitted[0], splitted[1], info[1], source, show, guru)
else:
print("cannot parse filename, specify problem with -p or --prob")
else:
print("cannot parse filename, specify problem with -p or --prob")