-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_api.py
More file actions
132 lines (118 loc) · 3.91 KB
/
leetcode_api.py
File metadata and controls
132 lines (118 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import requests
from config import LEETCODE_AUTH_COOKIE
from suggest import suggest_questions
def fetch_all_questions():
url = "https://leetcode.com/graphql"
# headers = {
# "Content-Type": "application/json",
# "x-csrftoken": "8sB8s1cBW6WCBRH3UV5NYyJR9Ba6sMqzyXrLBFJwuIUGNYFZlxeOcjLZ1CnQPL0W",
# "Cookie": f"LEETCODE_SESSION={LEETCODE_AUTH_COOKIE}",
# }
query = """
query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) {
problemsetQuestionList: questionList(
categorySlug: $categorySlug
limit: $limit
skip: $skip
filters: $filters
) {
total: totalNum
questions: data {
acRate
difficulty
frontendQuestionId: questionFrontendId
status
title
titleSlug
topicTags {
name
id
slug
}
}
}
}
"""
payload = {
"operationName": "problemsetQuestionList",
"variables": {"categorySlug": "", "skip": 0, "limit": 50, "filters": {}},
"query": query,
}
response = requests.post(url, json=payload)
# print(response.json())
if response.status_code == 200:
return response.json()["data"]["problemsetQuestionList"]["questions"]
else:
raise Exception(
f"Failed to fetch questions: {response.status_code}, {response.text}"
)
def fetch_last_solved_questions(username, limit):
"""
Fetch the solved question slugs of a particular user using LeetCode GraphQL API.
Args:
username (str): The LeetCode username.
offset (int): Pagination offset (default: 0).
limit (int): Number of solved questions to fetch (default: 10).
Returns:
list: A list of question slugs solved by the user.
"""
url = "https://leetcode.com/graphql"
headers = {
"Content-Type": "application/json",
"Cookie": f"LEETCODE_SESSION={LEETCODE_AUTH_COOKIE}",
}
query = """
query recentAcSubmissions($username: String!, $limit: Int!) {
recentAcSubmissionList(username: $username, limit: $limit) {
id
title
titleSlug
timestamp
}
}
"""
payload = {
"operationName": "recentAcSubmissions",
"variables": {"username": username, "limit": limit},
"query": query,
}
response = requests.post(url, json=payload, headers=headers)
print(
"Last solved question response",
response.json()["data"]["recentAcSubmissionList"],
)
if response.status_code == 200:
submissions = response.json()["data"]["recentAcSubmissionList"]
return submissions
else:
raise Exception(
f"Failed to fetch solved questions: {response.status_code}, {response.text}"
)
def suggest_similar_questions(last_solved_data):
"""
Suggest similar questions based on the last solved question.
Args:
last_solved_data (list): List containing the last solved question data.
Returns:
None: Prints suggested questions.
"""
if not last_solved_data:
print("No data found for the last solved question.")
return
# Extract the `titleSlug` of the last solved question
last_question_slug = last_solved_data[0]["titleSlug"]
print(f"Last solved question slug: {last_question_slug}")
# Use the `titleSlug` to query for similar questions
suggestions = suggest_questions(
last_question_slug
) # Function queries the vector database
return suggestions
# Print the suggestions
if suggestions:
print("Here are the suggestions:")
for suggestion in suggestions:
print(
f"- {suggestion['title']} (Difficulty: {suggestion['difficulty']}, Tags: {suggestion['tags']})"
)
else:
print("No similar questions found.")