From 6da8481b07592b4664da3c14065f1c505d01de20 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sun, 13 Aug 2017 09:02:59 +0100 Subject: [PATCH] Add getDecks function --- AnkiConnect.py | 19 +++++++++++++++++++ README.md | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/AnkiConnect.py b/AnkiConnect.py index a8dc83b..55a99ed 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -537,6 +537,20 @@ class AnkiBridge: return [] + def getDecks(self, cards): + decks = {} + for card in cards: + did = self.collection().db.scalar('select did from cards where id = ?', card) + deck = self.collection().decks.get(did)['name'] + + if deck in decks: + decks[deck].append(card) + else: + decks[deck] = [card] + + return decks + + def changeDeck(self, cards, deck): self.startEditing() @@ -837,6 +851,11 @@ class AnkiConnect: return self.anki.findCards(query) + @webApi + def getDecks(self, cards): + return self.anki.getDecks(cards) + + @webApi def changeDeck(self, cards, deck): return self.anki.changeDeck(cards, deck) diff --git a/README.md b/README.md index e82bfa6..488da6b 100644 --- a/README.md +++ b/README.md @@ -511,6 +511,30 @@ Below is a list of currently supported actions. Requests with invalid actions or ] ``` +* **getDecks** + + Accepts an array of card IDs and returns an object with each deck name as a key, and its value an array of the given + cards which belong to it. + + *Sample request*: + ``` + { + "action": "getDecks", + "params": { + "cards": [1502298036657, 1502298033753, 1502032366472] + } + } + ``` + + *Sample response*: + ``` + { + "Default": [1502032366472], + "Japanese::JLPT N3": [1502298036657, 1502298033753] + } + ``` + + * **changeDeck** Moves cards with the given IDs to a different deck, creating the deck if it doesn't exist yet.