WaniKani to Anki 2 plugin

こんにちは。

Please find at the below github an Anki 2 plugin that can be used to pull Kanji and Vocab that you have had in a lesson through the API into a deck.  You will need your API Key, naturally.

https://github.com/nigelkerr/wanikani2anki

UPDATE 2013-02-17 much updated:

@aina contributes here in this thread some better-looking templates. Thank you, aina !
It also now has a configuration dialog and a preferences file, so no more editing a python file to get the api key installed.

INSTALL
Easiest way: download the zip from https://github.com/nigelkerr/wanikani2anki/archive/master.zip which will unpack a folder called “wanikani2anki-master”. There should be some things in the folder. Drag the items inside the folder to your Anki/addons directory (don’t drag the wanikani2anki-master folder itself…).

UPDATE 2014-01-26 slight update: use https to talk to wanikani.

Let me know what info you need from my system.  I’m running Linux Mint 14, running Cinnamon Desktop.

Anki 2.0.5

Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
dlopen(“/usr/lib/python2.7/lib-dynload/readline.so”, 2);
import readline # dynamically loaded from /usr/lib/python2.7/lib-dynload/readline.so
>>>

Kicks out:
acalewin@reimu ~ % anki
Traceback (most recent call last):
  File “/home/acalewin/Anki/addons/wanikani.py”, line 131, in updateWaniKaniDeck
    assert m
AssertionError
acalewin@reimu ~ %



This looks like an awesome addon, but in windows I am having some weird difficulties with it. 

I downloaded the file, but when i go to edit it to put in my api key. the format of the file goes a bit wack. I am also unable to find the YOUR_API_KEY_HERE in the file anywhere. Its bizarre  I tried editing with notepad++ and some windows 8 editor thing. Both give me teh same. Also it comes out to about 1000 lines of code. Is that same on your end?

@ acalewin: I bet that the Anki2 model I’m using here for “make a normal and a reversed card” is differently named by you.  Find in the wanikani.py the MODEL_NAME variable declaration, change it to “Basic” and see how that changes anything.  Failing that, from Anki2’s “Browse” view see if there are any models you can find the names of to try for that variable.

@Daikonran: you may be seeing Anki2 compiling the .py file to a .pyo file in that same directory.  Anki2 does that.  You want to edit just the .py one, not the .pyo.

I  tried to edit it before I even put it in the anki folder. I havent opened anki with the file yet. So I am pretty sure its not that. Idk how to show you how its being outputted though. Any ideas?

@Daikonran screen shot of the editor window? 

You might also try this link directly to the raw text of the wanikani.py file: https://raw.github.com/nigelkerr/wanikani2anki/master/wanikani.py

Yeah. I got it to work. I have no idea what happened when i Downloaded the file by just right clicking and “saving link as” but i just copied your code directly and pasted into that downloaded file over the weird text and it now works. Thanx a lot. Going to fiddle with this. Will let you know if any problems.

also for windows users. the addons folder for anki is in C:\Users<Your user name>\Documents\Anki\addons

Cool, it worked after changing the model to “Basic” for me.

I’m not terribly familiar with the Anki python bindings, but is there a way to have it update tags on items so the srs level can be added to each card?  That way we could suspend anything that isn’t Englightened or above?  I added note.tags.append(jsonDict[u’stats’][u’srs’]) when you added the type tags, but realized that it doesn’t update existing cards when it goes through, so that tag would never be pulled off and reapplied when you increased the srs level of that item.

@ acalewin: this will require more figuring out nor I’ve done as yet, and in less well-documented parts of Anki.  This importer extends  the importer classes they’ve got there already, so there isn’t much expressiveness.

To customize what gets made into a card in this current plugin, line 60 reads:

    
 if item[u’stats’<span>]:

And you can add to that condition however you want to select what should be made into a card.

There is Anki API support for writing card data to the Anki database directly, and that might be where you’re headed.

Thank you a lot for your great work :smiley:

