diff --git a/AnkiConnect.py b/AnkiConnect.py index 56465d4..e051c16 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -29,6 +29,7 @@ import socket import sys from time import time from unicodedata import normalize +from operator import itemgetter # @@ -706,6 +707,72 @@ class AnkiBridge: else: return [] + def cardsInfo(self,cards): + result = [] + for cid in cards: + try: + card = self.collection().getCard(cid) + model = card.model() + note = card.note() + fields = {} + for info in model['flds']: + order = info['ord'] + name = info['name'] + fields[name] = {'value': note.fields[order], 'order': order} + + result.append({ + 'cardId': card.id, + 'fields': fields, + 'fieldOrder': card.ord, + 'question': card._getQA()['q'], + 'answer': card._getQA()['a'], + 'modelName': model['name'], + 'deckName': self.deckNameFromId(card.did), + 'css': model['css'], + 'factor': card.factor, + #This factor is 10 times the ease percentage, + # so an ease of 310% would be reported as 3100 + 'interval': card.ivl, + 'note': card.nid + }) + except TypeError as e: + # Anki will give a TypeError if the card ID does not exist. + # Best behavior is probably to add an "empty card" to the + # returned result, so that the items of the input and return + # lists correspond. + result.append({}) + + return result + + def notesInfo(self,notes): + result = [] + for nid in notes: + try: + note = self.collection().getNote(nid) + model = note.model() + + fields = {} + for info in model['flds']: + order = info['ord'] + name = info['name'] + fields[name] = {'value': note.fields[order], 'order': order} + + result.append({ + 'noteId': note.id, + 'tags' : note.tags, + 'fields': fields, + 'modelName': model['name'], + 'cards': self.collection().db.list( + "select id from cards where nid = ? order by ord", self.id) + }) + except TypeError as e: + # Anki will give a TypeError if the note ID does not exist. + # Best behavior is probably to add an "empty card" to the + # returned result, so that the items of the input and return + # lists correspond. + result.append({}) + return result + def getDecks(self, cards): decks = {} @@ -1196,6 +1263,13 @@ class AnkiConnect: def guiExitAnki(self): return self.anki.guiExitAnki() + @webApi() + def cardsInfo(self, cards): + return self.anki.cardsInfo(cards) + + @webApi() + def notesInfo(self, notes): + return self.anki.notesInfo(notes) # # Entry diff --git a/README.md b/README.md index 6e1c676..1634948 100644 --- a/README.md +++ b/README.md @@ -1065,6 +1065,93 @@ guarantee that your application continues to function properly in the future. } ``` +* **cardsInfo** + + Returns a list of objects containing for each card ID the card fields, front and back sides including CSS, note type, the note that the card belongs to, and deck name, as well as ease and interval. + + *Sample request*: + ```json + { + "action": "cardsInfo", + "version": 5, + "params": { + "cards": [1498938915662, 1502098034048] + } + } + ``` + + *Sample result*: + ```json + { + "result": [ + { + "answer": "back content", + "question": "front content", + "deckName": "Default", + "modelName": "Basic", + "fieldOrder": 1, + "fields": { + "Front": {"value": "front content", "order": 0}, + "Back": {"value": "back content", "order": 1} + }, + "css":"p {font-family:Arial;}", + "cardId": 1498938915662, + "interval": 16, + "note":1502298033753 + }, + { + "answer": "back content", + "question": "front content", + "deckName": "Default", + "modelName": "Basic", + "fieldOrder": 0, + "fields": { + "Front": {"value": "front content", "order": 0}, + "Back": {"value": "back content", "order": 1} + }, + "css":"p {font-family:Arial;}", + "cardId": 1502098034048, + "interval": 23, + "note":1502298033753 + } + ], + "error": null + } + ``` + +* **notesInfo** + + Returns a list of objects containing for each note ID the note fields, tags, note type and the cards belonging to the note. + + *Sample request*: + ```json + { + "action": "notesInfo", + "version": 5, + "params": { + "notes": [1502298033753] + } + } + ``` + + *Sample result*: + ```json + { + "result": [ + { + "noteId":1502298033753, + "modelName": "Basic", + "tags":["tag","another_tag"], + "fields": { + "Front": {"value": "front content", "order": 0}, + "Back": {"value": "back content", "order": 1} + }, + } + ], + "error": null + } + ``` + #### Media File Storage #### * **storeMediaFile**