Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent excessive checking of puzzle page for pre-existing answers #155

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions aocd/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
ur_broke = "You haven't collected any stars"
for year in years:
url = f"https://adventofcode.com/{year}/leaderboard/self"
log.debug("requesting personal stats for year %d", year)
response = http.get(url, token=self.token, redirect=False)
if 300 <= response.status < 400:
# expired tokens 302 redirect to the overall leaderboard
Expand Down Expand Up @@ -456,7 +457,7 @@
return json.loads(self.submit_results_path.read_text())
return []

def _submit(self, value, part, reopen=True, quiet=False):
def _submit(self, value, part, reopen=True, quiet=False, precheck=True):
# actual submit logic. not meant to be invoked directly - users are expected
# to use aocd.post.submit function, puzzle answer setters, or the aoc.runner
# which autosubmits answers by default.
Expand Down Expand Up @@ -541,13 +542,14 @@
"because that was the answer for part a"
)
url = self.submit_url
check_guess = self._check_already_solved(value, part)
if check_guess is not None:
if quiet:
log.info(check_guess)
else:
print(check_guess)
return
if precheck:
check_guess = self._check_already_solved(value, part)
if check_guess is not None:
if quiet:
log.info(check_guess)

Check warning on line 549 in aocd/models.py

View check run for this annotation

Codecov / codecov/patch

aocd/models.py#L549

Added line #L549 was not covered by tests
else:
print(check_guess)
return
sanitized = "..." + self.user.token[-4:]
log.info("posting %r to %s (part %s) token=%s", value, url, part, sanitized)
level = {"a": "1", "b": "2"}[part]
Expand Down Expand Up @@ -681,6 +683,7 @@
# check puzzle page for any previously solved answers.
# if these were solved by typing into the website directly, rather than using
# aocd submit, then our caches might not know about the answers yet.
log.debug(f"check page {self.year}/{self.day:02d} for existing answer ({part})")
self._request_puzzle_page()
if answer_path.is_file():
return answer_path.read_text(encoding="utf-8").strip()
Expand Down
14 changes: 10 additions & 4 deletions aocd/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,19 @@ def run_for(
post = part == "a" or (part == "b" and puzzle.answered_a)
if autosubmit and post:
try:
puzzle._submit(answer, part, reopen=reopen, quiet=True)
puzzle._submit(
value=answer,
part=part,
reopen=reopen,
quiet=True,
precheck=False,
)
except AocdError as err:
log.warning("error submitting - %s", err)
try:
# Correct submission will have created the answer file
answer_path = getattr(puzzle, f"answer_{part}_path")
if answer_path.is_file():
expected = getattr(puzzle, "answer_" + part)
except AttributeError:
pass
correct = expected is not None and str(expected) == answer
icon = colored("✔", "green") if correct else colored("✖", "red")
correction = ""
Expand Down
8 changes: 5 additions & 3 deletions aocd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@
req_count: dict[t.Literal["GET", "POST"], int]

def __init__(self) -> None:
proxy_url = os.environ.get('http_proxy') or os.environ.get('https_proxy')
proxy_url = os.environ.get("http_proxy") or os.environ.get("https_proxy")
headers = {"User-Agent": USER_AGENT}
if proxy_url:
self.pool_manager = urllib3.ProxyManager(proxy_url, headers={"User-Agent": USER_AGENT})
self.pool_manager = urllib3.ProxyManager(proxy_url, headers=headers)

Check warning on line 53 in aocd/utils.py

View check run for this annotation

Codecov / codecov/patch

aocd/utils.py#L53

Added line #L53 was not covered by tests
else:
self.pool_manager = urllib3.PoolManager(headers={"User-Agent": USER_AGENT})
self.pool_manager = urllib3.PoolManager(headers=headers)
self.req_count = {"GET": 0, "POST": 0}
self._max_t = 3.0
self._cooloff = 0.16
Expand Down Expand Up @@ -173,6 +174,7 @@
Returns a string like "authtype.username.userid"
"""
url = "https://adventofcode.com/settings"
log.debug(f"attempting to determine owner of token ...{token[-4:]}")
response = http.get(url, token=token, redirect=False)
if response.status != 200:
# bad tokens will 302 redirect to main page
Expand Down
Loading