-
Notifications
You must be signed in to change notification settings - Fork 43
/
generate.py
executable file
·216 lines (188 loc) · 6.71 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
#!/usr/bin/env python3
"""
This script generates an Anki deck with all the leetcode problems currently
known.
"""
import argparse
import asyncio
import logging
from pathlib import Path
from typing import Any, Awaitable, Callable, Coroutine, List
# https://github.com/kerrickstaley/genanki
import genanki # type: ignore
from tqdm import tqdm # type: ignore
import leetcode_anki.helpers.leetcode
LEETCODE_ANKI_MODEL_ID = 4567610856
LEETCODE_ANKI_DECK_ID = 8589798175
OUTPUT_FILE = "leetcode.apkg"
ALLOWED_EXTENSIONS = {".py", ".go"}
logging.getLogger().setLevel(logging.INFO)
def parse_args() -> argparse.Namespace:
"""
Parse command line arguments for the script
"""
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
)
parser.add_argument(
"--page-size",
type=int,
help="Get at most this many problems (decrease if leetcode API times out)",
default=500,
)
parser.add_argument(
"--list-id",
type=str,
help="Get all questions from a specific list id (https://leetcode.com/list?selectedList=<list_id>",
default="",
)
parser.add_argument(
"--output-file", type=str, help="Output filename", default=OUTPUT_FILE
)
args = parser.parse_args()
return args
class LeetcodeNote(genanki.Note):
"""
Extended base class for the Anki note, that correctly sets the unique
identifier of the note.
"""
@property
def guid(self) -> str:
# Hash by leetcode task handle
return genanki.guid_for(self.fields[0])
async def generate_anki_note(
leetcode_data: leetcode_anki.helpers.leetcode.LeetcodeData,
leetcode_model: genanki.Model,
leetcode_task_handle: str,
) -> LeetcodeNote:
"""
Generate a single Anki flashcard
"""
return LeetcodeNote(
model=leetcode_model,
fields=[
leetcode_task_handle,
str(await leetcode_data.problem_id(leetcode_task_handle)),
str(await leetcode_data.title(leetcode_task_handle)),
str(await leetcode_data.category(leetcode_task_handle)),
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, page_size: int, list_id: str, output_file: str
) -> None:
"""
Generate an Anki deck
"""
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, Path(output_file).stem)
leetcode_data = leetcode_anki.helpers.leetcode.LeetcodeData(
start, stop, page_size, list_id
)
note_generators: List[Awaitable[LeetcodeNote]] = []
task_handles = await leetcode_data.all_problems_handles()
logging.info("Generating flashcards")
for leetcode_task_handle in task_handles:
note_generators.append(
generate_anki_note(leetcode_data, leetcode_model, leetcode_task_handle)
)
for leetcode_note in tqdm(note_generators, unit="flashcard"):
leetcode_deck.add_note(await leetcode_note)
genanki.Package(leetcode_deck).write_to_file(output_file)
async def main() -> None:
"""
The main script logic
"""
args = parse_args()
start, stop, page_size, list_id, output_file = (
args.start,
args.stop,
args.page_size,
args.list_id,
args.output_file,
)
await generate(start, stop, page_size, list_id, output_file)
if __name__ == "__main__":
loop: asyncio.events.AbstractEventLoop = asyncio.get_event_loop()
loop.run_until_complete(main())