Skip to content

Instantly share code, notes, and snippets.

@kinowarrior
Forked from kiennguyen94/sort_cards.py
Created December 21, 2024 01:09
Show Gist options
  • Save kinowarrior/0e96abc11559cbcbb4224f7c602d0b5c to your computer and use it in GitHub Desktop.
Save kinowarrior/0e96abc11559cbcbb4224f7c602d0b5c to your computer and use it in GitHub Desktop.
Script to sort the wanikani deck by level, radical, kanji, and vocab.
import os
from typing import List
from anki.storage import Collection
# the deck: https://ankiweb.net/shared/info/2072613354
# TODO replace YOUR_USER_NAME
col = Collection('/Users/{YOUR_USER_NAME}/Library/Application Support/Anki2/User 1/collection.anki2')
deck_id = col.decks.id('WaniKani')
deck = col.decks.get(deck_id)
cards = col.find_cards("deck:WaniKani")
def find_level(tags: List[str]):
level = ""
typ = 0
for tag in tags:
if 'level' in tag:
level = tag
elif tag == 'radical':
typ = 0
elif tag == 'kanji':
typ = 1
elif tag == 'vocab':
typ = 2
else:
raise RuntimeError("fu")
return level, typ
cards.sort(key= lambda card_id: find_level(col.get_card(card_id).note().tags))
for i, card_id in enumerate(cards):
card = col.get_card(card_id)
card.due = i
col.update_card(card)
col.decks.save(deck)
col.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment