Skip to content

Commit 80a7cc0

Browse files
committed
feat(sync): implement submission state tracking
1 parent 50e1d09 commit 80a7cc0

2 files changed

Lines changed: 68 additions & 15 deletions

File tree

config/settings.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
from pathlib import Path
2-
from dotenv import load_dotenv
32
import os
43

5-
load_dotenv()
4+
from dotenv import load_dotenv
5+
6+
BASE_DIR = Path(__file__).resolve().parent.parent
7+
load_dotenv(BASE_DIR / ".env")
8+
69

710
class Config:
11+
BASE_DIR = BASE_DIR
12+
13+
BASE_URL = "https://leetcode.com"
14+
GRAPHQL_URL = f"{BASE_URL}/graphql"
815

916
LEETCODE_SESSION = os.getenv("LEETCODE_SESSION", "")
1017
LEETCODE_USERNAME = os.getenv("LEETCODE_USERNAME", "")
@@ -13,16 +20,35 @@ class Config:
1320
GITHUB_USERNAME = os.getenv("GITHUB_USERNAME", "")
1421
GITHUB_EMAIL = os.getenv("GITHUB_EMAIL", "")
1522

16-
STORAGE_DIR = Path(os.getenv("STORAGE_DIR", "storage"))
17-
LOGS_DIR = Path(os.getenv("LOGS_DIR", "logs"))
18-
19-
GRAPHQL_URL = os.getenv("GRAPHQL_URL", "https://leetcode.com/graphql")
23+
STORAGE_DIR = BASE_DIR / os.getenv("STORAGE_DIR", "storage")
24+
LOGS_DIR = BASE_DIR / os.getenv("LOGS_DIR", "logs")
2025

21-
REQUEST_TIMEOUT = 20
26+
REQUEST_TIMEOUT: int = 20
2227

2328
USER_AGENT = (
2429
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
25-
"AppleWebKit/537.36 "
26-
"(KHTML, like Gecko) "
30+
"AppleWebKit/537.36 (KHTML, like Gecko) "
2731
"Chrome/137.0 Safari/537.36"
28-
)
32+
)
33+
34+
@classmethod
35+
def initialize(cls) -> None:
36+
cls.STORAGE_DIR.mkdir(parents=True, exist_ok=True)
37+
cls.LOGS_DIR.mkdir(parents=True, exist_ok=True)
38+
39+
(cls.STORAGE_DIR / "cache").mkdir(exist_ok=True)
40+
(cls.STORAGE_DIR / "submissions").mkdir(exist_ok=True)
41+
42+
@classmethod
43+
def validate(cls):
44+
required = (
45+
"LEETCODE_SESSION",
46+
"LEETCODE_USERNAME",
47+
"CSRF_TOKEN",
48+
)
49+
50+
for variable in required:
51+
if not getattr(cls, variable):
52+
raise ValueError(f"Missing environment variable: {variable}")
53+
54+

main.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
from rich.panel import Panel
33
from rich.table import Table
44

5+
from sync.detector import SubmissionDetector
6+
7+
58
from leetcode.auth import LeetCodeAuth
69
from leetcode.client import LeetCodeClient
710
from leetcode.api import LeetCodeAPI
11+
from config.settings import Config
12+
813

914
console = Console()
1015

@@ -56,13 +61,15 @@ def display_recent_submissions(submissions):
5661

5762

5863
def main():
59-
banner()
64+
Config.validate()
65+
Config.initialize()
6066

61-
auth = LeetCodeAuth()
62-
session = auth.get_session()
67+
banner()
6368

64-
client = LeetCodeClient(session)
65-
api = LeetCodeAPI(client)
69+
session = LeetCodeAuth().get_session()
70+
api = LeetCodeAPI(
71+
LeetCodeClient(session)
72+
)
6673

6774
# Profile
6875
profile = api.get_profile()
@@ -80,8 +87,28 @@ def main():
8087
# Recent submissions
8188
submissions = api.get_recent_submissions()
8289

90+
91+
detector = SubmissionDetector()
92+
93+
new_submissions = detector.find_new(submissions)
94+
95+
if new_submissions:
96+
console.print(
97+
f"\n[bold green]Found {len(new_submissions)} new submission(s).[/bold green]\n"
98+
)
99+
else:
100+
console.print(
101+
"\n[bold blue]No new submissions found.[/bold blue]\n"
102+
)
103+
104+
for submission in new_submissions:
105+
console.print(f"• {submission.title}")
106+
83107
display_recent_submissions(submissions)
84108

109+
if new_submissions:
110+
detector.update(new_submissions[0])
111+
85112

86113
if __name__ == "__main__":
87114
main()

0 commit comments

Comments
 (0)