From d1b946934257901f1a87fe13da7f6720843be6cc Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 14 Sep 2025 20:07:08 -0500 Subject: [PATCH 1/3] fix(scraper): multi-letter codeforces problems --- lua/cp/scrape.lua | 20 ++++++++++++++++++++ scrapers/codeforces.py | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lua/cp/scrape.lua b/lua/cp/scrape.lua index 09ba5c2..82e2e4f 100644 --- a/lua/cp/scrape.lua +++ b/lua/cp/scrape.lua @@ -11,6 +11,11 @@ local function ensure_io_directory() vim.fn.mkdir("io", "p") end +local function check_internet_connectivity() + local result = vim.system({ "ping", "-c", "1", "-W", "3", "8.8.8.8" }, { text = true }):wait() + return result.code == 0 +end + local function setup_python_env() local plugin_path = get_plugin_path() local venv_dir = plugin_path .. "/.venv" @@ -50,6 +55,13 @@ function M.scrape_contest_metadata(platform, contest_id) } end + if not check_internet_connectivity() then + return { + success = false, + error = "No internet connection available", + } + end + if not setup_python_env() then return { success = false, @@ -119,6 +131,14 @@ function M.scrape_problem(ctx) } end + if not check_internet_connectivity() then + return { + success = false, + problem_id = ctx.problem_name, + error = "No internet connection available", + } + end + if not setup_python_env() then return { success = false, diff --git a/scrapers/codeforces.py b/scrapers/codeforces.py index 73be5a4..f2d97fa 100644 --- a/scrapers/codeforces.py +++ b/scrapers/codeforces.py @@ -80,7 +80,7 @@ def scrape_contest_problems(contest_id: str) -> list[dict[str, str]]: problem_letter: str = href.split("/")[-1].lower() problem_name: str = link.get_text(strip=True) - if problem_letter and problem_name and len(problem_letter) == 1: + if problem_letter and problem_name: problems.append({"id": problem_letter, "name": problem_name}) problems.sort(key=lambda x: x["id"]) From ab72ec955967b87b7dad82220a45b28e73fbc7c3 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 14 Sep 2025 20:19:42 -0500 Subject: [PATCH 2/3] fix(cache): problem caching --- lua/cp/cache.lua | 2 +- scrapers/codeforces.py | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lua/cp/cache.lua b/lua/cp/cache.lua index e74ace0..70a55c3 100644 --- a/lua/cp/cache.lua +++ b/lua/cp/cache.lua @@ -1,6 +1,6 @@ local M = {} -local cache_file = vim.fn.stdpath("data") .. "/cp-contest-cache.json" +local cache_file = vim.fn.stdpath("data") .. "/cp-nvim.json" local cache_data = {} local function get_expiry_date(platform) diff --git a/scrapers/codeforces.py b/scrapers/codeforces.py index f2d97fa..54383bc 100644 --- a/scrapers/codeforces.py +++ b/scrapers/codeforces.py @@ -27,20 +27,19 @@ def scrape(url: str) -> list[tuple[str, str]]: input_lines: list[str] = [] output_lines: list[str] = [] - for line_div in inp_pre.find_all("div", class_="test-example-line"): - input_lines.append(line_div.get_text().strip()) + input_text_raw = inp_pre.get_text().strip().replace("\r", "") + input_lines = [ + line.strip() + for line in input_text_raw.split("\n") + if line.strip() + ] - output_divs = out_pre.find_all("div", class_="test-example-line") - if not output_divs: - output_text_raw = out_pre.get_text().strip().replace("\r", "") - output_lines = [ - line.strip() - for line in output_text_raw.split("\n") - if line.strip() - ] - else: - for line_div in output_divs: - output_lines.append(line_div.get_text().strip()) + output_text_raw = out_pre.get_text().strip().replace("\r", "") + output_lines = [ + line.strip() + for line in output_text_raw.split("\n") + if line.strip() + ] if input_lines and output_lines: input_text = "\n".join(input_lines) From 26d2de0de3522c3b3c3a2751e8b2530322256377 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 14 Sep 2025 20:20:37 -0500 Subject: [PATCH 3/3] fix(ci): format --- scrapers/codeforces.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/scrapers/codeforces.py b/scrapers/codeforces.py index 54383bc..6343287 100644 --- a/scrapers/codeforces.py +++ b/scrapers/codeforces.py @@ -29,16 +29,12 @@ def scrape(url: str) -> list[tuple[str, str]]: input_text_raw = inp_pre.get_text().strip().replace("\r", "") input_lines = [ - line.strip() - for line in input_text_raw.split("\n") - if line.strip() + line.strip() for line in input_text_raw.split("\n") if line.strip() ] output_text_raw = out_pre.get_text().strip().replace("\r", "") output_lines = [ - line.strip() - for line in output_text_raw.split("\n") - if line.strip() + line.strip() for line in output_text_raw.split("\n") if line.strip() ] if input_lines and output_lines: