Skip to content

Commit 26a78ed

Browse files
committed
feat: implement LeetCode session authentication.
1 parent 7a9a0e2 commit 26a78ed

4 files changed

Lines changed: 57 additions & 5 deletions

File tree

config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
class Config:
88

9-
Leetcode_session = os.getenv("LEETCODE_SESSION", "")
10-
CSRF_token = os.getenv("CSRF_TOKEN", "")
9+
LEETCODE_SESSION = os.getenv("LEETCODE_SESSION", "")
10+
CSRF_TOKEN = os.getenv("CSRF_TOKEN", "")
1111

1212
GITHUB_USERNAME = os.getenv("GITHUB_USERNAME", "")
1313
GITHUB_EMAIL = os.getenv("GITHUB_EMAIL", "")

leetcode/api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from requests import Session
2+
3+
4+
class LeetCodeAPI:
5+
def __init__(self, session: Session):
6+
self.session = session
7+
8+
def validate_session(self) -> bool:
9+
response = self.session.get("https://leetcode.com/api/problems/all/")
10+
11+
return response.status_code == 200

leetcode/auth.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from requests import Session
2+
3+
from config import Config
4+
5+
6+
class LeetCodeAuth:
7+
8+
def __init__(self):
9+
self.session = Session()
10+
11+
self.session.headers.update(
12+
{
13+
"User-Agent": Config.USER_AGENT,
14+
"Referer": "https://leetcode.com/",
15+
"Origin": "https://leetcode.com",
16+
"Content-Type": "application/json",
17+
}
18+
)
19+
20+
self.session.cookies.update(
21+
{
22+
"LEETCODE_SESSION": Config.LEETCODE_SESSION,
23+
"csrftoken": Config.CSRF_TOKEN,
24+
}
25+
)
26+
27+
def get_session(self) -> Session:
28+
"""Return the configured session."""
29+
return self.session

main.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
from rich.console import Console
22
from rich.panel import Panel
33

4+
from leetcode.auth import LeetCodeAuth
5+
from leetcode.api import LeetCodeAPI
6+
47
console = Console()
8+
9+
510
def banner():
611
console.print(
712
Panel.fit(
813
"[bold cyan]LeetCode Sync[/bold cyan]\n"
914
"Automatic LeetCode → GitHub Synchronization",
10-
title="v0.1.0",
15+
title="v0.2.0",
1116
)
1217
)
18+
19+
1320
def main():
1421
banner()
1522

16-
console.print("[green]Project initialized successfully.[/green]")
17-
console.print("Next milestone: Authenticate with LeetCode.")
23+
auth = LeetCodeAuth()
24+
api = LeetCodeAPI(auth.get_session())
25+
26+
if api.validate_session():
27+
console.print("[green]✓ Session is valid[/green]")
28+
else:
29+
console.print("[red]✗ Invalid session[/red]")
1830

1931

2032
if __name__ == "__main__":

0 commit comments

Comments
 (0)