I’m using Mac OS X, and Anki shows me this error after clicking “Update WaniKani Deck”:

An error occurred in an add-on. Please contact the add-on author.
Traceback (most recent call last):
File "/Users/webmaster/Documents/Anki/addons/wanikani.py", line 132, in updateWaniKaniDeck
mw.col.models.setCurrent(m)
File "anki/models.pyc", line 111, in setCurrent
TypeError: 'NoneType' object has no attribute '__getitem__'


@ r0pe: don’t recognize what’s gone wrong immediately.  I think that line 111 setCurrent “anki/models.pyc” is referring to this file:

https://github.com/dae/anki/blob/master/anki/models.py

What’s there and possibly None or not-quite-as-expected at that moment is the model object attempted to be sent through from the plugin.
This might be a similar problem to what 
@ acalewin was seeing earlier, that the MODEL_NAME variable I send out by default isn’t the same name you have.  See above for possible strategies (I’d start first by setting MODEL_NAME to “Basic”).

You’re on: MACOSX 10.?.?, you’ve got Anki 2.0.?, no problems with Anki 2 heretofore?  

acalewin said... Let me know what info you need from my system.  I'm running Linux Mint 14, running Cinnamon Desktop.

 It looks like someone has already achieved enlightenment...
naigeru said... @ r0pe: don't recognize what's gone wrong immediately.  I think that line 111 setCurrent "anki/models.pyc" is referring to this file:

https://github.com/dae/anki/blob/master/anki/models.py

What's there and possibly None or not-quite-as-expected at that moment is the model object attempted to be sent through from the plugin.
This might be a similar problem to what 
@ acalewin was seeing earlier, that the MODEL_NAME variable I send out by default isn't the same name you have.  See above for possible strategies (I'd start first by setting MODEL_NAME to "Basic").

You're on: MACOSX 10.?.?, you've got Anki 2.0.?, no problems with Anki 2 heretofore?  
 Thank you naigeru, it worked setting MODEL_NAME to "Basic".
I'm on MAC OS X 10.8.2, I've got the latest Anki 2.0.5

Thank you again for the great work.

I changed some of your code, because I wanted it to import only vocabulary, and to show the meaning on the front and the kana and kanji on the back.
It worked great, I’m so happy now.

I can’t thank you enough naigaru.

どういたしまして!

r0pe said... I changed some of your code, because I wanted it to import only vocabulary, and to show the meaning on the front and the kana and kanji on the back.
It worked great, I'm so happy now.

I can't thank you enough naigaru.

 Post it please?

(and i can add it to the readme for the plugin…)

This is great!  Thanks so much, I had been adding cards one at a time, this will save so much time.
Mac 10.8.2, latest Anki.  I also had to change MODEL_NAME to “Basic”.

I don’t know a good way to post my changes. Because I don’t know much about coding.

For only importing vocab and not kanji:
I didn’t delete anything in the code, just made the following lines inactive with #:

EDIT: because of the code, it won't display the text correctly here. Therefor search for every line containing "kanji" and make the line inactive by writing # in front.

For changing the order to "Meaning" (front) and "character/kana" (back):
Look for the lines containing  the 3 words "meaning" "character" and "kana" and just change the order like that:

def noteFromJson(self, jsonDict):
note = ForeignNote()
note.fields.append(u"<span class='vocab front'>{}</span>".format(jsonDict[u'meaning']))
note.fields.append(u"<span class='vobab back'>{}<br>{}</span>".format(jsonDict[u'character'], jsonDict[u'kana']))
note.tags.append('vocab')
return note


Daikonran said... Yeah. I got it to work. I have no idea what happened when i Downloaded the file by just right clicking and "saving link as" but i just copied your code directly and pasted into that downloaded file over the weird text and it now works. Thanx a lot. Going to fiddle with this. Will let you know if any problems.

 I had the exact same thing happen! Thanks for sharing what you did to solve it.