Skip to content

Commit

Permalink
Merge pull request #155 from wimglenn/http
Browse files Browse the repository at this point in the history
prevent excessive checking of puzzle page for pre-existing answers
  • Loading branch information
wimglenn authored Dec 25, 2024
2 parents eed1caf + 9af6616 commit 143461e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
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 @@ def get_stats(
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 @@ def submit_results(self) -> list[Submission]:
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 @@ def _submit(self, value, part, reopen=True, quiet=False):
"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)
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 @@ def _get_answer(self, part):
# 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 @@ class HttpClient:
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)
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 @@ def get_owner(token: str) -> str:
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

0 comments on commit 143461e

Please sign in to comment.