-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubmit-py3.py
More file actions
executable file
·306 lines (265 loc) · 8.03 KB
/
submit-py3.py
File metadata and controls
executable file
·306 lines (265 loc) · 8.03 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import sys
import re
import json
import time
import os
from getpass import getpass
#'language_id': ext array
LANGUAGES = {
0: ['.c'],
1: ['.cpp', '.cc', '.cxx'],
2: ['.p','.pas'],
3: ['.java'],
4: ['.rb'],
5: ['.sh'],
6: ['.py'],
7: ['.php'],
8: ['.pl'],
9: ['.cs'],
10: ['.m'],
12: ['.go'],
13: ['.f95','.for'],
14: ['.scm'],
15: ['.scala'],
16: ['.lua'],
17: ['.js'],
18: ['.coffee'],
19: ['.ada'],
20: ['.vb'],
21: ['.awk'],
22: ['.ml'],
23: ['.bf'],
24: ['.ws'],
25: ['.groovy'],
26: ['.tcl'],
27: ['.asm'],
29: ['.d'],
33: ['.clj'],
37: ['.fs'],
39: ['.fal'],
41: ['.pike'],
43: ['.sed'],
44: ['.rs'],
46: ['.boo'],
47: ['.i'],
48: ['.bc'],
53: ['.n'],
54: ['.cobra'],
55: ['.nim'],
58: ['.txt'],
61: ['.io']
};
## "language_id": [ { "version_name": "language_id" }]
VERSIONS = {
0: {
"clang": 59
},
1: {
"clang": 60,
"clang11": 66,
"clang14": 67,
"11": 49, "c11": 49, "c++11": 49, "cpp11": 49,
"14": 88, "c14": 88, "c++14": 88, "cpp14": 88
},
6: {
"3": 28, "py3": 28, "python3": 28,
"pypy": 32,
"pypy3": 73
}
}
LOGIN_URL = 'https://www.acmicpc.net/signin'
SUBMIT_URL = 'https://www.acmicpc.net/submit/{{problem_id}}'
STATUS_URL = 'https://www.acmicpc.net/status/ajax';
CODE_OPEN_URL = 'https://www.acmicpc.net/setting/solution';
HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7',
'Referer': 'https://www.acmicpc.net',
'Origin': 'https://www.acmicpc.net'
}
s = requests.session()
read = lambda: sys.stdin.readline()
write = lambda x: print(x, end='', flush=True)
def get_language(filename, version):
dummy, extension = os.path.splitext(filename)
language = -1
for key, value in LANGUAGES.items():
if extension in value:
language = int(key)
if language != -1 and version is not None and language in VERSIONS:
for key, value in VERSIONS[language].items():
if version == key:
language = int(value)
return language
def get_source(filename):
if not os.path.exists(filename):
return (False, '')
fp = open(filename, 'r')
source = fp.read()
fp.close()
return (True, source)
def wait_solution(solution_id):
data = {
'solution_id': solution_id
}
while (True):
solution_headers = {
'X-Requested-With': 'XMLHttpRequest'
}
solution_headers.update(HEADERS)
res = s.post(STATUS_URL, headers=solution_headers, data=data)
result = json.loads(res.text)
print(result['result_name'])
submit_result = int(result['result'])
if submit_result > 3:
if submit_result == 4:
print(u'메모리: %sKB, 시간: %sMS' % (result['memory'], result['time']))
break
time.sleep(1)
def get_submit_url(problem_id):
return SUBMIT_URL.replace('{{problem_id}}', str(problem_id))
def get_submit_failure_reason(text):
reasons = [
u'10초에 한 번 제출할 수 있습니다',
u'소스 코드가 너무 짧아요',
u'없는 언어 입니다',
u'이메일 인증을 받지 못해서 제출할 수 없습니다',
u'없는 문제 입니다',
u'제출할 수 없는 문제 입니다',
u'소스 코드가 너무 길어요'
];
for index, reason in enumerate(reasons):
if reason in text:
return (-index, reasons[index])
return (-100, '알 수 없는 에러가 발생했습니다')
def get_code_open():
code_open = ''
res = s.get(CODE_OPEN_URL, headers=HEADERS)
pos = res.text.find('input type = "radio" name = "code_open" id="code_open_open" value="open" checked')
if pos != -1:
code_open = 'open'
pos = res.text.find('input type = "radio" name = "code_open" id="code_open_close" value="close" checked')
if pos != -1:
code_open = 'close'
pos = res.text.find('input type = "radio" name = "code_open" id="code_open_accepted" value="onlyaccepted" checked')
if pos != -1:
code_open = 'onlyaccepted'
return code_open
def submit(problem_id, source, language):
data = {
'problem_id': problem_id,
'source': source,
'language': language,
'code_open': get_code_open(),
'csrf_key': get_csrf_token(problem_id)
}
url = get_submit_url(problem_id)
res = s.post(url, headers=HEADERS, data=data)
# get solution_id
try:
m = re.search('watch_solution\([0-9]+\)', res.text)
m2 = re.search('[0-9]+', m.group(0))
solution_id = int(m2.group(0))
return (solution_id, '')
except:
return get_submit_failure_reason(res.text)
def get_csrf_token(problem_id):
url = get_submit_url(problem_id)
res = s.get(url, headers=HEADERS)
pos = res.text.find('name="csrf_key"')
csrf_token = res.text[pos+23:pos+23+32]
return csrf_token
def login(username, password):
data = {
'login_user_id': username,
'login_password': password
}
res = s.post(LOGIN_URL, headers=HEADERS, data=data)
if u'<title>로그인</title>' not in res.text:
return True
else:
return False
def validation_check(username, password):
if len(username) == 0:
print(u'아이디를 입력해 주세요')
return False
if len(password) == 0:
print(u'비밀번호를 입력해 주세요')
return False
return True
def get_problem_id_from_filename(filename):
problem_id, extension = os.path.splitext(filename)
if problem_id.isdigit():
return int(problem_id)
else:
return -1
def close_session():
s.close()
def main():
# parse arguments vector
argv = sys.argv[1:]
if len(argv) < 1 or len(argv) > 3:
print(u'Usage: python submit.py filename [version]')
print(u'Usage: python submit.py problem_id filename [version]')
return close_session()
# get filename, problem_id from arguments
if len(argv) == 1:
filename = argv[0]
problem_id = get_problem_id_from_filename(filename)
version = None
if problem_id == -1:
print(u'파일 이름은 문제번호.확장자 형식이 되어야 합니다')
return close_session()
else:
try:
problem_id = int(argv[0])
filename = argv[1]
if len(argv) > 2:
version = argv[2]
else:
version = None
except:
filename = argv[0]
problem_id = get_problem_id_from_filename(filename)
if len(argv) > 1:
version = argv[1]
else:
version = None
# get source from filename
success, source = get_source(filename)
if not success:
print(u'%s는 없는 파일입니다' % (filename))
return close_session()
# get language_id
language = get_language(filename, version)
if language == -1:
print(u'무슨 언어인지 모르겠어요. 확장자를 확인해 주세요')
return close_session()
# get username, password
try:
write(u'아이디: ')
username = read().strip()
write(u'비밀번호: ')
password = getpass('')
except:
print('')
return close_session()
# validation check
if not validation_check(username, password):
return close_session()
# login
if not login(username, password):
print(u'아이디 또는 비밀번호가 일치하지 않습니다')
return close_session()
# submit source code
solution_id, msg = submit(problem_id, source, language)
if solution_id <= 0 :
print(msg)
return close_session()
# wait result
wait_solution(solution_id)
return close_session()
# start
main()