-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_graphql.py
More file actions
290 lines (256 loc) · 9.08 KB
/
leetcode_graphql.py
File metadata and controls
290 lines (256 loc) · 9.08 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# leetcode_graphql.py
import httpx
import logging
from typing import Dict, Optional, List, Tuple
from datetime import datetime, timezone
from models import CalendarStats, CalendarStreak, Badge
import json
import asyncio
logger = logging.getLogger('devquest.leetcode.graphql')
class LeetCodeGraphQLService:
def __init__(self):
self.graphql_url = "https://leetcode.com/graphql/"
async def _make_graphql_request(
self,
query: str,
variables: Dict,
headers: Dict
) -> Optional[Dict]:
try:
async with httpx.AsyncClient() as client:
response = await client.post(
self.graphql_url,
json={
"query": query,
"variables": variables
},
headers=headers
)
response.raise_for_status()
data = response.json()
if "errors" in data:
logger.error(f"GraphQL errors: {data['errors']}")
return None
return data.get("data", {})
except Exception as e:
logger.error(f"GraphQL request failed: {str(e)}", exc_info=True)
return None
async def fetch_user_tag_stats(
self,
username: str,
csrf_token: str,
cookie: str
) -> Optional[Dict]:
"""Fetch user's tag statistics"""
query = """
query skillStats($username: String!) {
matchedUser(username: $username) {
tagProblemCounts {
advanced { tagName tagSlug problemsSolved }
intermediate { tagName tagSlug problemsSolved }
fundamental { tagName tagSlug problemsSolved }
}
}
}
"""
headers = {
"Content-Type": "application/json",
"x-csrftoken": csrf_token,
"Cookie": cookie,
"Referer": "https://leetcode.com"
}
data = await self._make_graphql_request(query, {"username": username}, headers)
return data.get("matchedUser", {}).get("tagProblemCounts", {})
async def fetch_calendar_stats(
self,
username: str,
csrf_token: str,
cookie: str
) -> Optional[Dict]:
"""Fetch user's submission calendar"""
query = """
query userProfileCalendar($username: String!, $year: Int) {
matchedUser(username: $username) {
userCalendar(year: $year) {
activeYears
streak
totalActiveDays
dccBadges {
timestamp
badge { name icon }
}
submissionCalendar
}
}
}
"""
headers = {
"Content-Type": "application/json",
"x-csrftoken": csrf_token,
"Cookie": cookie,
"Referer": "https://leetcode.com"
}
current_year = datetime.now().year
data = await self._make_graphql_request(query, {
"username": username,
"year": current_year
}, headers)
return data.get("matchedUser", {}).get("userCalendar", {})
def process_calendar_data(self, calendar_data: Dict) -> CalendarStats:
"""Process raw calendar data into structured format"""
if not calendar_data:
return CalendarStats()
# Parse submission calendar
submissions_calendar = json.loads(
calendar_data.get("submissionCalendar", "{}")
)
# Convert Unix timestamps to YYYY-MM-DD format and organize data
submissions_by_date = {}
monthly_submissions = {}
yearly_submissions = {}
for timestamp_str, count in submissions_calendar.items():
# Convert timestamp to datetime
timestamp = int(timestamp_str)
date = datetime.fromtimestamp(timestamp, tz=timezone.utc)
# Store daily data
date_str = date.strftime("%Y-%m-%d")
submissions_by_date[date_str] = count
# Update monthly aggregates
month_key = date.strftime("%Y-%m")
monthly_submissions[month_key] = (
monthly_submissions.get(month_key, 0) + count
)
# Update yearly aggregates
year_key = date.strftime("%Y")
yearly_submissions[year_key] = (
yearly_submissions.get(year_key, 0) + count
)
return CalendarStats(
active_years=calendar_data.get("activeYears", []),
total_active_days=calendar_data.get("totalActiveDays", 0),
streak=calendar_data.get("streak", 0),
submissions_by_date=submissions_by_date,
monthly_submissions=monthly_submissions,
yearly_submissions=yearly_submissions,
streaks=CalendarStreak(
current=calendar_data.get("streak", 0),
longest=calendar_data.get("streak", 0) # You might want to calculate this separately
)
)
async def fetch_language_stats(
self,
username: str,
csrf_token: str,
cookie: str
) -> Optional[List[Dict]]:
"""Fetch user's language statistics"""
query = """
query languageStats($username: String!) {
matchedUser(username: $username) {
languageProblemCount {
languageName
problemsSolved
}
}
}
"""
headers = {
"Content-Type": "application/json",
"x-csrftoken": csrf_token,
"Cookie": cookie,
"Referer": "https://leetcode.com"
}
data = await self._make_graphql_request(query, {"username": username}, headers)
return data.get("matchedUser", {}).get("languageProblemCount", [])
async def fetch_problem_stats(
self,
username: str,
csrf_token: str,
cookie: str
) -> Optional[Dict]:
"""Fetch user's problem solving statistics"""
query = """
query userProblemsSolved($username: String!) {
allQuestionsCount {
difficulty
count
}
matchedUser(username: $username) {
problemsSolvedBeatsStats {
difficulty
percentage
}
submitStatsGlobal {
acSubmissionNum {
difficulty
count
}
}
}
}
"""
headers = {
"Content-Type": "application/json",
"x-csrftoken": csrf_token,
"Cookie": cookie,
"Referer": "https://leetcode.com"
}
return await self._make_graphql_request(query, {"username": username}, headers)
async def fetch_user_badges(
self,
username: str,
csrf_token: str,
cookie: str
) -> Optional[Dict]:
"""Fetch user's active badge"""
query = """
query getUserProfile($username: String!) {
matchedUser(username: $username) {
activeBadge {
displayName
icon
}
}
}
"""
headers = {
"Content-Type": "application/json",
"x-csrftoken": csrf_token,
"Cookie": cookie,
"Referer": "https://leetcode.com"
}
data = await self._make_graphql_request(query, {"username": username}, headers)
return data.get("matchedUser", {}).get("activeBadge") if data else None
def process_badge_data(self, badge_data: Optional[Dict]) -> List[Badge]:
"""Process badge data into structured format"""
badges = []
if badge_data:
badges.append(Badge(
display_name=badge_data.get("displayName", ""),
icon_url=badge_data.get("icon", "")
))
return badges
async def fetch_all_stats(
self,
username: str,
csrf_token: str,
cookie: str
) -> Tuple[Optional[Dict], Optional[List[Dict]], Optional[Dict], Optional[Dict], Optional[Dict]]:
"""Fetch all statistics in parallel"""
tag_stats_task = self.fetch_user_tag_stats(username, csrf_token, cookie)
language_stats_task = self.fetch_language_stats(username, csrf_token, cookie)
problem_stats_task = self.fetch_problem_stats(username, csrf_token, cookie)
calendar_stats_task = self.fetch_calendar_stats(username, csrf_token, cookie)
badge_stats_task = self.fetch_user_badges(username, csrf_token, cookie)
results = await asyncio.gather(
tag_stats_task,
language_stats_task,
problem_stats_task,
calendar_stats_task,
badge_stats_task,
return_exceptions=True
)
return tuple(
None if isinstance(result, Exception) else result
for result in results
)