-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.py
202 lines (170 loc) · 8.01 KB
/
scraper.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
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
import csv
import os
from pathlib import Path
import requests
from dotenv import load_dotenv
from playwright.sync_api import sync_playwright
load_dotenv()
playwright = sync_playwright().start()
browser = playwright.chromium.launch()
page = browser.new_page()
page.goto("https://cses.fi/problemset/")
h2_elements = page.query_selector_all("h2")
categories = [h2.inner_text() for h2 in h2_elements[1:]]
tasks_dir = Path(__file__).parent / "tasks"
tasks_dir.mkdir(exist_ok=True)
with open("tasks.csv", "w") as f:
writer = csv.DictWriter(f, fieldnames=["path", "cses_id", "status"])
writer.writeheader()
for i, category in enumerate(categories, start=1):
category_slug = f'{str(i).rjust(2, "0")}-{category.replace(" ", "-").lower()}'
category_dir = tasks_dir / category_slug
category_dir.mkdir(exist_ok=True)
a_elements = page.query_selector_all(f'h2:has-text("{category}") + ul a')
ids = [
a.get_attribute("href").removeprefix("/problemset/task/")
for a in a_elements
]
titles = [a.inner_text() for a in a_elements]
for j, (_id, title) in enumerate(zip(ids, titles), start=1):
task_slug = f'{str(j).rjust(2, "0")}-{title.replace("ü", "u").replace("'", "").replace(" ", "-").lower()}'
task_dir = category_dir / task_slug
task_dir.mkdir(exist_ok=True)
task_file = task_dir / "task.md"
task_file.touch(exist_ok=True)
with task_file.open("w") as f:
f.write(f"# {title.replace("ü", "u")} \n\n")
solution_file = task_dir / "solution.py"
solution_file.touch(exist_ok=True)
with solution_file.open("w") as f:
f.write(f"# {title.replace("ü", "u").lower()} \n\n")
writer.writerow(
{
"path": f"{category_slug}/{task_slug}",
"cses_id": _id,
"status": "unsolved",
}
)
task_base_url = "https://cses.fi/problemset/task"
tasks_csv = Path(__file__).parent / "tasks.csv"
with tasks_csv.open() as f:
reader = csv.DictReader(f)
for row in reader:
page.goto(f"{task_base_url}/{row['cses_id']}")
with (Path(__file__).parent / "tasks" / row["path"] / "task.md").open(
"a", encoding="utf-8"
) as tasks_md:
try:
page.locator(".md").evaluate(
"""
(node) => {
const mrowElements = document.querySelectorAll('mrow');
mrowElements.forEach((element) => {
element.remove();
});
const katexElements = document.querySelectorAll('.katex-html');
katexElements.forEach((element) => {
element.remove();
});
const images = document.querySelectorAll('img[src^="/file"]');
images.forEach((image, i) => {
const imgUrl = image.getAttribute('src');
const imageName = imgUrl.split('/').pop();
const span = document.createElement('span');
span.textContent = '\\n\\n';
image.insertAdjacentElement('afterend', span);
});
const h1Elements = document.querySelectorAll('h1');
h1Elements.forEach((element) => {
element.textContent = '## ' + element.textContent;
});
const annotationElements = document.querySelectorAll('annotation');
annotationElements.forEach((element) => {
element.textContent = '```' + element.textContent + '```';
});
const listItemElements = node.querySelectorAll('li');
listItemElements.forEach((element) => {
element.innerHTML = '- ' + element.innerHTML;
});
const headings = document.querySelectorAll('h1:not([id^="example"])');
headings.forEach((element) => {
let nextElement = element.nextElementSibling;
console.log(nextElement, nextElement.id, nextElement.id === "example")
if (nextElement && nextElement.tagName === 'P') {
nextElement.textContent = '- ' + nextElement.textContent;
}
});
const preElements = document.querySelectorAll('pre');
preElements.forEach((element) => {
element.textContent = '```\\n' + element.textContent.trim() + '\\n```\\n';
});
return node.innerText
}
"""
)
images = [
image.get_attribute("src")
for image in page.locator("img").all()
if image.get_attribute("src").startswith("/file")
]
if images:
images_dir = (
Path(__file__).parent / "tasks" / row["path"] / "images"
)
images_dir.mkdir(exist_ok=True)
for image_url in images:
response = requests.get(f"https://cses.fi{image_url}")
image_file = images_dir / f'{image_url.split("/")[-1]}.png'
with image_file.open("wb") as f:
f.write(response.content)
examples_dir = (
Path(__file__).parent / "tasks" / row["path"] / "examples"
)
examples_dir.mkdir(exist_ok=True)
for i, pre in enumerate(page.locator('h1[id^="example"] ~ pre').all()):
example_dir = examples_dir / str(i // 2 + 1).rjust(2, "0")
example_dir.mkdir(exist_ok=True)
if i % 2 == 0:
with (example_dir / "in").open("w") as f:
f.write(
pre.text_content()
.strip()
.removeprefix("```")
.removesuffix("```")
.strip()
)
f.write("\n")
else:
with (example_dir / "out").open("w") as f:
f.write(
pre.text_content()
.strip()
.removeprefix("```")
.removesuffix("```")
.strip()
)
f.write("\n")
tasks_md.write(page.locator(".md").text_content())
except Exception as e:
print(row, e)
page.goto("https://cses.fi/problemset/")
page.get_by_text("Login").click()
page.fill('input[type="text"]', os.environ["CSES_USERNAME"])
page.fill('input[type="password"]', os.environ["CSES_PASSWORD"])
page.click('input[type="submit"]')
tests_base_url = "https://cses.fi/problemset/tests"
tasks_csv = Path(__file__).parent / "tasks.csv"
with tasks_csv.open() as f:
reader = csv.DictReader(f)
for row in reader:
try:
page.goto(f"{tests_base_url}/{row['cses_id']}")
with page.expect_download() as download:
page.click('input[value="Download"]')
download.value.save_as(
Path(__file__).parent / "tasks" / row["path"] / "tests.zip"
)
except Exception as e:
print(row, e)
browser.close()
playwright.stop()