-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeecrowd_challenges_extractor.py
93 lines (61 loc) · 2.13 KB
/
beecrowd_challenges_extractor.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
from os import makedirs, path
from bs4 import BeautifulSoup
from httpx import get
URL_BASE = "https://www.beecrowd.com.br/repository/UOJ_"
class SelectorsShema:
title: str = "title"
problem_description: str = ".description"
_input: str = ".input"
output: str = ".output"
def get_html(challenge: int):
req = get(f"{URL_BASE}{challenge}.html")
if req.status_code != 200:
return None
return req.text
def html_to_bs4(html):
return BeautifulSoup(html, "html.parser")
def challenge_group_folder(challenge_group: str):
if not path.exists(challenge_group):
makedirs(challenge_group)
def save_problem(challenge_group: str, challenge: int):
html = get_html(challenge)
if html is None:
return False
bs = html_to_bs4(html)
filename = bs.select_one(SelectorsShema.title).text.split("-")[0].strip()
title = bs.select_one(SelectorsShema.title).text.strip()
description = bs.select_one(SelectorsShema.problem_description).text.strip()
_input = bs.select_one(SelectorsShema._input).text.strip()
output = bs.select_one(SelectorsShema.output).text.strip()
input_examples = "\n\n".join([el.text.strip() for el in bs.select(".division")])
output_examples = "\n\n".join(
[el.text.strip() for el in bs.select(".division + td")]
)
challenge_group_folder(challenge_group)
with open(f"{challenge_group}/{filename}.html", "w", encoding="utf-8") as file:
file.write(html)
with open(f"{challenge_group}/{filename}.txt", "w", encoding="utf-8") as file:
file.write(
f"""
Desafio: {title}
Descrição:
{description}
Entrada:
{_input}
Saida:
{output}
Exemplo de Entrada:
{input_examples}
Exemplo de Saída:
{output_examples}
"""
)
print(f"Desafio {title}!")
return True
def get_challenges(challenge_group: str, initial_challenge: int = 1000):
loop = True
challenge = initial_challenge
while loop:
loop = save_problem(challenge_group, challenge)
challenge += 1
get_challenges("DOWNLOAD", 1000)