fix(submit): use file path over stdin; fix CF CodeMirror textarea (#305)
## Problem After the initial submit hardening, two issues remained: source code was read in Lua and piped as stdin to the scraper (unnecessary roundtrip since the file exists on disk), and CF's `page.fill()` timed out on the hidden `textarea[name="source"]` because CodeMirror owns the editor state. ## Solution Pass the source file path as a CLI arg instead — AtCoder calls `page.set_input_files(file_path)` directly, CF reads it with `Path(file_path).read_text()`. Fix CF source injection via `page.evaluate()` into the CodeMirror instance. Extract `BROWSER_SUBMIT_NAV_TIMEOUT` as a per-platform `defaultdict` (CF defaults to 2× nav timeout). Save the buffer with `vim.cmd.update()` before submitting.
This commit is contained in:
parent
127089c57f
commit
a202725cc5
28 changed files with 269 additions and 168 deletions
|
|
@ -33,6 +33,7 @@ from .timeouts import (
|
|||
BROWSER_NAV_TIMEOUT,
|
||||
BROWSER_SESSION_TIMEOUT,
|
||||
BROWSER_SETTLE_DELAY,
|
||||
BROWSER_SUBMIT_NAV_TIMEOUT,
|
||||
BROWSER_TURNSTILE_POLL,
|
||||
HTTP_TIMEOUT,
|
||||
)
|
||||
|
|
@ -297,7 +298,7 @@ def _ensure_browser() -> None:
|
|||
def _submit_headless(
|
||||
contest_id: str,
|
||||
problem_id: str,
|
||||
source_code: str,
|
||||
file_path: str,
|
||||
language_id: str,
|
||||
credentials: dict[str, str],
|
||||
_retried: bool = False,
|
||||
|
|
@ -362,19 +363,12 @@ def _submit_headless(
|
|||
f'select[name="data.LanguageId"] option[value="{language_id}"]'
|
||||
).wait_for(state="attached", timeout=BROWSER_ELEMENT_WAIT)
|
||||
page.select_option('select[name="data.LanguageId"]', language_id)
|
||||
ext = _LANGUAGE_ID_EXTENSION.get(language_id, "txt")
|
||||
page.set_input_files(
|
||||
"#input-open-file",
|
||||
{
|
||||
"name": f"solution.{ext}",
|
||||
"mimeType": "text/plain",
|
||||
"buffer": source_code.encode(),
|
||||
},
|
||||
)
|
||||
page.set_input_files("#input-open-file", file_path)
|
||||
page.wait_for_timeout(BROWSER_SETTLE_DELAY)
|
||||
page.locator('button[type="submit"]').click()
|
||||
page.wait_for_url(
|
||||
lambda url: "/submissions/me" in url, timeout=BROWSER_NAV_TIMEOUT
|
||||
lambda url: "/submissions/me" in url,
|
||||
timeout=BROWSER_SUBMIT_NAV_TIMEOUT["atcoder"],
|
||||
)
|
||||
except Exception as e:
|
||||
submit_error = str(e)
|
||||
|
|
@ -423,7 +417,7 @@ def _submit_headless(
|
|||
return _submit_headless(
|
||||
contest_id,
|
||||
problem_id,
|
||||
source_code,
|
||||
file_path,
|
||||
language_id,
|
||||
credentials,
|
||||
_retried=True,
|
||||
|
|
@ -581,7 +575,7 @@ class AtcoderScraper(BaseScraper):
|
|||
self,
|
||||
contest_id: str,
|
||||
problem_id: str,
|
||||
source_code: str,
|
||||
file_path: str,
|
||||
language_id: str,
|
||||
credentials: dict[str, str],
|
||||
) -> SubmitResult:
|
||||
|
|
@ -589,7 +583,7 @@ class AtcoderScraper(BaseScraper):
|
|||
_submit_headless,
|
||||
contest_id,
|
||||
problem_id,
|
||||
source_code,
|
||||
file_path,
|
||||
language_id,
|
||||
credentials,
|
||||
)
|
||||
|
|
@ -651,15 +645,14 @@ async def main_async() -> int:
|
|||
return 0 if contest_result.success else 1
|
||||
|
||||
if mode == "submit":
|
||||
if len(sys.argv) != 5:
|
||||
if len(sys.argv) != 6:
|
||||
print(
|
||||
SubmitResult(
|
||||
success=False,
|
||||
error="Usage: atcoder.py submit <contest_id> <problem_id> <language>",
|
||||
error="Usage: atcoder.py submit <contest_id> <problem_id> <language> <file_path>",
|
||||
).model_dump_json()
|
||||
)
|
||||
return 1
|
||||
source_code = sys.stdin.read()
|
||||
creds_raw = os.environ.get("CP_CREDENTIALS", "{}")
|
||||
try:
|
||||
credentials = json.loads(creds_raw)
|
||||
|
|
@ -667,7 +660,7 @@ async def main_async() -> int:
|
|||
credentials = {}
|
||||
language_id = get_language_id("atcoder", sys.argv[4]) or sys.argv[4]
|
||||
submit_result = await scraper.submit(
|
||||
sys.argv[2], sys.argv[3], source_code, language_id, credentials
|
||||
sys.argv[2], sys.argv[3], sys.argv[5], language_id, credentials
|
||||
)
|
||||
print(submit_result.model_dump_json())
|
||||
return 0 if submit_result.success else 1
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ class BaseScraper(ABC):
|
|||
self,
|
||||
contest_id: str,
|
||||
problem_id: str,
|
||||
source_code: str,
|
||||
file_path: str,
|
||||
language_id: str,
|
||||
credentials: dict[str, str],
|
||||
) -> SubmitResult: ...
|
||||
|
|
@ -114,14 +114,13 @@ class BaseScraper(ABC):
|
|||
return 0 if result.success else 1
|
||||
|
||||
case "submit":
|
||||
if len(args) != 5:
|
||||
if len(args) != 6:
|
||||
print(
|
||||
self._submit_error(
|
||||
"Usage: <platform> submit <contest_id> <problem_id> <language_id>"
|
||||
"Usage: <platform> submit <contest_id> <problem_id> <language_id> <file_path>"
|
||||
).model_dump_json()
|
||||
)
|
||||
return 1
|
||||
source_code = sys.stdin.read()
|
||||
creds_raw = os.environ.get("CP_CREDENTIALS", "{}")
|
||||
try:
|
||||
credentials = json.loads(creds_raw)
|
||||
|
|
@ -129,7 +128,7 @@ class BaseScraper(ABC):
|
|||
credentials = {}
|
||||
language_id = get_language_id(self.platform_name, args[4]) or args[4]
|
||||
result = await self.submit(
|
||||
args[2], args[3], source_code, language_id, credentials
|
||||
args[2], args[3], args[5], language_id, credentials
|
||||
)
|
||||
print(result.model_dump_json())
|
||||
return 0 if result.success else 1
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ MEMORY_LIMIT_RE = re.compile(
|
|||
)
|
||||
|
||||
|
||||
async def fetch_json(client: httpx.AsyncClient, path: str) -> dict:
|
||||
async def fetch_json(client: httpx.AsyncClient, path: str) -> dict[str, Any]:
|
||||
r = await client.get(BASE_URL + path, headers=HEADERS, timeout=HTTP_TIMEOUT)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
|
@ -256,7 +256,7 @@ class CodeChefScraper(BaseScraper):
|
|||
self,
|
||||
contest_id: str,
|
||||
problem_id: str,
|
||||
source_code: str,
|
||||
file_path: str,
|
||||
language_id: str,
|
||||
credentials: dict[str, str],
|
||||
) -> SubmitResult:
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ from .models import (
|
|||
from .timeouts import (
|
||||
BROWSER_NAV_TIMEOUT,
|
||||
BROWSER_SESSION_TIMEOUT,
|
||||
BROWSER_SUBMIT_NAV_TIMEOUT,
|
||||
HTTP_TIMEOUT,
|
||||
)
|
||||
|
||||
|
|
@ -289,7 +290,7 @@ class CodeforcesScraper(BaseScraper):
|
|||
self,
|
||||
contest_id: str,
|
||||
problem_id: str,
|
||||
source_code: str,
|
||||
file_path: str,
|
||||
language_id: str,
|
||||
credentials: dict[str, str],
|
||||
) -> SubmitResult:
|
||||
|
|
@ -297,7 +298,7 @@ class CodeforcesScraper(BaseScraper):
|
|||
_submit_headless,
|
||||
contest_id,
|
||||
problem_id,
|
||||
source_code,
|
||||
file_path,
|
||||
language_id,
|
||||
credentials,
|
||||
)
|
||||
|
|
@ -306,13 +307,15 @@ class CodeforcesScraper(BaseScraper):
|
|||
def _submit_headless(
|
||||
contest_id: str,
|
||||
problem_id: str,
|
||||
source_code: str,
|
||||
file_path: str,
|
||||
language_id: str,
|
||||
credentials: dict[str, str],
|
||||
_retried: bool = False,
|
||||
) -> SubmitResult:
|
||||
from pathlib import Path
|
||||
|
||||
source_code = Path(file_path).read_text()
|
||||
|
||||
try:
|
||||
from scrapling.fetchers import StealthySession # type: ignore[import-untyped,unresolved-import]
|
||||
except ImportError:
|
||||
|
|
@ -379,12 +382,22 @@ def _submit_headless(
|
|||
problem_id.upper(),
|
||||
)
|
||||
page.select_option('select[name="programTypeId"]', language_id)
|
||||
page.fill('textarea[name="source"]', source_code)
|
||||
page.evaluate(
|
||||
"""(code) => {
|
||||
const cm = document.querySelector('.CodeMirror');
|
||||
if (cm && cm.CodeMirror) {
|
||||
cm.CodeMirror.setValue(code);
|
||||
}
|
||||
const ta = document.querySelector('textarea[name="source"]');
|
||||
if (ta) ta.value = code;
|
||||
}""",
|
||||
source_code,
|
||||
)
|
||||
page.locator("form.submit-form input.submit").click(no_wait_after=True)
|
||||
try:
|
||||
page.wait_for_url(
|
||||
lambda url: "/my" in url or "/status" in url,
|
||||
timeout=BROWSER_NAV_TIMEOUT * 2,
|
||||
timeout=BROWSER_SUBMIT_NAV_TIMEOUT["codeforces"],
|
||||
)
|
||||
except Exception:
|
||||
err_el = page.query_selector("span.error")
|
||||
|
|
@ -441,7 +454,7 @@ def _submit_headless(
|
|||
return _submit_headless(
|
||||
contest_id,
|
||||
problem_id,
|
||||
source_code,
|
||||
file_path,
|
||||
language_id,
|
||||
credentials,
|
||||
_retried=True,
|
||||
|
|
|
|||
|
|
@ -357,10 +357,13 @@ class CSESScraper(BaseScraper):
|
|||
self,
|
||||
contest_id: str,
|
||||
problem_id: str,
|
||||
source_code: str,
|
||||
file_path: str,
|
||||
language_id: str,
|
||||
credentials: dict[str, str],
|
||||
) -> SubmitResult:
|
||||
from pathlib import Path
|
||||
|
||||
source_code = Path(file_path).read_text()
|
||||
username = credentials.get("username", "")
|
||||
password = credentials.get("password", "")
|
||||
if not username or not password:
|
||||
|
|
|
|||
|
|
@ -273,7 +273,7 @@ class KattisScraper(BaseScraper):
|
|||
self,
|
||||
contest_id: str,
|
||||
problem_id: str,
|
||||
source_code: str,
|
||||
file_path: str,
|
||||
language_id: str,
|
||||
credentials: dict[str, str],
|
||||
) -> SubmitResult:
|
||||
|
|
|
|||
|
|
@ -1,7 +1,13 @@
|
|||
from collections import defaultdict
|
||||
|
||||
HTTP_TIMEOUT = 15.0
|
||||
|
||||
BROWSER_SESSION_TIMEOUT = 15000
|
||||
BROWSER_NAV_TIMEOUT = 10000
|
||||
BROWSER_SUBMIT_NAV_TIMEOUT: defaultdict[str, int] = defaultdict(
|
||||
lambda: BROWSER_NAV_TIMEOUT
|
||||
)
|
||||
BROWSER_SUBMIT_NAV_TIMEOUT["codeforces"] = BROWSER_NAV_TIMEOUT * 2
|
||||
BROWSER_TURNSTILE_POLL = 5000
|
||||
BROWSER_ELEMENT_WAIT = 10000
|
||||
BROWSER_SETTLE_DELAY = 500
|
||||
|
|
|
|||
|
|
@ -73,8 +73,11 @@ def _parse_results_page(html: str) -> dict[str, list[tuple[str, str]]]:
|
|||
for part in parts:
|
||||
heading_m = DIVISION_HEADING_RE.search(part)
|
||||
if heading_m:
|
||||
current_div = heading_m.group(3).lower()
|
||||
sections.setdefault(current_div, [])
|
||||
div = heading_m.group(3)
|
||||
if div:
|
||||
key = div.lower()
|
||||
current_div = key
|
||||
sections.setdefault(key, [])
|
||||
continue
|
||||
if current_div is not None:
|
||||
for m in PROBLEM_BLOCK_RE.finditer(part):
|
||||
|
|
@ -285,7 +288,7 @@ class USACOScraper(BaseScraper):
|
|||
self,
|
||||
contest_id: str,
|
||||
problem_id: str,
|
||||
source_code: str,
|
||||
file_path: str,
|
||||
language_id: str,
|
||||
credentials: dict[str, str],
|
||||
) -> SubmitResult:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue