|
32 | 32 | # TODO: call with current username
|
33 | 33 |
|
34 | 34 | import json
|
| 35 | +import os |
| 36 | +import sys |
35 | 37 | from pathlib import Path
|
36 | 38 | from typing import Any, Dict
|
37 | 39 |
|
38 | 40 | import pandas as pd
|
39 | 41 | import requests
|
40 | 42 | from model import Model
|
| 43 | +from pydantic_core import ValidationError |
41 | 44 | from requests import Response
|
42 | 45 |
|
| 46 | +os.chdir(os.path.dirname(os.path.abspath(__file__))) |
43 | 47 | url = "https://leetcode.com/api/problems/algorithms/"
|
44 | 48 | ratings_url = "https://raw.githubusercontent.com/zerotrac/leetcode_problem_rating/main/ratings.txt"
|
45 | 49 | data_dir: Path = Path.cwd() / "data"
|
|
76 | 80 | clean_file.writelines(clean_lines)
|
77 | 81 | except requests.RequestException as e:
|
78 | 82 | print(f"Error fetching ratings data: {e}")
|
79 |
| - |
| 83 | + sys.exit(1) |
80 | 84 |
|
81 | 85 | try:
|
82 | 86 | response: Response = requests.get(url, headers=headers, timeout=5)
|
83 | 87 | data: Model = Model.model_validate_json(response.text)
|
84 | 88 | 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: |
86 | 95 | print(f"Error fetching problem data: {e}")
|
87 |
| - data_dict = {} |
| 96 | + sys.exit(1) |
88 | 97 |
|
89 | 98 | try:
|
90 | 99 | df1: pd.DataFrame = pd.json_normalize(data_dict, record_path=["stat_status_pairs"])
|
|
130 | 139 | df2.to_csv(problem_list_path, index=False)
|
131 | 140 | except KeyError as e:
|
132 | 141 | print(f"Error processing problem data: {e}")
|
133 |
| - |
| 142 | + sys.exit(1) |
134 | 143 |
|
135 | 144 | try:
|
136 | 145 | df3: pd.DataFrame = pd.read_csv(clean_ratings_path)
|
|
143 | 152 | df4.to_csv(data_output_path, index=False)
|
144 | 153 | except (FileNotFoundError, pd.errors.EmptyDataError) as e:
|
145 | 154 | print(f"Error merging data: {e}")
|
| 155 | + sys.exit(1) |
0 commit comments