Open Mochitto opened 1 year ago
I have looked a bit into the AnkiConnect and tried to make something. I documented the findings below.
Here are the things that learned:
Naturally, the user needs to install AnkiConnect plugin and have Anki running. AnkiConnect exposes a ton of functionality via HTTP API.
HTTP requests needs to contain a JSON-encoded object, with specific fields:
{
"action": "forgetCards",
"version": 6,
"params": {
"cards": [1498938915662, 1502098034048]
}
}
Where:
"action"
tells AnkiConnect what should be done, for example createDeck
or guiAddCards
"params"
contains contextual information related to the specific "action" and is specified in the docs"version"
tells which version of AnkiConnect Api should be used (I recommend that we keep this at latest, so 6
).HTTP responses look like this:
{
"result": ["Default", "Filtered Deck 1"],
"error": null
}
Where:
"result"
contains the result data relevant to the action
, but only if error
is null
"error"
contains error string, if something bad happened.I played a bit with the provided Python snippet and tried a few of the API methods.
As the name says, this is the action for adding notes.
I could create a test note with the following JSON, I think that the most fields are self-explanatory. There are also possible audio
and video
fields with the same content as the picture
key.
{
"action": "addNote",
"version": 6,
"params": {
"note": {
"deckName": "Work Notes",
"modelName": "Basic",
"fields": {
"Front": "front content",
"Back": "back content"
},
"options": {
"allowDuplicate": false,
"duplicateScope": "deck",
"duplicateScopeOptions": {
"deckName": "Work Notes",
"checkChildren": true,
"checkAllModels": true
}
},
"picture": [{
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/A_black_cat_named_Tilly.jpg/220px-A_black_cat_named_Tilly.jpg",
"filename": "black_cat.jpg",
"skipHash": "8d6e4646dfae812bf39651b59d7429ce",
"fields": [
"Back"
]
}],
"tags": [
"testinput"
]
}
}
}
Some remarks:
"allowDuplicate": false
field, however it is only using the Front
field to determine if you are trying to add a duplicate. In other words if the existing note in the Anki and the note you are trying to add have the same Front
field but maybe different Back
field or tags or something else, then that's a duplicate. id
number.Same fields as above, with addition of id
field. That's the way to tell Anki which note you want to update.
{
"action": "updateNote",
"version": 6,
"params": {
"note": {
"id": 1514547547030,
"fields": {
"Front": "new front content",
"Back": "new back content"
},
"tags": ["new", "tags"]
}
}
}
Some remarks:
findNotes
API which takes Anki query parameter.md2anki
again and again), then we need some way of storing and tracking the id
number in the markdown. The logic could then such that you would write the id
number in the markdown file, next to the note that created it, and whenever a note with id
number would be encountered, then it would try to find that note via API and update it. That is not hard to implement.addNote
and addNotes
API methods. Latter one takes an array of addNote
compatible objects and creates multiple notes them. The docs do not indicate that there is any other special behavior going on. If I have 100 notes, what difference does it matter if I create 100 addNote
requests or 1 addNotes
requests with all 100 notes in the body? Is there anything HTTP related that could affect this?Hello after quite some time!
I would like to get back to working on this, since I have some free time in the coming months :)
I looked again code that I wrote in the #16 and would like some input where to take this.
Using CSV/manual import is a bit annoying; it would be nice if you could use the Anki API (it would require you to add the path to the collection file to the config) to send the cards automatically to anki. Maybe it can also send images automatically (would remove the need of manually adding the media folder).