From a6c15d7bb20ac099cecf728f05307c4317691fee Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sat, 12 Aug 2017 15:57:28 +0100 Subject: [PATCH 1/3] Change isSuspended to areSuspended; add areDue function; code style fixes --- AnkiConnect.py | 60 ++++++++++++++++++++++++++++++++++++++------------ README.md | 32 ++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 19 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index 95b1326..5faf6fc 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -24,6 +24,7 @@ import os.path import select import socket import sys +from time import time # @@ -401,18 +402,44 @@ class AnkiBridge: return False - def isSuspended(self, card): - card = self.collection().getCard(card) - if card.queue == -1: - return True - else: - return False + def areSuspended(self, cards): + suspended = [] + for card in cards: + card = self.collection().getCard(card) + if card.queue == -1: + suspended.append(True) + else: + suspended.append(False) + return suspended + + + def areDue(self, cards): + due = [] + for card in cards: + date, ivl = self.collection().db.all('select id/1000.0, ivl from revlog where cid = ?', card)[-1] + + if self.findCards('cid:%s is:new' % card): + due.append(True) + continue + + if (ivl >= -1200): + if self.findCards('cid:%s is:due' % card): + due.append(True) + else: + due.append(False) + else: + if date - ivl <= time(): + due.append(True) + else: + due.append(False) + + return due def getIntervals(self, cards, complete=False): intervals = [] for card in cards: - interval = self.window().col.db.list('select ivl from revlog where cid = ?', card) + interval = self.collection().db.list('select ivl from revlog where cid = ?', card) if not complete: interval = interval[-1] intervals.append(interval) @@ -510,22 +537,22 @@ class AnkiBridge: def changeDeck(self, cards, deck): self.startEditing() - did = self.window().col.decks.id(deck) + did = self.collection().decks.id(deck) mod = anki.utils.intTime() - usn = self.window().col.usn() + usn = self.collection().usn() # normal cards scids = anki.utils.ids2str(cards) # remove any cards from filtered deck first - self.window().col.sched.remFromDyn(cards) + self.collection().sched.remFromDyn(cards) # then move into new deck - self.window().col.db.execute('update cards set usn=?, mod=?, did=? where id in ' + scids, usn, mod, did) + self.collection().db.execute('update cards set usn=?, mod=?, did=? where id in ' + scids, usn, mod, did) self.stopEditing() def cardsToNotes(self, cards): - return self.window().col.db.list('select distinct nid from cards where id in ' + anki.utils.ids2str(cards)) + return self.collection().db.list('select distinct nid from cards where id in ' + anki.utils.ids2str(cards)) def guiBrowse(self, query=None): @@ -755,8 +782,13 @@ class AnkiConnect: @webApi - def isSuspended(self, card): - return self.anki.isSuspended(card) + def areSuspended(self, cards): + return self.anki.areSuspended(cards) + + + @webApi + def areDue(self, cards): + return self.anki.areDue(cards) @webApi diff --git a/README.md b/README.md index e728f29..605fdb9 100644 --- a/README.md +++ b/README.md @@ -385,23 +385,45 @@ Below is a list of currently supported actions. Requests with invalid actions or true ``` -* **isSuspended** +* **areSuspended** - Returns `true` if the given card is suspended or `false` otherwise. + Returns an array, where the value at an index is `true` if the card with the given ID at that index is suspended, or + `false` otherwise. *Sample request*: ``` { - "action": "isSuspended", + "action": "areSuspended", "params": { - "card": 1483959291685 + "cards": [1483959291685, 1483959293217] } } ``` *Sample response*: ``` - false + [false, true] + ``` + +* **areDue** + + Returns an array, where the value at an index is `true` if the card with the ID given at that index is due, or + `false` otherwise. Note: cards in the learning queue with a large interval (over 20 minutes) are treated as not due + until the time of their interval has passed, to match the way Anki treats them. + + *Sample request*: + ``` + { + "action": "areDue", + "params": { + "cards": [1483959291685, 1483959293217] + } + } + ``` + + *Sample response*: + ``` + [false, true] ``` * **getIntervals** From 64c61a32fac8320eb63faaeb9348be1ea9434e18 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sat, 12 Aug 2017 16:21:04 +0100 Subject: [PATCH 2/3] Fix IndexError for new cards with areDue / getIntervals --- AnkiConnect.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index 5faf6fc..a8dc83b 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -416,12 +416,11 @@ class AnkiBridge: def areDue(self, cards): due = [] for card in cards: - date, ivl = self.collection().db.all('select id/1000.0, ivl from revlog where cid = ?', card)[-1] - if self.findCards('cid:%s is:new' % card): due.append(True) continue + date, ivl = self.collection().db.all('select id/1000.0, ivl from revlog where cid = ?', card)[-1] if (ivl >= -1200): if self.findCards('cid:%s is:due' % card): due.append(True) @@ -439,6 +438,10 @@ class AnkiBridge: def getIntervals(self, cards, complete=False): intervals = [] for card in cards: + if self.findCards('cid:%s is:new' % card): + intervals.append(0) + continue + interval = self.collection().db.list('select ivl from revlog where cid = ?', card) if not complete: interval = interval[-1] From 3f4885adfbf78fe2b570fadaf711455cdd457661 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sat, 12 Aug 2017 16:28:38 +0100 Subject: [PATCH 3/3] Improve wording of README --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 605fdb9..e82bfa6 100644 --- a/README.md +++ b/README.md @@ -387,8 +387,7 @@ Below is a list of currently supported actions. Requests with invalid actions or * **areSuspended** - Returns an array, where the value at an index is `true` if the card with the given ID at that index is suspended, or - `false` otherwise. + Returns an array indicating whether each of the given cards is suspended (in the same order). *Sample request*: ``` @@ -407,9 +406,9 @@ Below is a list of currently supported actions. Requests with invalid actions or * **areDue** - Returns an array, where the value at an index is `true` if the card with the ID given at that index is due, or - `false` otherwise. Note: cards in the learning queue with a large interval (over 20 minutes) are treated as not due - until the time of their interval has passed, to match the way Anki treats them. + Returns an array indicating whether each of the given cards is due (in the same order). Note: cards in the learning + queue with a large interval (over 20 minutes) are treated as not due until the time of their interval has passed, to + match the way Anki treats them when reviewing. *Sample request*: ```