fix(scrapers): remove unused import and fix E402 in base.py
Problem: `atcoder.py` imported `pathlib.Path` but never used it. `base.py` had local imports placed after module-level code, violating E402. Solution: Remove the unused `Path` import from `atcoder.py`. Move the `.language_ids` and `.models` imports to the top of `base.py`, after the stdlib imports.
This commit is contained in:
parent
11ce743273
commit
74c46526c2
2 changed files with 41 additions and 20 deletions
|
|
@ -6,7 +6,6 @@ import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
import backoff
|
import backoff
|
||||||
|
|
@ -16,7 +15,13 @@ from bs4 import BeautifulSoup, Tag
|
||||||
from requests.adapters import HTTPAdapter
|
from requests.adapters import HTTPAdapter
|
||||||
from urllib3.util.retry import Retry
|
from urllib3.util.retry import Retry
|
||||||
|
|
||||||
from .base import BaseScraper, clear_platform_cookies, extract_precision, load_platform_cookies, save_platform_cookies
|
from .base import (
|
||||||
|
BaseScraper,
|
||||||
|
clear_platform_cookies,
|
||||||
|
extract_precision,
|
||||||
|
load_platform_cookies,
|
||||||
|
save_platform_cookies,
|
||||||
|
)
|
||||||
from .models import (
|
from .models import (
|
||||||
ContestListResult,
|
ContestListResult,
|
||||||
ContestSummary,
|
ContestSummary,
|
||||||
|
|
@ -432,7 +437,9 @@ def _login_headless(credentials: dict[str, str]) -> LoginResult:
|
||||||
google_search=False,
|
google_search=False,
|
||||||
cookies=saved_cookies,
|
cookies=saved_cookies,
|
||||||
) as session:
|
) as session:
|
||||||
session.fetch(f"{BASE_URL}/home", page_action=check_action, network_idle=True)
|
session.fetch(
|
||||||
|
f"{BASE_URL}/home", page_action=check_action, network_idle=True
|
||||||
|
)
|
||||||
if logged_in:
|
if logged_in:
|
||||||
return LoginResult(success=True, error="")
|
return LoginResult(success=True, error="")
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|
@ -462,9 +469,13 @@ def _login_headless(credentials: dict[str, str]) -> LoginResult:
|
||||||
nonlocal logged_in
|
nonlocal logged_in
|
||||||
logged_in = _at_check_logged_in(page)
|
logged_in = _at_check_logged_in(page)
|
||||||
|
|
||||||
session.fetch(f"{BASE_URL}/home", page_action=verify_action, network_idle=True)
|
session.fetch(
|
||||||
|
f"{BASE_URL}/home", page_action=verify_action, network_idle=True
|
||||||
|
)
|
||||||
if not logged_in:
|
if not logged_in:
|
||||||
return LoginResult(success=False, error="Login failed (bad credentials?)")
|
return LoginResult(
|
||||||
|
success=False, error="Login failed (bad credentials?)"
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
browser_cookies = session.context.cookies()
|
browser_cookies = session.context.cookies()
|
||||||
|
|
@ -547,7 +558,9 @@ def _submit_headless(
|
||||||
) as session:
|
) as session:
|
||||||
if not _retried and saved_cookies:
|
if not _retried and saved_cookies:
|
||||||
print(json.dumps({"status": "checking_login"}), flush=True)
|
print(json.dumps({"status": "checking_login"}), flush=True)
|
||||||
session.fetch(f"{BASE_URL}/home", page_action=check_login, network_idle=True)
|
session.fetch(
|
||||||
|
f"{BASE_URL}/home", page_action=check_login, network_idle=True
|
||||||
|
)
|
||||||
|
|
||||||
if not logged_in:
|
if not logged_in:
|
||||||
print(json.dumps({"status": "logging_in"}), flush=True)
|
print(json.dumps({"status": "logging_in"}), flush=True)
|
||||||
|
|
@ -558,7 +571,9 @@ def _submit_headless(
|
||||||
)
|
)
|
||||||
login_error = get_login_error()
|
login_error = get_login_error()
|
||||||
if login_error:
|
if login_error:
|
||||||
return SubmitResult(success=False, error=f"Login failed: {login_error}")
|
return SubmitResult(
|
||||||
|
success=False, error=f"Login failed: {login_error}"
|
||||||
|
)
|
||||||
logged_in = True
|
logged_in = True
|
||||||
try:
|
try:
|
||||||
browser_cookies = session.context.cookies()
|
browser_cookies = session.context.cookies()
|
||||||
|
|
@ -577,13 +592,20 @@ def _submit_headless(
|
||||||
if needs_relogin and not _retried:
|
if needs_relogin and not _retried:
|
||||||
clear_platform_cookies("atcoder")
|
clear_platform_cookies("atcoder")
|
||||||
return _submit_headless(
|
return _submit_headless(
|
||||||
contest_id, problem_id, file_path, language_id, credentials, _retried=True
|
contest_id,
|
||||||
|
problem_id,
|
||||||
|
file_path,
|
||||||
|
language_id,
|
||||||
|
credentials,
|
||||||
|
_retried=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
if submit_error:
|
if submit_error:
|
||||||
return SubmitResult(success=False, error=submit_error)
|
return SubmitResult(success=False, error=submit_error)
|
||||||
|
|
||||||
return SubmitResult(success=True, error="", submission_id="", verdict="submitted")
|
return SubmitResult(
|
||||||
|
success=True, error="", submission_id="", verdict="submitted"
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return SubmitResult(success=False, error=str(e))
|
return SubmitResult(success=False, error=str(e))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,16 @@ from abc import ABC, abstractmethod
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from .language_ids import get_language_id
|
||||||
|
from .models import (
|
||||||
|
CombinedTest,
|
||||||
|
ContestListResult,
|
||||||
|
LoginResult,
|
||||||
|
MetadataResult,
|
||||||
|
SubmitResult,
|
||||||
|
TestsResult,
|
||||||
|
)
|
||||||
|
|
||||||
_COOKIE_FILE = Path.home() / ".cache" / "cp-nvim" / "cookies.json"
|
_COOKIE_FILE = Path.home() / ".cache" / "cp-nvim" / "cookies.json"
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -36,17 +46,6 @@ def clear_platform_cookies(platform: str) -> None:
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
from .language_ids import get_language_id
|
|
||||||
from .models import (
|
|
||||||
CombinedTest,
|
|
||||||
ContestListResult,
|
|
||||||
LoginResult,
|
|
||||||
MetadataResult,
|
|
||||||
SubmitResult,
|
|
||||||
TestsResult,
|
|
||||||
)
|
|
||||||
|
|
||||||
_PRECISION_ABS_REL_RE = re.compile(
|
_PRECISION_ABS_REL_RE = re.compile(
|
||||||
r"(?:absolute|relative)\s+error[^.]*?10\s*[\^{]\s*\{?\s*[-\u2212]\s*(\d+)\s*\}?",
|
r"(?:absolute|relative)\s+error[^.]*?10\s*[\^{]\s*\{?\s*[-\u2212]\s*(\d+)\s*\}?",
|
||||||
re.IGNORECASE,
|
re.IGNORECASE,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue