-
Notifications
You must be signed in to change notification settings - Fork 0
/
atcoder.py
executable file
·152 lines (117 loc) · 4.08 KB
/
atcoder.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
#!/usr/bin/env python
# coding: utf-8
import argparse
import configparser
import os
import re
import shutil
import requests
def login(session, username: str, password: str):
login_url = 'https://atcoder.jp/login'
res = session.get(login_url)
csrf_token, = re.search(
r'var csrfToken = "(.+?)"',
res.text,
flags=(re.MULTILINE | re.DOTALL)
).groups()
session.post(login_url, data={
'csrf_token': csrf_token,
'username': username,
'password': password
}).raise_for_status()
return csrf_token
def get_samples(task_page: str) -> tuple:
hooks = (
('入力例', '出力例'),
('Sample Input', 'Sample Output')
)
ret = None
for i, o in hooks:
if re.search(i, task_page, flags=re.MULTILINE):
pat = r'<h3.*?>{}\s*?[1-9]</h3>.*?<pre.*?>(.*?)</pre>'
ret = tuple(zip(
map(lambda x: tuple(x.strip().splitlines()), re.findall(
pat.format(i), task_page, flags=(re.MULTILINE | re.DOTALL)
)),
map(lambda x: x.replace('\r\n', '\n').strip(), re.findall(
pat.format(o), task_page, flags=(re.MULTILINE | re.DOTALL)
))
))
break
return ret
def modify(name: str, test: str, samples: tuple) -> str:
head, indent, tail = re.search(
r'(.*?)^(\s*?)# SAMPLES\s*?$(.*)',
test,
flags=(re.MULTILINE | re.DOTALL)
).groups()
ret = re.sub(
r'^(import )_( as task$)',
r'\1{}\2'.format(name),
head,
flags=re.MULTILINE
)
ret += ',\n'.join(map(lambda x: indent+str(x), samples))
ret += tail
return ret
def snake(text: str):
return text.replace('-', '_')
def main(code: str, alpha: str, url: str = '',
username: str = '', password: str = '', logout: bool = False,
wipdir: str = ''):
session = requests.session()
session.cookies.clear()
csrf_token = ''
if username and password:
csrf_token = login(session, username, password)
if not url:
url = 'https://atcoder.jp/contests/{c}/tasks/{cs}_{a}'.format(
c=code, cs=snake(code), a=alpha
)
res = session.get(url)
if res.status_code != 200:
print(res.status_code)
return
samples = get_samples(res.text)
if logout:
session.post('https://atcoder.jp/logout', data={
'csrf_token': csrf_token
}).raise_for_status()
session.cookies.clear()
if not samples:
print('No test cases found. Just copy templates? (Y/n)')
if input().lower() != 'y':
return
samples = tuple()
with open('test_.py', 'r') as temp:
test = temp.read()
name = '{}_{}'.format(snake(code), alpha)
new_test = modify(name, test, samples)
with open(os.path.join(wipdir, 'test_{}.py'.format(name)), 'w') as o:
o.write(new_test)
shutil.copyfile('_.py', os.path.join(wipdir, '{}.py'.format(name)))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('code', type=str)
parser.add_argument('alpha', type=str)
parser.add_argument('url', nargs='?', type=str, default='')
args = parser.parse_args()
code, alpha, url = args.code, args.alpha, args.url
config_file = 'atcoder.ini'
if os.path.exists(config_file):
config = configparser.ConfigParser()
config.read(config_file)
login_sec = config['login']
username = login_sec.get('username')
password = login_sec.get('password')
logout = login_sec.getboolean('logout', fallback=False)
workspace_sec = config['workspace']
wipdir = workspace_sec.get('wipdir', fallback='')
filename = '{}_{}.py'.format(snake(code), alpha)
if os.path.exists(os.path.join(wipdir, filename)) or \
os.path.exists(os.path.join(wipdir, 'test_{}'.format(filename))):
print('File exists. Overwrite? (y/N)')
if input().lower() != 'y':
exit()
main(code, alpha, url, username, password, logout, wipdir)
print('Done')