Skip to content

Commit 7c3c380

Browse files
feat: profile details
1 parent 535709f commit 7c3c380

File tree

7 files changed

+103
-49
lines changed

7 files changed

+103
-49
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
typer
2-
requests
2+
requests
3+
gql[all]

src/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .commands.show import show
2-
from .commands.list import list
2+
from .commands.details import details
33

4-
__all__ = ['show', 'list']
4+
__all__ = ['show', 'details']

src/commands/details.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from server.api import fetch_problem_list
2+
from lib.ui import display_problem_stats
3+
from rich.spinner import Spinner
4+
from rich.live import Live
5+
6+
def details():
7+
"""List all available LeetCode problems."""
8+
spinner = Spinner('dots')
9+
with Live(spinner, refresh_per_second=10, transient=True) as live:
10+
live.console.print("[cyan]Fetching problem list...")
11+
data = fetch_problem_list()
12+
display_problem_stats(data)

src/commands/list.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/lib/ui.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from rich.console import Console
2+
from rich.table import Table
3+
4+
console = Console()
5+
6+
def create_problems_table(data):
7+
table = Table(title="LeetCode Problems Count")
8+
table.add_column("Difficulty", style="cyan")
9+
table.add_column("Count", justify="right", style="green")
10+
11+
for item in data['allQuestionsCount']:
12+
table.add_row(item['difficulty'], str(item['count']))
13+
return table
14+
15+
def create_user_stats_table(data):
16+
if not data.get('matchedUser'):
17+
return None
18+
19+
table = Table(title="Your Statistics")
20+
table.add_column("Difficulty", style="cyan")
21+
table.add_column("Solved", justify="right", style="green")
22+
table.add_column("Beats", justify="right", style="yellow")
23+
24+
user_data = data['matchedUser']
25+
solved = user_data['submitStatsGlobal']['acSubmissionNum']
26+
beats = user_data['problemsSolvedBeatsStats']
27+
28+
difficulties = ['Easy', 'Medium', 'Hard']
29+
for diff in difficulties:
30+
solved_count = next((item['count'] for item in solved if item['difficulty'] == diff), 0)
31+
beats_percent = next((f"{item['percentage']:.1f}%" for item in beats if item['difficulty'] == diff), "N/A")
32+
table.add_row(diff, str(solved_count), beats_percent)
33+
34+
return table
35+
36+
def display_problem_stats(data):
37+
"""Display problem statistics in a formatted way."""
38+
39+
problems_table = create_problems_table(data)
40+
console.print(problems_table)
41+
42+
user_stats = create_user_stats_table(data)
43+
if user_stats:
44+
console.print("\n")
45+
console.print(user_stats)

src/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import typer
22
from commands.show import show
3-
from commands.list import list
3+
from src.commands.details import details
44

55
app = typer.Typer()
66

77
app.command()(show)
8-
app.command()(list)
8+
app.command()(details)
99

1010
if __name__ == "__main__":
1111
app()

src/server/api.py

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
1-
import requests
2-
from .config import BASE_URL
1+
from gql import gql, Client
2+
from gql.transport.requests import RequestsHTTPTransport
33

4-
def fetch_problem_list():
5-
"""Fetches a list of LeetCode problems from the API."""
6-
query = {
7-
"query": """
8-
query {
9-
problemsetQuestionListV2(
10-
categorySlug: ""
11-
limit: 10
12-
skip: 0
13-
) {
14-
questions {
15-
questionFrontendId
16-
title
17-
difficulty
18-
topicTags {
19-
name
20-
}
21-
}
22-
}
23-
}
24-
"""
25-
}
4+
transport = RequestsHTTPTransport(
5+
url='https://leetcode.com/graphql',
6+
headers={'Content-Type': 'application/json'},
7+
)
268

27-
response = requests.post(BASE_URL, json=query)
28-
data = response.json()
9+
client = Client(
10+
transport=transport,
11+
fetch_schema_from_transport=False
12+
)
2913

30-
if "errors" in data:
31-
print("Error fetching problems:", data["errors"])
32-
return []
14+
def fetch_problem_list(username: str = "yuvrajsinh5252"):
15+
variables = {"username": username}
16+
query = gql(
17+
"""
18+
query userProblemsSolved($username: String!) {
19+
allQuestionsCount {
20+
difficulty
21+
count
22+
}
23+
matchedUser(username: $username) {
24+
problemsSolvedBeatsStats {
25+
difficulty
26+
percentage
27+
}
28+
submitStatsGlobal {
29+
acSubmissionNum {
30+
difficulty
31+
count
32+
}
33+
}
34+
}
35+
}
36+
"""
37+
)
3338

34-
return data["data"]["problemsetQuestionList"]["questions"]
39+
try:
40+
result = client.execute(query, variable_values=variables)
41+
return result
42+
except Exception as e:
43+
print(f"Error fetching data: {str(e)}")
44+
return None

0 commit comments

Comments
 (0)