Skip to content

Instantly share code, notes, and snippets.

@christydennison
Last active November 17, 2023 20:47
Show Gist options
  • Select an option

  • Save christydennison/aa31ce726ed31a3664c9f21eebe3e887 to your computer and use it in GitHub Desktop.

Select an option

Save christydennison/aa31ce726ed31a3664c9f21eebe3e887 to your computer and use it in GitHub Desktop.
wanikani: skip ahead lesson ordering and do specific subject lessons
import requests
from itertools import batched
from collections import defaultdict
import json
import os
API_KEY = os.environ["WANIKANI_READ_API_KEY"]
HEADERS = {"Authorization": f"Bearer {API_KEY}"}
BASE_URL = "https://api.wanikani.com/v2"
SUBJECTS_PATH = os.path.join(BASE_URL, "subjects")
BATCH_SIZE = 5
def save_files(all_data, sub_name):
subs_levels = defaultdict(list)
for obj in all_data:
d = obj["data"]
primary_meaning = [m["meaning"] for m in d["meanings"] if m["primary"]][0]
subs_levels[d["level"]].append(
{"id": obj["id"], "c": d["slug"], "m": primary_meaning}
)
# pretty print
review_batches = defaultdict(list)
for level, subs in subs_levels.items():
for batch in batched(subs, BATCH_SIZE):
batch_ids = [b["id"] for b in batch]
review_batches[level].append("-".join(map(str, batch_ids)))
batches_str = ""
for level in sorted(review_batches.keys()):
batch_str = "\n".join(review_batches[level])
batches_str += f"{level}\n{batch_str}\n\n"
with open(f"{sub_name}_batches.txt", "w") as f:
f.write(batches_str)
# json
with open(f"{sub_name}_batches.json", "w", encoding="utf-8") as f:
json.dump(subs_levels, f, ensure_ascii=False)
def download_kanji():
req = requests.get(
SUBJECTS_PATH,
headers=HEADERS,
params={"types": ["kanji"]},
)
next_req = requests.get(
SUBJECTS_PATH,
headers=HEADERS,
params={"types": ["kanji"], "page_after_id": 1439},
)
nnext_req = requests.get(
SUBJECTS_PATH,
headers=HEADERS,
params={"types": ["kanji"], "page_after_id": 2439},
)
all_data = []
all_data.extend(req.json()["data"])
all_data.extend(next_req.json()["data"])
all_data.extend(nnext_req.json()["data"])
save_files(all_data, "kanji")
def download_radicals():
req = requests.get(
SUBJECTS_PATH,
headers=HEADERS,
params={"types": ["radical"]},
)
all_data = req.json()["data"]
save_files(all_data, "radical")
## Uncomment to run
# download_radicals()
# download_kanji()
"""
Want to get radical or kanji lessons before the rest of the subjects in your lesson queue?
Produces a file with level followed by the lesson queue string (change the BATCH_SIZE variable
if you want a different length):
10
179-180-181-182-183
184-185-186-187-200
219-240-275-439-8774
To run, uncomment one or both of the function calls above, then paste into a terminal:
python wanikani.py
You have two options to use these:
1) Click on "Lessons". Take the first id (in this case, 179) and paste it into the path:
https://www.wanikani.com/subject-lessons/{18 digit num}/179
2) Go straight to the quiz with the first line:
https://www.wanikani.com/subject-lessons/{18 digit num}/quiz?queue=179-180-181-182-183
The json files can be used as a cache for other functions.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment