Skip to content

Commit 3d04f49

Browse files
Fix crash bug of envgen when getting 404 (#201)
1 parent 8b71b65 commit 3d04f49

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

atcodertools/client/atcoder.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,23 @@
88
import requests
99
from bs4 import BeautifulSoup
1010

11+
from atcodertools.client.models.contest import Contest
12+
from atcodertools.client.models.problem import Problem
13+
from atcodertools.client.models.problem_content import ProblemContent, InputFormatDetectionError, SampleDetectionError
1114
from atcodertools.client.models.submission import Submission
1215
from atcodertools.common.language import Language
1316
from atcodertools.common.logging import logger
1417
from atcodertools.fileutils.artifacts_cache import get_cache_file_path
15-
from atcodertools.client.models.contest import Contest
16-
from atcodertools.client.models.problem import Problem
17-
from atcodertools.client.models.problem_content import ProblemContent, InputFormatDetectionError, SampleDetectionError
1818

1919

2020
class LoginError(Exception):
2121
pass
2222

2323

24+
class PageNotFoundError(Exception):
25+
pass
26+
27+
2428
default_cookie_path = get_cache_file_path('cookie.txt')
2529

2630

@@ -108,6 +112,8 @@ def login(self,
108112
def download_problem_list(self, contest: Contest) -> List[Problem]:
109113
resp = self._request(contest.get_problem_list_url())
110114
soup = BeautifulSoup(resp.text, "html.parser")
115+
if resp.status_code == 404:
116+
raise PageNotFoundError
111117
res = []
112118
for tag in soup.find('table').select('tr')[1::]:
113119
tag = tag.find("a")

atcodertools/tools/envgen.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from colorama import Fore
1313

14-
from atcodertools.client.atcoder import AtCoderClient, Contest, LoginError
14+
from atcodertools.client.atcoder import AtCoderClient, Contest, LoginError, PageNotFoundError
1515
from atcodertools.client.models.problem import Problem
1616
from atcodertools.client.models.problem_content import InputFormatDetectionError, SampleDetectionError
1717
from atcodertools.codegen.code_style_config import DEFAULT_WORKSPACE_DIR_PATH
@@ -169,17 +169,18 @@ def prepare_contest(atcoder_client: AtCoderClient,
169169
retry_max_tries: int = 10):
170170
attempt_count = 1
171171
while True:
172-
problem_list = atcoder_client.download_problem_list(
173-
Contest(contest_id=contest_id))
174-
if problem_list:
172+
try:
173+
problem_list = atcoder_client.download_problem_list(
174+
Contest(contest_id=contest_id))
175175
break
176-
if 0 < retry_max_tries < attempt_count:
177-
raise EnvironmentInitializationError
178-
logger.warning(
179-
"Failed to fetch. Will retry in {} seconds. (Attempt {})".format(retry_delay_secs, attempt_count))
180-
time.sleep(retry_delay_secs)
181-
retry_delay_secs = min(retry_delay_secs * 2, retry_max_delay_secs)
182-
attempt_count += 1
176+
except PageNotFoundError:
177+
if 0 < retry_max_tries < attempt_count:
178+
raise EnvironmentInitializationError
179+
logger.warning(
180+
"Failed to fetch. Will retry in {} seconds. (Attempt {})".format(retry_delay_secs, attempt_count))
181+
time.sleep(retry_delay_secs)
182+
retry_delay_secs = min(retry_delay_secs * 2, retry_max_delay_secs)
183+
attempt_count += 1
183184

184185
tasks = [(atcoder_client,
185186
problem,

0 commit comments

Comments
 (0)