Skip to content

Commit 67e1590

Browse files
committed
feat: get problems by contest
add contest selection and fetching functionality
1 parent 726cc52 commit 67e1590

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

python/lc_libs/rating.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
def _update_rating(data_path: Path) -> None:
1111
response = None
1212
try:
13-
response = requests.get(constant.RATING_URL_CN, timeout=10)
13+
response = requests.get(constant.RATING_URL_CN, timeout=5)
1414
except requests.exceptions.Timeout:
1515
logging.debug("Timeout for rating data from %s, try to get from %s",
1616
constant.RATING_URL_CN, constant.RATING_URL)
1717
if not response or response.status_code != 200:
18-
response = requests.get(constant.RATING_URL, timeout=30)
18+
response = requests.get(constant.RATING_URL, timeout=5)
1919
response.raise_for_status()
2020

2121
with data_path.open("w", encoding="utf-8") as f:

python/scripts/leetcode.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
3. Random
5151
4. Random remain [Problems that submitted but not accepted yet]
5252
5. Category
53+
6. Contest
5354
"""
5455
__user_input_submit = """Please select the submit method [0-4, default: 0]:
5556
0. Back
@@ -72,6 +73,13 @@
7273
b. last page
7374
n. next page
7475
"""
76+
__user_input_contest_type = """Please select the contest type [0-2, default: 0]:
77+
0. Back
78+
1. Weekly contest
79+
2. Biweekly contest
80+
"""
81+
__user_input_contest_id_num = """Enter the contest ID number (e.g. 1, 2, etc.): """
82+
7583
__user_input_favorite_method = """Please select the favorite method [0-2, default: 0]:
7684
0. Back
7785
1. List problems in the favorite
@@ -85,6 +93,7 @@
8593

8694
__allow_all = lambda x: True
8795
__allow_all_not_empty = lambda x: bool(x.strip())
96+
__allow_number = lambda x: bool(re.match(r"^\d+$", x))
8897

8998

9099
def input_until_valid(prompt, check_func, error_msg=None):
@@ -283,6 +292,40 @@ def get_problem(languages, problem_folder, cookie):
283292
print(f"Problem [{problem_id}] fetched successfully.")
284293
else:
285294
print(f"Failed to fetch the problem. Check {problem_id} is correct?")
295+
case "6":
296+
contest_type = input_until_valid(
297+
__user_input_contest_type,
298+
lambda x: x in ["0", "1", "2"],
299+
"Invalid input, please enter 1 for weekly contest, 2 for biweekly contest, or 0 to go back."
300+
)
301+
print(__separate_line)
302+
contest_id = input_until_valid(
303+
__user_input_contest_id_num,
304+
__allow_number,
305+
"Invalid input, please enter a number."
306+
)
307+
print(__separate_line)
308+
if contest_type == "0":
309+
return
310+
elif contest_type == "1":
311+
contest_id = f"weekly-contest-{contest_id}"
312+
elif contest_type == "2":
313+
contest_id = f"biweekly-contest-{contest_id}"
314+
else:
315+
print("Invalid contest type, please try again.")
316+
continue
317+
contest_questions = contest_lib.get_contest_info(contest_id)
318+
results = []
319+
with ThreadPoolExecutor(max_workers=max(1, len(contest_questions))) as executor:
320+
for question_data in contest_questions:
321+
results.append(
322+
executor.submit(get_problem_main, problem_slug=question_data["title_slug"], force=True,
323+
cookie=cookie, skip_language=True,
324+
languages=languages, problem_folder=problem_folder))
325+
for future in results:
326+
exit_code = future.result()
327+
if exit_code != 0:
328+
print("Failed to fetch a contest problem. Please check the contest ID and try again.")
286329
case _:
287330
return
288331

0 commit comments

Comments
 (0)