Skip to content

Commit c1b1f8c

Browse files
committed
feat(downloader): create problem directory structure
1 parent 6aba022 commit c1b1f8c

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

leetcode/downloader.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from pathlib import Path
2+
3+
from config.settings import Config
4+
5+
6+
class SubmissionDownloader:
7+
"""
8+
Handles downloading and storing LeetCode submissions locally.
9+
"""
10+
11+
def __init__(self):
12+
self.solutions_dir = Config.STORAGE_DIR / "solutions"
13+
14+
# Create storage/solutions if it doesn't exist
15+
self.solutions_dir.mkdir(
16+
parents=True,
17+
exist_ok=True,
18+
)
19+
20+
@staticmethod
21+
def _format_folder_name(question_id: str, slug: str) -> str:
22+
"""
23+
Convert:
24+
295
25+
find-median-from-data-stream
26+
27+
Into:
28+
0295_Find_Median_from_Data_Stream
29+
"""
30+
31+
problem_number = str(question_id).zfill(4)
32+
33+
title = "_".join(
34+
word.capitalize()
35+
for word in slug.split("-")
36+
)
37+
38+
return f"{problem_number}_{title}"
39+
40+
def create_problem_directory(self, detail) -> Path:
41+
"""
42+
Create the directory for a problem if it doesn't already exist.
43+
"""
44+
45+
folder_name = self._format_folder_name(
46+
detail.question_id,
47+
detail.title_slug,
48+
)
49+
50+
problem_dir = self.solutions_dir / folder_name
51+
52+
problem_dir.mkdir(
53+
parents=True,
54+
exist_ok=True,
55+
)
56+
57+
return problem_dir

main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from leetcode.api import LeetCodeAPI
1111
from config.settings import Config
1212

13+
from leetcode.downloader import SubmissionDownloader
14+
1315

1416
console = Console()
1517

@@ -133,5 +135,14 @@ def main():
133135
20: "Compile Error",
134136
}
135137
console.print(f"Status : {STATUS_MAP.get(detail.status_code, 'Unknown')}")
138+
139+
detail = api.get_submission_detail(submissions[0].id)
140+
141+
downloader = SubmissionDownloader()
142+
143+
folder = downloader.create_problem_directory(detail)
144+
145+
console.print(f"\n[green]Directory created:[/green] {folder}")
146+
136147
if __name__ == "__main__":
137148
main()

0 commit comments

Comments
 (0)