-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode.py
More file actions
197 lines (165 loc) · 5.57 KB
/
leetcode.py
File metadata and controls
197 lines (165 loc) · 5.57 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
import requests
# URL to LeetCode.com
originUrl = "https://leetcode.com"
# URL to LeetCode's GraphQL interface
endPoint = "https://leetcode.com/graphql"
# Request object with Session maintained
session = requests.Session()
# Cookies for User Session
cookies = {}
# Add/Update Session token to Cookie Object
def setSessionToken(token):
cookies['LEETCODE_SESSION'] = token
# Lookup the problem from LeetCode Server to find Problem Data
def searchProblem(query):
gqlquery = '''
query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput){
problemsetQuestionList: questionList(
categorySlug: $categorySlug
limit: $limit
skip: $skip
filters: $filters
) {
total: totalNum
questions: data {
frontendQuestionId: questionFrontendId
title
titleSlug
}
}
}
'''
variables = {
"categorySlug": "",
"skip": 0,
"limit": 1,
"filters": {
"searchKeywords": str(query)
}
}
r = session.post(endPoint, json={"query": gqlquery, "variables": variables}, cookies=cookies)
if r.status_code == 200:
result = r.json()
if 'errors' not in result and result['data']:
result = result['data']
if result['problemsetQuestionList']:
result = result['problemsetQuestionList']
if result['total'] > 0:
return result['questions'][0]
else:
print(f"Problem Search Query failed to run with a {r.status_code}.")
exit(0)
return None
# Fetch problem details using titleslug from LeetCode Server
def getProblemDetails(title_slug):
gqlquery = '''
query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionFrontendId
title
titleSlug
content
isPaidOnly
difficulty
likes
dislikes
topicTags {
name
slug
}
codeSnippets {
lang
langSlug
code
}
}
}
'''
variables = {
"titleSlug": str(title_slug)
}
r = session.post(endPoint, json={"query": gqlquery, "variables": variables}, cookies=cookies)
if r.status_code == 200:
result = r.json()
if 'errors' not in result and result['data']:
result = result['data']
if result['question']:
return result['question']
else:
print(f"Problem Details Query failed to run with a {r.status_code}.")
exit(0)
return None
# Fetch User Status with Session from LeetCode Server
def getUserStatus():
gqlquery = '''
query globalData {
userStatus {
isSignedIn
isPremium
username
realName
}
}
'''
variables = {}
r = session.post(endPoint, json={"query": gqlquery, "variables": variables}, cookies=cookies)
if r.status_code == 200:
result = r.json()
if 'errors' not in result and result['data']:
result = result['data']
if result['userStatus']:
return result['userStatus']
else:
print(f"User Status Query failed to run with a {r.status_code}.")
exit(0)
return None
# Fetch User's Submissions with Session from LeetCode Server
def getSubmissionsList(title_slug):
gqlquery = '''
query Submissions($offset: Int!, $limit: Int!, $lastKey: String, $questionSlug: String!) {
submissionList(offset: $offset, limit: $limit, lastKey: $lastKey, questionSlug: $questionSlug) {
submissions {
statusDisplay
lang
url
}
}
}
'''
variables = {
"offset": 0,
"limit": 20,
"lastKey": None,
"questionSlug": str(title_slug)
}
r = session.post(endPoint, json={"query": gqlquery, "variables": variables}, cookies=cookies)
if r.status_code == 200:
result = r.json()
if 'errors' not in result and result['data']:
result = result['data']
if result['submissionList']:
result = result['submissionList']
if result['submissions']:
# Only Accepted Solutions are needed
return [x for x in result['submissions'] if x['statusDisplay'] == "Accepted"]
else:
print(f"Submissions List Query failed to run with a {r.status_code}.")
exit(0)
return None
# Fetch User's Submitted Code with Session from LeetCode Server
def getSubmittedCode(sub_url):
r = session.get(originUrl + sub_url, cookies=cookies)
if r.status_code == 200:
endingWords = "editCodeUrl: '"
startingWords = "submissionCode: '"
# Search through HTML
submissionCode = r.text[r.text.find(startingWords):r.text.find(endingWords)].strip() \
.replace(startingWords, '') \
.replace("',", '')
# Unescape Unicode Characters
submissionCode = submissionCode.encode().decode('unicode-escape')
return submissionCode
else:
print(f"Submitted code fetch failed to run with a {r.status_code}.")
exit(0)
return None