-
Notifications
You must be signed in to change notification settings - Fork 9
/
example.py
320 lines (280 loc) · 9.74 KB
/
example.py
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from __future__ import annotations
import os
import sys
from time import sleep
import leetcode
import leetcode.auth
# Initialize client
configuration = leetcode.Configuration()
# NOTE: cookies var is just a dict with `csrftoken` and `LEETCODE_SESSION`
# fields which contain corresponding cookies from web browser
leetcode_session: str = os.environ["LEETCODE_SESSION_ID"]
csrf_token: str = leetcode.auth.get_csrf_cookie(leetcode_session)
configuration.api_key["x-csrftoken"] = csrf_token
configuration.api_key["csrftoken"] = csrf_token
configuration.api_key["LEETCODE_SESSION"] = leetcode_session
configuration.api_key["Referer"] = "https://leetcode.com"
configuration.debug = False
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
graphql_request = leetcode.GraphqlQuery(
query="""
{
user {
username
isCurrentUserPremium
}
}
""",
variables=leetcode.GraphqlQueryVariables(),
)
print(api_instance.graphql_post(body=graphql_request))
graphql_request = leetcode.GraphqlQuery(
query="""
query getQuestionDetail($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionId
questionFrontendId
boundTopicId
title
content
translatedTitle
isPaidOnly
difficulty
likes
dislikes
isLiked
similarQuestions
contributors {
username
profileUrl
avatarUrl
__typename
}
langToValidPlayground
topicTags {
name
slug
translatedName
__typename
}
companyTagStats
codeSnippets {
lang
langSlug
code
__typename
}
stats
codeDefinition
hints
solution {
id
canSeeDetail
__typename
}
status
sampleTestCase
enableRunCode
metaData
translatedContent
judgerAvailable
judgeType
mysqlSchemas
enableTestMode
envInfo
__typename
}
}
""",
variables=leetcode.GraphqlQueryGetQuestionDetailVariables(title_slug="two-sum"),
operation_name="getQuestionDetail",
)
print(api_instance.graphql_post(body=graphql_request))
# Get stats
api_response = api_instance.api_problems_topic_get(topic="shell")
print("Stats of this session")
print(api_response)
# Try to test your solution
code = """
class Solution:
def twoSum(self, nums, target):
print("stdout")
return [1]
"""
test_submission = leetcode.TestSubmission(
data_input="[2,7,11,15]\n9",
typed_code=code,
question_id=1,
test_mode=False,
lang="python",
)
interpretation_id = api_instance.problems_problem_interpret_solution_post(
problem="two-sum", body=test_submission
)
print("Test has been queued. Result:")
print(interpretation_id)
sleep(5) # FIXME: should probably be a busy-waiting loop
test_submission_result = api_instance.submissions_detail_id_check_get(
id=interpretation_id.interpret_id
)
print("Got test result:")
print(leetcode.TestSubmissionResult(**test_submission_result))
# Real submission
submission = leetcode.Submission(
judge_type="large", typed_code=code, question_id=1, test_mode=False, lang="python"
)
submission_id = api_instance.problems_problem_submit_post(
problem="two-sum", body=submission
)
print("Submission has been queued. Result:")
print(submission_id)
sleep(5) # FIXME: should probably be a busy-waiting loop
submission_result = api_instance.submissions_detail_id_check_get(
id=submission_id.submission_id
)
print("Got submission result:")
print(leetcode.SubmissionResult(**submission_result))
"""
Example output
Stats of this session
{'ac_easy': 0,
'ac_hard': 0,
'ac_medium': 0,
'category_slug': 'shell',
'frequency_high': 1,
'frequency_mid': 0,
'num_solved': 0,
'num_total': 4,
'stat_status_pairs': [{'difficulty': {'level': 1},
'frequency': 1.287531104481484,
'is_favor': False,
'paid_only': False,
'progress': 38.375356759103795,
'stat': {'frontend_question_id': 195,
'is_new_question': False,
'question__article__has_video_solution': None,
'question__article__live': None,
'question__article__slug': None,
'question__hide': False,
'question__title': 'Tenth Line',
'question__title_slug': 'tenth-line',
'question_id': 195,
'total_acs': 65036,
'total_submitted': 198693},
'status': None},
{'difficulty': {'level': 2},
'frequency': 0.6308040499858097,
'is_favor': False,
'paid_only': False,
'progress': 0.0,
'stat': {'frontend_question_id': 194,
'is_new_question': False,
'question__article__has_video_solution': None,
'question__article__live': None,
'question__article__slug': None,
'question__hide': False,
'question__title': 'Transpose File',
'question__title_slug': 'transpose-file',
'question_id': 194,
'total_acs': 16450,
'total_submitted': 66923},
'status': None},
{'difficulty': {'level': 1},
'frequency': 2.3421289990102343,
'is_favor': False,
'paid_only': False,
'progress': 100.0,
'stat': {'frontend_question_id': 193,
'is_new_question': False,
'question__article__has_video_solution': None,
'question__article__live': None,
'question__article__slug': None,
'question__hide': False,
'question__title': 'Valid Phone Numbers',
'question__title_slug': 'valid-phone-numbers',
'question_id': 193,
'total_acs': 47750,
'total_submitted': 187823},
'status': None},
{'difficulty': {'level': 2},
'frequency': 1.68540194451456,
'is_favor': False,
'paid_only': False,
'progress': 61.62464324089619,
'stat': {'frontend_question_id': 192,
'is_new_question': False,
'question__article__has_video_solution': None,
'question__article__live': None,
'question__article__slug': None,
'question__hide': False,
'question__title': 'Word Frequency',
'question__title_slug': 'word-frequency',
'question_id': 192,
'total_acs': 32773,
'total_submitted': 128496},
'status': None}],
'user_name': 'omgitspavel'}
Test has been queued. Result:
{'interpret_id': 'runcode_1627226797.967799_mdxHjiedlk',
'test_case': '[2,7,11,15]\n9'}
Got test result:
{'code_answer': ['[1]'],
'code_output': ['stdout', ''],
'correct_answer': False,
'elapsed_time': 35,
'expected_code_answer': ['[0,1]'],
'expected_code_output': [],
'expected_elapsed_time': 17,
'expected_lang': 'cpp',
'expected_memory': 6140000,
'expected_run_success': True,
'expected_status_code': 10,
'expected_status_runtime': '4',
'expected_task_finish_time': 1627224209392,
'full_runtime_error': None,
'lang': 'python',
'memory': 13240000,
'memory_percentile': None,
'pretty_lang': 'Python',
'question_id': None,
'run_success': True,
'runtime_error': None,
'runtime_percentile': None,
'state': 'SUCCESS',
'status_code': 10,
'status_memory': '13.2 MB',
'status_msg': 'Accepted',
'status_runtime': '20 ms',
'submission_id': 'runcode_1627226797.967799_mdxHjiedlk',
'task_finish_time': 1627226798140,
'total_correct': None,
'total_testcases': None}
Submission has been queued. Result:
{'submission_id': 528121775}
Got submission result:
{'code_output': '[1]',
'compare_result': '000000000000000000000000000000000000000000000000000000',
'elapsed_time': 87,
'expected_output': '[0,1]',
'full_runtime_error': None,
'input': '[2,7,11,15]\n9',
'input_formatted': '[2,7,11,15], 9',
'lang': 'python',
'last_testcase': '[2,7,11,15]\n9',
'memory': 14364000,
'memory_percentile': None,
'pretty_lang': 'Python',
'question_id': 1,
'run_success': True,
'runtime_error': None,
'runtime_percentile': None,
'state': 'SUCCESS',
'status_code': 11,
'status_memory': 'N/A',
'status_msg': 'Wrong Answer',
'status_runtime': 'N/A',
'std_output': 'stdout\n',
'submission_id': '528121775',
'task_finish_time': 1627226803971,
'total_correct': 0,
"""