Skip to content

Commit ed42453

Browse files
feat: add CAPTCHA detection for submissions (#311)
* feat: add CAPTCHA detection for submissions 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Fix styles --------- Co-authored-by: Claude <[email protected]>
1 parent 7e2be6e commit ed42453

File tree

5 files changed

+304
-13
lines changed

5 files changed

+304
-13
lines changed

atcodertools/client/atcoder.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class PageNotFoundError(Exception):
2525
pass
2626

2727

28+
class CaptchaError(Exception):
29+
pass
30+
31+
2832
default_cookie_path = get_cache_file_path('cookie.txt')
2933

3034

@@ -167,6 +171,10 @@ def submit_source_code(self, contest: Contest, problem: Problem, lang: Union[str
167171

168172
resp = self._request(contest.get_submit_url())
169173

174+
if "https://challenges.cloudflare.com" in resp.text:
175+
raise CaptchaError(
176+
"CAPTCHA detected. Cannot submit automatically. Please submit manually through the web interface.")
177+
170178
soup = BeautifulSoup(resp.text, "html.parser")
171179
session_id = soup.find("input", attrs={"type": "hidden"}).get("value")
172180
task_select_area = soup.find(

atcodertools/tools/submit.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from atcodertools.tools.tester import USER_FACING_JUDGE_TYPE_LIST, DEFAULT_EPS
99
from atcodertools.tools.utils import with_color
1010

11-
from atcodertools.client.atcoder import AtCoderClient, LoginError
11+
from atcodertools.client.atcoder import AtCoderClient, LoginError, CaptchaError
1212
from atcodertools.tools import tester
1313
from atcodertools.common.logging import logger
1414
from atcodertools.config.config import Config, ConfigType, USER_CONFIG_PATH
@@ -170,11 +170,15 @@ def main(prog, args, credential_supplier=None, use_local_session_cache=True, cli
170170
logger.warning("code wasn't recognized as {}".format(encoding))
171171
logger.info(
172172
"Submitting {} as {}".format(code_path, metadata.lang.name))
173-
submission = client.submit_source_code(
174-
metadata.problem.contest, metadata.problem, metadata.lang, source)
175-
logger.info("{} {}".format(
176-
with_color("Done!", Fore.LIGHTGREEN_EX),
177-
metadata.problem.contest.get_submissions_url(submission)))
173+
try:
174+
submission = client.submit_source_code(
175+
metadata.problem.contest, metadata.problem, metadata.lang, source)
176+
logger.info("{} {}".format(
177+
with_color("Done!", Fore.LIGHTGREEN_EX),
178+
metadata.problem.contest.get_submissions_url(submission)))
179+
except CaptchaError as e:
180+
logger.error(with_color(str(e), Fore.LIGHTRED_EX))
181+
return False
178182
if config.submit_config.exec_after_submit:
179183
run_command(config.submit_config.exec_after_submit, args.dir)
180184

tests/resources/test_atcoder_client_mock/submit/after_get.html

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,12 +1084,6 @@ <h2>提出</h2>
10841084
<div class="form-group">
10851085
<label class="control-label col-sm-3 col-md-2"></label>
10861086
<div class="col-sm-5">
1087-
1088-
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
1089-
1090-
<div class="cf-challenge" data-sitekey="0x4AAAAAAA6HJUmmLP7mLxx0" data-theme="light"></div>
1091-
1092-
10931087
<button id="submit" type="submit" class="btn btn-primary">提出</button>
10941088
</div>
10951089
</div>

0 commit comments

Comments
 (0)