refactor(submit): pass file path instead of source via stdin
Problem: Submit read the source file in Lua, piped the full content as stdin through to Python, then re-encoded it into an in-memory buffer just to hand it back to the browser's file input. Unnecessary roundtrip for AtCoder; CF also gains nothing from Lua owning the read. Solution: Pass `source_file` path as a CLI arg to the scraper instead of reading it in Lua and streaming via stdin. AtCoder calls `page.set_input_files(file_path)` directly. Codeforces reads the file with `Path(file_path).read_text()` before the browser session. Also saves the buffer with `vim.cmd.update()` before submitting.
This commit is contained in:
parent
b5a8fce13c
commit
c963728fe9
5 changed files with 22 additions and 38 deletions
|
|
@ -316,15 +316,14 @@ function M.submit(
|
||||||
contest_id,
|
contest_id,
|
||||||
problem_id,
|
problem_id,
|
||||||
language,
|
language,
|
||||||
source_code,
|
source_file,
|
||||||
credentials,
|
credentials,
|
||||||
on_status,
|
on_status,
|
||||||
callback
|
callback
|
||||||
)
|
)
|
||||||
local done = false
|
local done = false
|
||||||
run_scraper(platform, 'submit', { contest_id, problem_id, language }, {
|
run_scraper(platform, 'submit', { contest_id, problem_id, language, source_file }, {
|
||||||
ndjson = true,
|
ndjson = true,
|
||||||
stdin = source_code,
|
|
||||||
env_extra = { CP_CREDENTIALS = vim.json.encode(credentials) },
|
env_extra = { CP_CREDENTIALS = vim.json.encode(credentials) },
|
||||||
on_event = function(ev)
|
on_event = function(ev)
|
||||||
if ev.credentials ~= nil then
|
if ev.credentials ~= nil then
|
||||||
|
|
|
||||||
|
|
@ -53,9 +53,7 @@ function M.submit(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
prompt_credentials(platform, function(creds)
|
prompt_credentials(platform, function(creds)
|
||||||
local source_lines = vim.fn.readfile(source_file)
|
vim.cmd.update()
|
||||||
local source_code = table.concat(source_lines, '\n')
|
|
||||||
|
|
||||||
vim.notify('[cp.nvim] Submitting...', vim.log.levels.INFO)
|
vim.notify('[cp.nvim] Submitting...', vim.log.levels.INFO)
|
||||||
|
|
||||||
require('cp.scraper').submit(
|
require('cp.scraper').submit(
|
||||||
|
|
@ -63,7 +61,7 @@ function M.submit(opts)
|
||||||
contest_id,
|
contest_id,
|
||||||
problem_id,
|
problem_id,
|
||||||
language,
|
language,
|
||||||
source_code,
|
source_file,
|
||||||
creds,
|
creds,
|
||||||
function(ev)
|
function(ev)
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
|
|
|
||||||
|
|
@ -37,11 +37,6 @@ from .timeouts import (
|
||||||
HTTP_TIMEOUT,
|
HTTP_TIMEOUT,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LANGUAGE_ID_EXTENSION = {
|
|
||||||
"6017": "cc",
|
|
||||||
"6082": "py",
|
|
||||||
}
|
|
||||||
|
|
||||||
MIB_TO_MB = 1.048576
|
MIB_TO_MB = 1.048576
|
||||||
BASE_URL = "https://atcoder.jp"
|
BASE_URL = "https://atcoder.jp"
|
||||||
ARCHIVE_URL = f"{BASE_URL}/contests/archive"
|
ARCHIVE_URL = f"{BASE_URL}/contests/archive"
|
||||||
|
|
@ -297,7 +292,7 @@ def _ensure_browser() -> None:
|
||||||
def _submit_headless(
|
def _submit_headless(
|
||||||
contest_id: str,
|
contest_id: str,
|
||||||
problem_id: str,
|
problem_id: str,
|
||||||
source_code: str,
|
file_path: str,
|
||||||
language_id: str,
|
language_id: str,
|
||||||
credentials: dict[str, str],
|
credentials: dict[str, str],
|
||||||
_retried: bool = False,
|
_retried: bool = False,
|
||||||
|
|
@ -362,15 +357,7 @@ def _submit_headless(
|
||||||
f'select[name="data.LanguageId"] option[value="{language_id}"]'
|
f'select[name="data.LanguageId"] option[value="{language_id}"]'
|
||||||
).wait_for(state="attached", timeout=BROWSER_ELEMENT_WAIT)
|
).wait_for(state="attached", timeout=BROWSER_ELEMENT_WAIT)
|
||||||
page.select_option('select[name="data.LanguageId"]', language_id)
|
page.select_option('select[name="data.LanguageId"]', language_id)
|
||||||
ext = _LANGUAGE_ID_EXTENSION.get(language_id, "txt")
|
page.set_input_files("#input-open-file", file_path)
|
||||||
page.set_input_files(
|
|
||||||
"#input-open-file",
|
|
||||||
{
|
|
||||||
"name": f"solution.{ext}",
|
|
||||||
"mimeType": "text/plain",
|
|
||||||
"buffer": source_code.encode(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
page.wait_for_timeout(BROWSER_SETTLE_DELAY)
|
page.wait_for_timeout(BROWSER_SETTLE_DELAY)
|
||||||
page.locator('button[type="submit"]').click()
|
page.locator('button[type="submit"]').click()
|
||||||
page.wait_for_url(
|
page.wait_for_url(
|
||||||
|
|
@ -423,7 +410,7 @@ def _submit_headless(
|
||||||
return _submit_headless(
|
return _submit_headless(
|
||||||
contest_id,
|
contest_id,
|
||||||
problem_id,
|
problem_id,
|
||||||
source_code,
|
file_path,
|
||||||
language_id,
|
language_id,
|
||||||
credentials,
|
credentials,
|
||||||
_retried=True,
|
_retried=True,
|
||||||
|
|
@ -581,7 +568,7 @@ class AtcoderScraper(BaseScraper):
|
||||||
self,
|
self,
|
||||||
contest_id: str,
|
contest_id: str,
|
||||||
problem_id: str,
|
problem_id: str,
|
||||||
source_code: str,
|
file_path: str,
|
||||||
language_id: str,
|
language_id: str,
|
||||||
credentials: dict[str, str],
|
credentials: dict[str, str],
|
||||||
) -> SubmitResult:
|
) -> SubmitResult:
|
||||||
|
|
@ -589,7 +576,7 @@ class AtcoderScraper(BaseScraper):
|
||||||
_submit_headless,
|
_submit_headless,
|
||||||
contest_id,
|
contest_id,
|
||||||
problem_id,
|
problem_id,
|
||||||
source_code,
|
file_path,
|
||||||
language_id,
|
language_id,
|
||||||
credentials,
|
credentials,
|
||||||
)
|
)
|
||||||
|
|
@ -651,15 +638,14 @@ async def main_async() -> int:
|
||||||
return 0 if contest_result.success else 1
|
return 0 if contest_result.success else 1
|
||||||
|
|
||||||
if mode == "submit":
|
if mode == "submit":
|
||||||
if len(sys.argv) != 5:
|
if len(sys.argv) != 6:
|
||||||
print(
|
print(
|
||||||
SubmitResult(
|
SubmitResult(
|
||||||
success=False,
|
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()
|
).model_dump_json()
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
source_code = sys.stdin.read()
|
|
||||||
creds_raw = os.environ.get("CP_CREDENTIALS", "{}")
|
creds_raw = os.environ.get("CP_CREDENTIALS", "{}")
|
||||||
try:
|
try:
|
||||||
credentials = json.loads(creds_raw)
|
credentials = json.loads(creds_raw)
|
||||||
|
|
@ -667,7 +653,7 @@ async def main_async() -> int:
|
||||||
credentials = {}
|
credentials = {}
|
||||||
language_id = get_language_id("atcoder", sys.argv[4]) or sys.argv[4]
|
language_id = get_language_id("atcoder", sys.argv[4]) or sys.argv[4]
|
||||||
submit_result = await scraper.submit(
|
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())
|
print(submit_result.model_dump_json())
|
||||||
return 0 if submit_result.success else 1
|
return 0 if submit_result.success else 1
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ class BaseScraper(ABC):
|
||||||
self,
|
self,
|
||||||
contest_id: str,
|
contest_id: str,
|
||||||
problem_id: str,
|
problem_id: str,
|
||||||
source_code: str,
|
file_path: str,
|
||||||
language_id: str,
|
language_id: str,
|
||||||
credentials: dict[str, str],
|
credentials: dict[str, str],
|
||||||
) -> SubmitResult: ...
|
) -> SubmitResult: ...
|
||||||
|
|
@ -114,14 +114,13 @@ class BaseScraper(ABC):
|
||||||
return 0 if result.success else 1
|
return 0 if result.success else 1
|
||||||
|
|
||||||
case "submit":
|
case "submit":
|
||||||
if len(args) != 5:
|
if len(args) != 6:
|
||||||
print(
|
print(
|
||||||
self._submit_error(
|
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()
|
).model_dump_json()
|
||||||
)
|
)
|
||||||
return 1
|
return 1
|
||||||
source_code = sys.stdin.read()
|
|
||||||
creds_raw = os.environ.get("CP_CREDENTIALS", "{}")
|
creds_raw = os.environ.get("CP_CREDENTIALS", "{}")
|
||||||
try:
|
try:
|
||||||
credentials = json.loads(creds_raw)
|
credentials = json.loads(creds_raw)
|
||||||
|
|
@ -129,7 +128,7 @@ class BaseScraper(ABC):
|
||||||
credentials = {}
|
credentials = {}
|
||||||
language_id = get_language_id(self.platform_name, args[4]) or args[4]
|
language_id = get_language_id(self.platform_name, args[4]) or args[4]
|
||||||
result = await self.submit(
|
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())
|
print(result.model_dump_json())
|
||||||
return 0 if result.success else 1
|
return 0 if result.success else 1
|
||||||
|
|
|
||||||
|
|
@ -289,7 +289,7 @@ class CodeforcesScraper(BaseScraper):
|
||||||
self,
|
self,
|
||||||
contest_id: str,
|
contest_id: str,
|
||||||
problem_id: str,
|
problem_id: str,
|
||||||
source_code: str,
|
file_path: str,
|
||||||
language_id: str,
|
language_id: str,
|
||||||
credentials: dict[str, str],
|
credentials: dict[str, str],
|
||||||
) -> SubmitResult:
|
) -> SubmitResult:
|
||||||
|
|
@ -297,7 +297,7 @@ class CodeforcesScraper(BaseScraper):
|
||||||
_submit_headless,
|
_submit_headless,
|
||||||
contest_id,
|
contest_id,
|
||||||
problem_id,
|
problem_id,
|
||||||
source_code,
|
file_path,
|
||||||
language_id,
|
language_id,
|
||||||
credentials,
|
credentials,
|
||||||
)
|
)
|
||||||
|
|
@ -306,13 +306,15 @@ class CodeforcesScraper(BaseScraper):
|
||||||
def _submit_headless(
|
def _submit_headless(
|
||||||
contest_id: str,
|
contest_id: str,
|
||||||
problem_id: str,
|
problem_id: str,
|
||||||
source_code: str,
|
file_path: str,
|
||||||
language_id: str,
|
language_id: str,
|
||||||
credentials: dict[str, str],
|
credentials: dict[str, str],
|
||||||
_retried: bool = False,
|
_retried: bool = False,
|
||||||
) -> SubmitResult:
|
) -> SubmitResult:
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
source_code = Path(file_path).read_text()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from scrapling.fetchers import StealthySession # type: ignore[import-untyped,unresolved-import]
|
from scrapling.fetchers import StealthySession # type: ignore[import-untyped,unresolved-import]
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
@ -451,7 +453,7 @@ def _submit_headless(
|
||||||
return _submit_headless(
|
return _submit_headless(
|
||||||
contest_id,
|
contest_id,
|
||||||
problem_id,
|
problem_id,
|
||||||
source_code,
|
file_path,
|
||||||
language_id,
|
language_id,
|
||||||
credentials,
|
credentials,
|
||||||
_retried=True,
|
_retried=True,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue