-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathgenerate.py
executable file
·397 lines (330 loc) · 12.7 KB
/
generate.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python3
import argparse
import asyncio
import functools
import json
import logging
import os
import time
from functools import lru_cache
from typing import Any, Callable, Coroutine, Dict, Iterator, List, Tuple
import diskcache
# https://github.com/kerrickstaley/genanki
import genanki # type: ignore
# https://github.com/prius/python-leetcode
import leetcode # type: ignore
import leetcode.auth # type: ignore
import urllib3
from tqdm import tqdm
LEETCODE_ANKI_MODEL_ID = 4567610856
LEETCODE_ANKI_DECK_ID = 8589798175
OUTPUT_FILE = "leetcode.apkg"
CACHE_DIR = "cache"
ALLOWED_EXTENSIONS = {".py", ".go"}
leetcode_api_access_lock = asyncio.Lock()
logging.getLogger().setLevel(logging.INFO)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Generate Anki cards for leetcode")
parser.add_argument(
"--start", type=int, help="Start generation from this problem", default=0
)
parser.add_argument(
"--stop", type=int, help="Stop generation on this problem", default=2 ** 64
)
args = parser.parse_args()
return args
def retry(times: int, exceptions: Tuple[Exception], delay: float) -> Callable:
"""
Retry Decorator
Retries the wrapped function/method `times` times if the exceptions listed
in `exceptions` are thrown
"""
def decorator(func):
@functools.wraps(func)
async def wrapper(*args, **kwargs):
for attempt in range(times - 1):
try:
return await func(*args, **kwargs)
except exceptions:
logging.exception(f"Exception occured, try {attempt + 1}/{times}")
time.sleep(delay)
logging.error("Last try")
return await func(*args, **kwargs)
return wrapper
return decorator
class LeetcodeData:
def __init__(self) -> None:
# Initialize leetcode API client
self._api_instance = get_leetcode_api_client()
# Init problem data cache
if not os.path.exists(CACHE_DIR):
os.mkdir(CACHE_DIR)
self._cache = diskcache.Cache(CACHE_DIR)
@retry(times=3, exceptions=(urllib3.exceptions.ProtocolError,), delay=5)
async def _get_problem_data(self, problem_slug: str) -> Dict[str, str]:
if problem_slug in self._cache:
return self._cache[problem_slug]
api_instance = self._api_instance
graphql_request = leetcode.GraphqlQuery(
query="""
query getQuestionDetail($titleSlug: String!) {
question(titleSlug: $titleSlug) {
freqBar
questionId
questionFrontendId
boundTopicId
title
content
translatedTitle
translatedContent
isPaidOnly
difficulty
likes
dislikes
isLiked
similarQuestions
contributors {
username
profileUrl
avatarUrl
__typename
}
langToValidPlayground
topicTags {
name
slug
translatedName
__typename
}
companyTagStats
codeSnippets {
lang
langSlug
code
__typename
}
stats
hints
solution {
id
canSeeDetail
__typename
}
status
sampleTestCase
metaData
judgerAvailable
judgeType
mysqlSchemas
enableRunCode
enableTestMode
envInfo
__typename
}
}
""",
variables=leetcode.GraphqlQueryVariables(title_slug=problem_slug),
operation_name="getQuestionDetail",
)
# Critical section. Don't allow more than one parallel request to
# the Leetcode API
async with leetcode_api_access_lock:
time.sleep(2) # Leetcode has a rate limiter
data = api_instance.graphql_post(body=graphql_request).data.question
# Save data in the cache
self._cache[problem_slug] = data
return data
async def _get_description(self, problem_slug: str) -> str:
data = await self._get_problem_data(problem_slug)
return data.content or "No content"
async def _stats(self, problem_slug: str) -> Dict[str, str]:
data = await self._get_problem_data(problem_slug)
return json.loads(data.stats)
async def submissions_total(self, problem_slug: str) -> int:
return (await self._stats(problem_slug))["totalSubmissionRaw"]
async def submissions_accepted(self, problem_slug: str) -> int:
return (await self._stats(problem_slug))["totalAcceptedRaw"]
async def description(self, problem_slug: str) -> str:
return await self._get_description(problem_slug)
async def solution(self, problem_slug: str) -> str:
return ""
async def difficulty(self, problem_slug: str) -> str:
data = await self._get_problem_data(problem_slug)
diff = data.difficulty
if diff == "Easy":
return "<font color='green'>Easy</font>"
elif diff == "Medium":
return "<font color='orange'>Medium</font>"
elif diff == "Hard":
return "<font color='red'>Hard</font>"
else:
raise ValueError(f"Incorrect difficulty: {diff}")
async def paid(self, problem_slug: str) -> str:
data = await self._get_problem_data(problem_slug)
return data.is_paid_only
async def problem_id(self, problem_slug: str) -> str:
data = await self._get_problem_data(problem_slug)
return data.question_frontend_id
async def likes(self, problem_slug: str) -> int:
data = await self._get_problem_data(problem_slug)
likes = data.likes
if not isinstance(likes, int):
raise ValueError(f"Likes should be int: {likes}")
return likes
async def dislikes(self, problem_slug: str) -> int:
data = await self._get_problem_data(problem_slug)
dislikes = data.dislikes
if not isinstance(dislikes, int):
raise ValueError(f"Dislikes should be int: {dislikes}")
return dislikes
async def tags(self, problem_slug: str) -> List[str]:
data = await self._get_problem_data(problem_slug)
return list(map(lambda x: x.slug, data.topic_tags))
async def freq_bar(self, problem_slug: str) -> float:
data = await self._get_problem_data(problem_slug)
return data.freq_bar or 0
class LeetcodeNote(genanki.Note):
@property
def guid(self):
# Hash by leetcode task handle
return genanki.guid_for(self.fields[0])
@lru_cache(None)
def get_leetcode_api_client() -> leetcode.DefaultApi:
configuration = leetcode.Configuration()
session_id = os.environ["LEETCODE_SESSION_ID"]
csrf_token = leetcode.auth.get_csrf_cookie(session_id)
configuration.api_key["x-csrftoken"] = csrf_token
configuration.api_key["csrftoken"] = csrf_token
configuration.api_key["LEETCODE_SESSION"] = session_id
configuration.api_key["Referer"] = "https://leetcode.com"
configuration.debug = False
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
return api_instance
def get_leetcode_task_handles() -> Iterator[Tuple[str, str, str]]:
api_instance = get_leetcode_api_client()
for topic in ["algorithms", "database", "shell", "concurrency"]:
api_response = api_instance.api_problems_topic_get(topic=topic)
for stat_status_pair in api_response.stat_status_pairs:
stat = stat_status_pair.stat
yield (topic, stat.question__title, stat.question__title_slug)
async def generate_anki_note(
leetcode_data: LeetcodeData,
leetcode_model: genanki.Model,
leetcode_task_handle: str,
leetcode_task_title: str,
topic: str,
) -> LeetcodeNote:
return LeetcodeNote(
model=leetcode_model,
fields=[
leetcode_task_handle,
str(await leetcode_data.problem_id(leetcode_task_handle)),
leetcode_task_title,
topic,
await leetcode_data.description(leetcode_task_handle),
await leetcode_data.difficulty(leetcode_task_handle),
"yes" if await leetcode_data.paid(leetcode_task_handle) else "no",
str(await leetcode_data.likes(leetcode_task_handle)),
str(await leetcode_data.dislikes(leetcode_task_handle)),
str(await leetcode_data.submissions_total(leetcode_task_handle)),
str(await leetcode_data.submissions_accepted(leetcode_task_handle)),
str(
int(
await leetcode_data.submissions_accepted(leetcode_task_handle)
/ await leetcode_data.submissions_total(leetcode_task_handle)
* 100
)
),
str(await leetcode_data.freq_bar(leetcode_task_handle)),
],
tags=await leetcode_data.tags(leetcode_task_handle),
# FIXME: sort field doesn't work doesn't work
sort_field=str(await leetcode_data.freq_bar(leetcode_task_handle)).zfill(3),
)
async def generate(start: int, stop: int) -> None:
leetcode_model = genanki.Model(
LEETCODE_ANKI_MODEL_ID,
"Leetcode model",
fields=[
{"name": "Slug"},
{"name": "Id"},
{"name": "Title"},
{"name": "Topic"},
{"name": "Content"},
{"name": "Difficulty"},
{"name": "Paid"},
{"name": "Likes"},
{"name": "Dislikes"},
{"name": "SubmissionsTotal"},
{"name": "SubmissionsAccepted"},
{"name": "SumissionAcceptRate"},
{"name": "Frequency"},
# TODO: add hints
],
templates=[
{
"name": "Leetcode",
"qfmt": """
<h2>{{Id}}. {{Title}}</h2>
<b>Difficulty:</b> {{Difficulty}}<br/>
👍 {{Likes}} 👎 {{Dislikes}}<br/>
<b>Submissions (total/accepted):</b>
{{SubmissionsTotal}}/{{SubmissionsAccepted}}
({{SumissionAcceptRate}}%)
<br/>
<b>Topic:</b> {{Topic}}<br/>
<b>Frequency:</b>
<progress value="{{Frequency}}" max="100">
{{Frequency}}%
</progress>
<br/>
<b>URL:</b>
<a href='https://leetcode.com/problems/{{Slug}}/'>
https://leetcode.com/problems/{{Slug}}/
</a>
<br/>
<h3>Description</h3>
{{Content}}
""",
"afmt": """
{{FrontSide}}
<hr id="answer">
<b>Discuss URL:</b>
<a href='https://leetcode.com/problems/{{Slug}}/discuss/'>
https://leetcode.com/problems/{{Slug}}/discuss/
</a>
<br/>
<b>Solution URL:</b>
<a href='https://leetcode.com/problems/{{Slug}}/solution/'>
https://leetcode.com/problems/{{Slug}}/solution/
</a>
<br/>
""",
},
],
)
leetcode_deck = genanki.Deck(LEETCODE_ANKI_DECK_ID, "leetcode")
leetcode_data = LeetcodeData()
note_generators: List[Coroutine[Any, Any, LeetcodeNote]] = []
for topic, leetcode_task_title, leetcode_task_handle in list(
get_leetcode_task_handles()
)[start:stop]:
note_generators.append(
generate_anki_note(
leetcode_data,
leetcode_model,
leetcode_task_handle,
leetcode_task_title,
topic,
)
)
for leetcode_note in tqdm(note_generators):
leetcode_deck.add_note(await leetcode_note)
genanki.Package(leetcode_deck).write_to_file(OUTPUT_FILE)
async def main() -> None:
args = parse_args()
start, stop = args.start, args.stop
await generate(start, stop)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())