Merge branch 'main' into feat/test

This commit is contained in:
Barrett Ruth 2025-09-18 16:21:45 +02:00 committed by GitHub
commit de5020e4da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 137 additions and 84 deletions

View file

@ -12,10 +12,36 @@ def parse_problem_url(contest_id: str, problem_letter: str) -> str:
return f"https://atcoder.jp/contests/{contest_id}/tasks/{task_id}"
def extract_problem_from_row(row, contest_id: str) -> dict[str, str] | None:
cells = row.find_all("td")
if len(cells) < 2:
return None
task_link = cells[1].find("a")
if not task_link:
return None
task_name = task_link.get_text(strip=True)
task_href = task_link.get("href", "")
if not task_href:
return None
task_id = task_href.split("/")[-1]
if not task_id.startswith(contest_id + "_"):
return None
problem_letter = task_id[len(contest_id) + 1 :]
if not problem_letter or not task_name:
return None
return {"id": problem_letter.lower(), "name": task_name}
def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]:
try:
contest_url: str = f"https://atcoder.jp/contests/{contest_id}/tasks"
headers: dict[str, str] = {
contest_url = f"https://atcoder.jp/contests/{contest_id}/tasks"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
@ -23,31 +49,18 @@ def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]:
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
problems: list[dict[str, str]] = []
task_table = soup.find("table", class_="table")
if not task_table:
return []
rows = task_table.find_all("tr")[1:] # Skip header row
rows = task_table.find_all("tr")[1:]
problems = []
for row in rows:
cells = row.find_all("td")
if len(cells) >= 2:
task_link = cells[1].find("a")
if task_link:
task_name: str = task_link.get_text(strip=True)
task_href: str = task_link.get("href", "")
# Extract problem letter from task name or URL
task_id: str = task_href.split("/")[-1] if task_href else ""
if task_id.startswith(contest_id + "_"):
problem_letter: str = task_id[len(contest_id) + 1 :]
if problem_letter and task_name:
problems.append(
{"id": problem_letter.lower(), "name": task_name}
)
problem = extract_problem_from_row(row, contest_id)
if problem:
problems.append(problem)
problems.sort(key=lambda x: x["id"])
return problems
@ -57,9 +70,38 @@ def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]:
return []
def extract_test_case_from_headers(sample_headers, i: int) -> tuple[str, str] | None:
if i >= len(sample_headers):
return None
header = sample_headers[i]
if "input" not in header.get_text().lower():
return None
input_pre = header.find_next("pre")
if not input_pre or i + 1 >= len(sample_headers):
return None
next_header = sample_headers[i + 1]
if "output" not in next_header.get_text().lower():
return None
output_pre = next_header.find_next("pre")
if not output_pre:
return None
input_text = input_pre.get_text().strip().replace("\r", "")
output_text = output_pre.get_text().strip().replace("\r", "")
if not input_text or not output_text:
return None
return (input_text, output_text)
def scrape(url: str) -> list[tuple[str, str]]:
try:
headers: dict[str, str] = {
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
@ -68,33 +110,20 @@ def scrape(url: str) -> list[tuple[str, str]]:
soup = BeautifulSoup(response.text, "html.parser")
tests: list[tuple[str, str]] = []
sample_headers = soup.find_all(
"h3", string=lambda x: x and "sample" in x.lower() if x else False
)
tests = []
i = 0
while i < len(sample_headers):
header = sample_headers[i]
if "input" in header.get_text().lower():
input_pre = header.find_next("pre")
if input_pre and i + 1 < len(sample_headers):
next_header = sample_headers[i + 1]
if "output" in next_header.get_text().lower():
output_pre = next_header.find_next("pre")
if output_pre:
input_text: str = (
input_pre.get_text().strip().replace("\r", "")
)
output_text: str = (
output_pre.get_text().strip().replace("\r", "")
)
if input_text and output_text:
tests.append((input_text, output_text))
i += 2
continue
i += 1
test_case = extract_test_case_from_headers(sample_headers, i)
if test_case:
tests.append(test_case)
i += 2
else:
i += 1
return tests