forked from abeaumont/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-samples.py
More file actions
executable file
·66 lines (55 loc) · 2.25 KB
/
get-samples.py
File metadata and controls
executable file
·66 lines (55 loc) · 2.25 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
#!/usr/bin/env python3
# Get input/output samples for a contest
import asyncio
import os
import sys
import aiohttp
from bs4 import BeautifulSoup
class BadStatusException(Exception):
pass
class TaskException(Exception):
pass
async def process_task(client: aiohttp.ClientSession, name: str, url: str) -> None:
print('Getting samples for task {}'.format(name))
async with client.get(url) as response:
if response.status != 200:
raise TaskException(
'Invalid status code {}'.format(response.status))
soup = BeautifulSoup(await response.text(), 'html.parser')
sections = soup.find_all('section')
for section in sections:
h3 = section.h3.text
if h3.startswith('Sample'):
_, io, n = h3.split()
if io.lower().startswith('in'):
fname = '{}-{}.{}'.format(name, n, 'in')
else:
fname = '{}-{}.{}'.format(name, n, 'ans')
with open(fname, 'w') as f:
f.write(section.pre.text.replace('\r\n', '\n'))
print('Got samples for task {}'.format(name))
async def main(contest: str) -> None:
async with aiohttp.ClientSession() as client:
print('Getting samples for contest {}...'.format(contest))
prefix = 'https://' + contest + '.contest.atcoder.jp'
url = prefix + '/assignments'
async with client.get(url) as response:
if response.status != 200:
raise BadStatusException(
'Bad status for {}: {}'.format(url, response.status))
soup = BeautifulSoup(await response.text(), 'html.parser')
table = soup.find_all('tbody')[0]
tasks = []
for row in table.find_all('tr'):
task = row.td.a
name = task.text.lower()
url = prefix + task.get('href')
task = asyncio.ensure_future(process_task(client, name, url))
tasks.append(task)
await asyncio.gather(*tasks)
if __name__ == '__main__':
if len(sys.argv) > 1:
contest = sys.argv[1]
else:
contest = os.path.basename(os.path.abspath(os.curdir))
asyncio.get_event_loop().run_until_complete(main(contest))