Skip to content

Commit 626278d

Browse files
committed
added a bit more robustness to make sure this script runs from anywhere, stops when it hits an error, validationerror is handled, etc. and updated the data
1 parent f3c0d4c commit 626278d

File tree

5 files changed

+6967
-5507
lines changed

5 files changed

+6967
-5507
lines changed

api_leet.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@
3232
# TODO: call with current username
3333

3434
import json
35+
import os
36+
import sys
3537
from pathlib import Path
3638
from typing import Any, Dict
3739

3840
import pandas as pd
3941
import requests
4042
from model import Model
43+
from pydantic_core import ValidationError
4144
from requests import Response
4245

46+
os.chdir(os.path.dirname(os.path.abspath(__file__)))
4347
url = "https://leetcode.com/api/problems/algorithms/"
4448
ratings_url = "https://raw.githubusercontent.com/zerotrac/leetcode_problem_rating/main/ratings.txt"
4549
data_dir: Path = Path.cwd() / "data"
@@ -76,15 +80,20 @@
7680
clean_file.writelines(clean_lines)
7781
except requests.RequestException as e:
7882
print(f"Error fetching ratings data: {e}")
79-
83+
sys.exit(1)
8084

8185
try:
8286
response: Response = requests.get(url, headers=headers, timeout=5)
8387
data: Model = Model.model_validate_json(response.text)
8488
data_dict: Dict[str, Any] = data.model_dump()
85-
except (requests.RequestException, json.JSONDecodeError, AttributeError) as e:
89+
except (
90+
requests.RequestException,
91+
json.JSONDecodeError,
92+
AttributeError,
93+
ValidationError,
94+
) as e:
8695
print(f"Error fetching problem data: {e}")
87-
data_dict = {}
96+
sys.exit(1)
8897

8998
try:
9099
df1: pd.DataFrame = pd.json_normalize(data_dict, record_path=["stat_status_pairs"])
@@ -130,7 +139,7 @@
130139
df2.to_csv(problem_list_path, index=False)
131140
except KeyError as e:
132141
print(f"Error processing problem data: {e}")
133-
142+
sys.exit(1)
134143

135144
try:
136145
df3: pd.DataFrame = pd.read_csv(clean_ratings_path)
@@ -143,3 +152,4 @@
143152
df4.to_csv(data_output_path, index=False)
144153
except (FileNotFoundError, pd.errors.EmptyDataError) as e:
145154
print(f"Error merging data: {e}")
155+
sys.exit(1)

0 commit comments

Comments
 (0)