feat: caching
This commit is contained in:
parent
64c7559c78
commit
40117c2cf1
10 changed files with 764 additions and 175 deletions
|
|
@ -60,51 +60,135 @@ def parse_problem_url(contest_id: str, problem_letter: str) -> str:
|
|||
)
|
||||
|
||||
|
||||
def scrape_contest_problems(contest_id: str):
|
||||
try:
|
||||
contest_url = f"https://codeforces.com/contest/{contest_id}"
|
||||
scraper = cloudscraper.create_scraper()
|
||||
response = scraper.get(contest_url, timeout=10)
|
||||
response.raise_for_status()
|
||||
|
||||
soup = BeautifulSoup(response.text, "html.parser")
|
||||
problems = []
|
||||
|
||||
problem_links = soup.find_all("a", href=lambda x: x and f"/contest/{contest_id}/problem/" in x)
|
||||
|
||||
for link in problem_links:
|
||||
href = link.get("href", "")
|
||||
if f"/contest/{contest_id}/problem/" in href:
|
||||
problem_letter = href.split("/")[-1].lower()
|
||||
problem_name = link.get_text(strip=True)
|
||||
|
||||
if problem_letter and problem_name and len(problem_letter) == 1:
|
||||
problems.append({
|
||||
"id": problem_letter,
|
||||
"name": problem_name
|
||||
})
|
||||
|
||||
problems.sort(key=lambda x: x["id"])
|
||||
|
||||
seen = set()
|
||||
unique_problems = []
|
||||
for p in problems:
|
||||
if p["id"] not in seen:
|
||||
seen.add(p["id"])
|
||||
unique_problems.append(p)
|
||||
|
||||
return unique_problems
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed to scrape contest problems: {e}", file=sys.stderr)
|
||||
return []
|
||||
|
||||
|
||||
def scrape_sample_tests(url: str):
|
||||
print(f"Scraping: {url}", file=sys.stderr)
|
||||
return scrape(url)
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3:
|
||||
if len(sys.argv) < 2:
|
||||
result = {
|
||||
"success": False,
|
||||
"error": "Usage: codeforces.py <contest_id> <problem_letter>",
|
||||
"problem_id": None,
|
||||
"error": "Usage: codeforces.py metadata <contest_id> OR codeforces.py tests <contest_id> <problem_letter>",
|
||||
}
|
||||
print(json.dumps(result))
|
||||
sys.exit(1)
|
||||
|
||||
contest_id = sys.argv[1]
|
||||
problem_letter = sys.argv[2]
|
||||
problem_id = contest_id + problem_letter.lower()
|
||||
mode = sys.argv[1]
|
||||
|
||||
url = parse_problem_url(contest_id, problem_letter)
|
||||
tests = scrape_sample_tests(url)
|
||||
if mode == "metadata":
|
||||
if len(sys.argv) != 3:
|
||||
result = {
|
||||
"success": False,
|
||||
"error": "Usage: codeforces.py metadata <contest_id>",
|
||||
}
|
||||
print(json.dumps(result))
|
||||
sys.exit(1)
|
||||
|
||||
contest_id = sys.argv[2]
|
||||
problems = scrape_contest_problems(contest_id)
|
||||
|
||||
if not problems:
|
||||
result = {
|
||||
"success": False,
|
||||
"error": f"No problems found for contest {contest_id}",
|
||||
}
|
||||
print(json.dumps(result))
|
||||
sys.exit(1)
|
||||
|
||||
if not tests:
|
||||
result = {
|
||||
"success": False,
|
||||
"error": f"No tests found for {contest_id} {problem_letter}",
|
||||
"success": True,
|
||||
"contest_id": contest_id,
|
||||
"problems": problems,
|
||||
}
|
||||
print(json.dumps(result))
|
||||
|
||||
elif mode == "tests":
|
||||
if len(sys.argv) != 4:
|
||||
result = {
|
||||
"success": False,
|
||||
"error": "Usage: codeforces.py tests <contest_id> <problem_letter>",
|
||||
}
|
||||
print(json.dumps(result))
|
||||
sys.exit(1)
|
||||
|
||||
contest_id = sys.argv[2]
|
||||
problem_letter = sys.argv[3]
|
||||
problem_id = contest_id + problem_letter.lower()
|
||||
|
||||
url = parse_problem_url(contest_id, problem_letter)
|
||||
tests = scrape_sample_tests(url)
|
||||
|
||||
if not tests:
|
||||
result = {
|
||||
"success": False,
|
||||
"error": f"No tests found for {contest_id} {problem_letter}",
|
||||
"problem_id": problem_id,
|
||||
"url": url,
|
||||
}
|
||||
print(json.dumps(result))
|
||||
sys.exit(1)
|
||||
|
||||
test_cases = []
|
||||
for input_data, output_data in tests:
|
||||
test_cases.append({"input": input_data, "output": output_data})
|
||||
|
||||
result = {
|
||||
"success": True,
|
||||
"problem_id": problem_id,
|
||||
"url": url,
|
||||
"test_cases": test_cases,
|
||||
}
|
||||
print(json.dumps(result))
|
||||
|
||||
else:
|
||||
result = {
|
||||
"success": False,
|
||||
"error": f"Unknown mode: {mode}. Use 'metadata' or 'tests'",
|
||||
}
|
||||
print(json.dumps(result))
|
||||
sys.exit(1)
|
||||
|
||||
test_cases = []
|
||||
for input_data, output_data in tests:
|
||||
test_cases.append({"input": input_data, "output": output_data})
|
||||
|
||||
result = {
|
||||
"success": True,
|
||||
"problem_id": problem_id,
|
||||
"url": url,
|
||||
"test_cases": test_cases,
|
||||
}
|
||||
|
||||
print(json.dumps(result))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue