perf(cses): cache API token across submits

Problem: every `:CP submit` on CSES ran the full 5-request login flow
(~1.5 s overhead) even when the token from a previous submit was still
valid.

Solution: persist the API token in credentials via a `credentials`
ndjson event. On subsequent submits, validate the cached token with a
single GET before falling back to the full login.
This commit is contained in:
Barrett Ruth 2026-03-05 01:18:16 -05:00
parent 08593d828d
commit 027fae65a4

View file

@ -342,6 +342,19 @@ class CSESScraper(BaseScraper):
return None
return token
async def _check_token(
self, client: httpx.AsyncClient, token: str
) -> bool:
try:
r = await client.get(
f"{API_URL}/login",
headers={"X-Auth-Token": token, **HEADERS},
timeout=TIMEOUT_S,
)
return r.status_code == 200
except Exception:
return False
async def submit(
self,
contest_id: str,
@ -356,11 +369,30 @@ class CSESScraper(BaseScraper):
return self._submit_error("Missing credentials. Use :CP login cses")
async with httpx.AsyncClient(follow_redirects=True) as client:
print(json.dumps({"status": "logging_in"}), flush=True)
token = credentials.get("token")
if token:
print(json.dumps({"status": "checking_login"}), flush=True)
if not await self._check_token(client, token):
token = None
token = await self._web_login(client, username, password)
if not token:
return self._submit_error("Login failed (bad credentials?)")
print(json.dumps({"status": "logging_in"}), flush=True)
token = await self._web_login(client, username, password)
if not token:
return self._submit_error("Login failed (bad credentials?)")
print(
json.dumps(
{
"credentials": {
"username": username,
"password": password,
"token": token,
}
}
),
flush=True,
)
print(json.dumps({"status": "submitting"}), flush=True)