This repository was archived by the owner on Mar 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatcoder_cli_app.py
More file actions
381 lines (293 loc) · 12.8 KB
/
atcoder_cli_app.py
File metadata and controls
381 lines (293 loc) · 12.8 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import os
import time
import configparser
import requests
import subprocess
import winsound
from bs4 import BeautifulSoup
from pathlib import Path
import colorama
from colorama import Fore, Back, Style
# 問題点
# ARC併設のABCに対応できてない(途中からarcに変わるので)
# 提出した後のフィードバック待ちは非同期にしたいなあ
# けど非同期にすると提出の結果が分かった瞬間に、結果表示が入力待ちとかに割り込む形になっちゃうのかなあ……
# だとすると結構厄介?
class AtCoderCLIApp:
session = requests.session()
user_name = None
password = None
default_language = None
last_contest_name = None
ac_wav = 'ac.wav'
wa_wav = 'wa.wav'
# C++, dmd, ldc, Python3, Pypy3 での提出に対応
language_ids = {'cpp': '3003',
'dmd': '3009',
'ldc': '3010',
'python': '3023',
'pypy': '3510'}
language_ext = {'cpp': '.cpp',
'dmd': '.d',
'ldc': '.d',
'python': '.py',
'pypy': '.py'}
def __init__(self):
if not Path('./atcoder_cli_app.ini').exists():
raise FileNotFoundError('iniファイルが存在しません')
config = configparser.ConfigParser()
config.read('atcoder_cli_app.ini')
self.user_name = config['General']['user_name']
self.password = config['General']['password']
self.default_language = config['General']['default_language']
if 'last_contest_name' in config['General']:
self.last_contest_name = config['General']['last_contest_name']
def start(self):
while True:
try:
command = input('$ ').split()
except KeyboardInterrupt:
# Ctrl+C でもErrorを出さずに抜けられるように
print()
return
# 空文字列のとき
if not command:
continue
if command[0] in {'fetch', 'f'}:
if len(command) < 2:
print('コンテスト名を引数に与えてね')
continue
contest_name = command[1]
self.fetch_test_cases(contest_name)
elif command[0] in {'make', 'm'}:
if len(command) < 2:
print('タスク番号を引数に与えてね')
continue
task_number = command[1]
self.make_test_case(task_number)
elif command[0] in {'test', 't'}:
if len(command) < 2:
print('タスク番号を引数に与えてね')
continue
task_number = command[1]
self.run_test(task_number)
elif command[0] in {'submit', 's'}:
if len(command) < 2:
print('タスク番号を引数に与えてね')
continue
task_number = command[1]
self.submit(task_number)
elif command[0] == 'set':
if len(command) < 2:
print('コンテスト名を引数に与えてね')
continue
self.set_contest_name(command[1])
elif command[0] == 'exit':
return
else:
continue
def login(self):
login_url = 'https://practice.contest.atcoder.jp/login'
login_info = {
'name': self.user_name,
'password': self.password
}
res = self.session.post(login_url, data=login_info)
if res.url == 'https://practice.contest.atcoder.jp/login':
raise LoginError('ログインに失敗しました')
def submit(self, task_number, language=''):
if self.last_contest_name is None:
print('先にコンテスト名を指定してfetchしてください')
return
contest_name = self.last_contest_name
contest_url = 'https://' + contest_name + '.contest.atcoder.jp'
self.login()
submit_url = contest_url + '/submit'
res = self.session.get(submit_url)
soup = BeautifulSoup(res.text, 'lxml')
selector = soup.find('select', attrs={'id': 'submit-task-selector'})
task_ids = [x.get('value') for x in selector.find_all('option')]
__session = soup.find('input').get('value')
selected_task_id = task_ids[ord(task_number) - ord('a')]
submit_info = {'__session': __session,
'task_id': selected_task_id
}
params = {'task_id': selected_task_id}
if not language:
language = self.default_language
for task_id in task_ids:
if task_id == selected_task_id:
submit_info['language_id_' +
task_id] = self.language_ids[language]
else:
submit_info['language_id_' + task_id] = '3003' # C++(何でもいいぽい?)
p = Path('./' + task_number + self.language_ext[language])
if not p.exists():
print(p.name + 'が存在しません')
return
source_code = p.read_text(encoding='utf-8')
submit_info['source_code'] = source_code
res = self.session.post(submit_url, params=params, data=submit_info)
submitted_url = contest_url + '/submissions/me'
if not res.url.startswith(submitted_url):
print('提出に失敗したかも?')
def set_contest_name(self, contest_name):
self.last_contest_name = contest_name
# ini ファイルにも保存
config = configparser.ConfigParser()
config.read('atcoder_cli_app.ini')
config['General']['last_contest_name'] = contest_name
with open('atcoder_cli_app.ini', 'w') as config_file:
config.write(config_file)
def exists_contest_page(self, contest_name) -> bool:
contest_url = 'https://' + contest_name + '.contest.atcoder.jp'
res = self.session.get(contest_url)
soup = BeautifulSoup(res.text, 'lxml')
is_404 = (soup.find('title').string[:3] == '404')
return (not is_404)
def fetch_test_cases(self, contest_name):
# コンテスト名だけじゃなくて、URLコピペでも指定できるようにしたいよね
if not self.exists_contest_page(contest_name):
print(contest_name + 'というコンテストが存在しません')
return
try:
os.mkdir(contest_name)
except FileExistsError:
print('既に同じ名前のファイルが存在します')
print('そのまま上書きします')
self.set_contest_name(contest_name)
self.login()
contest_url = 'https://' + contest_name + '.contest.atcoder.jp'
for task_number in 'abcdefgh':
# https://code-thanks-festival-2018.contest.atcoder.jp/tasks/code_thanks_festival_2018_a
# などを見ると、なぜか-は_に置き換えなければいけないようだ
modified_contest_name = contest_name.replace('-', '_')
task_url = contest_url + '/tasks/' + modified_contest_name + '_' + task_number
res = self.session.get(task_url)
soup = BeautifulSoup(res.text, 'lxml')
# 「提出する」ボタンがあるかどうかで、その問題ページが存在するかを判定している
exists_submit_button = soup.find(
'a', attrs={'class': 'btn btn-primary btn-large'}) is not None
if not exists_submit_button:
break
# "Problem Statement" があるかどうかで、英語問題文が用意されているか判定する
exists_english_statement = (
res.text.find('Problem Statement') >= 0)
# サンプルはpreタグで囲われているのでそれを取ってくる
samples = soup.find_all('pre')
N = len(samples)
if exists_english_statement:
# 英語の部分が重複するので捨てる
samples = samples[:N//2]
# 入出力の記法に関する部分を除く(varタグで入れ子になっているのは消す)
while samples and samples[0].string is None:
samples = samples[1:]
# print(samples, sep='\n')
for i, sample in enumerate(samples):
file_path = contest_name + '/sample_' + task_number
file_path += '_' + str(i // 2)
file_path += '.in' if i % 2 == 0 else '.out'
lines = sample.string.splitlines()
# print(lines)
with open(file_path, mode='w') as f:
for line in lines:
f.write(line.strip() + '\n')
print('Fetched sample cases in ' + task_number.upper())
# print(samples)
time.sleep(1)
print('サンプルケースのダウンロードが完了しました!')
def make_test_case(self, task_number):
if self.last_contest_name is None:
print('コンテスト名が指定されていません')
return
# 一応これで再起動問題はクリアしてるけど歯抜けになる可能性があるな
# 0, 1, 3とか、まあそこは大きな問題では無いけど……
p = Path('./' + self.last_contest_name)
try:
max_custom_number = max(
int(x.stem[9:]) for x in p.iterdir() if x.name.find('custom') >= 0)
except ValueError:
max_custom_number = -1
next_custom_number = max_custom_number + 1
dir_path = self.last_contest_name
print('Input')
print('-'*20)
input_data = ''
while True:
line = input()
if not line:
break
input_data += line + '\n'
print('Output')
print('-'*20)
output_data = ''
while True:
line = input()
if not line:
break
output_data += line + '\n'
ans = input('これでいい? [y/n] ')
if ans in {'n', 'N', 'no', 'No', 'NO'}:
return
file_path = dir_path + '/custom_' + task_number
file_path += '_' + str(next_custom_number)
file_path += '.in'
with open(file_path, mode='w') as f:
f.write(input_data)
print('Made ' + file_path)
file_path = dir_path + '/custom_' + task_number
file_path += '_' + str(next_custom_number)
file_path += '.out'
with open(file_path, mode='w') as f:
f.write(output_data)
print('Made ' + file_path)
print('Done!')
def run_test(self, task_number):
if self.last_contest_name is None:
print('先にコンテスト名を指定してfetchしてください')
return
contest_name = self.last_contest_name
p = Path('./' + contest_name)
sample_ins = [x for x in p.iterdir() if x.name[7] ==
task_number and x.suffix == '.in']
accepted = True
for sample_in in sample_ins:
with sample_in.open() as f:
try:
proc = subprocess.run(
[task_number + ".exe"], stdin=f, stdout=subprocess.PIPE)
except FileNotFoundError:
print('実行ファイルが存在しないよ、コンパイルし忘れてる?')
return
actual = [line.strip()
for line in proc.stdout.decode('utf-8').splitlines()]
# print(actual)
sample_out = Path('./' + contest_name + '/' +
sample_in.stem + '.out')
expect = [line.strip()
for line in sample_out.read_text().splitlines()]
print('-'*5 + sample_in.name + '-'*5)
print('expect:')
print(*expect, sep='\n')
print('actual:')
print(*actual, sep='\n')
# print(actual)
# print(expect)
ok = actual == expect
print(Fore.GREEN + 'OK' if ok else Fore.RED + 'NG')
print()
accepted = accepted and ok
# 非同期で効果音を鳴らす
sound_effect_path = './se/' + \
(self.ac_wav if accepted else self.wa_wav)
winsound.PlaySound(sound_effect_path,
winsound.SND_FILENAME | winsound.SND_ASYNC)
print(Back.GREEN + 'Accepted!!!' if accepted else Back.RED + 'Wrong Answer')
print()
class LoginError(Exception):
def __init__(self, message):
self.message = message
if __name__ == '__main__':
colorama.init(autoreset=True)
acca = AtCoderCLIApp()
acca.start()