From 0012245c57e07010cc487b26061563ef8f897197 Mon Sep 17 00:00:00 2001
From: Antistic <3298461+antistic@users.noreply.github.com>
Date: Sun, 11 Jun 2023 22:57:08 +0100
Subject: [PATCH 1/2] add new action: answerCards
---
README.md | 30 ++++++++++++++++++++++++++++++
plugin/__init__.py | 16 ++++++++++++++++
tests/test_cards.py | 12 ++++++++++++
3 files changed, 58 insertions(+)
diff --git a/README.md b/README.md
index 19a008b..f36e317 100644
--- a/README.md
+++ b/README.md
@@ -724,6 +724,36 @@ corresponding to when the API was available for use.
```
+#### `answerCards`
+
+* Answer cards. Answers are between 1 (Again) and 4 (Easy). Will start the timer immediately before answering. Returns `true` if card exists, `false` otherwise.
+
+
+ Sample request:
+
+ ```json
+ {
+ "action": "answerCards",
+ "version": 6,
+ "params": {
+ "cards": [1498938915662, 1502098034048],
+ "answers": [2, 4],
+ }
+ }
+ ```
+
+
+
+ Sample result:
+
+ ```json
+ {
+ "result": [true, true],
+ "error": null
+ }
+ ```
+
+
---
### Deck Actions
diff --git a/plugin/__init__.py b/plugin/__init__.py
index 73f4d81..c09cd11 100644
--- a/plugin/__init__.py
+++ b/plugin/__init__.py
@@ -1530,6 +1530,22 @@ class AnkiConnect:
self.stopEditing()
+ @util.api()
+ def answerCards(self, cards, answers):
+ scheduler = self.scheduler()
+ success = []
+ for i, cid in enumerate(cards):
+ try:
+ card = self.getCard(cid)
+ card.start_timer()
+ scheduler.answerCard(card, answers[i])
+ success.append(True)
+ except NotFoundError:
+ success.append(False)
+
+ return success
+
+
@util.api()
def cardReviews(self, deck, startID):
return self.database().all(
diff --git a/tests/test_cards.py b/tests/test_cards.py
index 57705d4..dd69b44 100755
--- a/tests/test_cards.py
+++ b/tests/test_cards.py
@@ -76,3 +76,15 @@ def test_forgetCards(setup):
def test_relearnCards(setup):
ac.relearnCards(cards=setup.card_ids)
+
+
+class TestAnswerCards:
+ def test_answerCards(self, setup):
+ ac.scheduler().reset()
+ result = ac.answerCards(cards=setup.card_ids, answers=[2, 1, 4, 3])
+ assert result == [True] * 4
+
+ def test_answerCards_with_invalid_card_id(self, setup):
+ ac.scheduler().reset()
+ result = ac.answerCards(cards=[123], answers=[2])
+ assert result == [False]
From 3892481cea8ed10a0782c2925923499e97ce5b04 Mon Sep 17 00:00:00 2001
From: ant <3298461+antistic@users.noreply.github.com>
Date: Thu, 15 Jun 2023 12:49:02 +0100
Subject: [PATCH 2/2] change param format for answerCards
---
README.md | 14 +++++++++++---
plugin/__init__.py | 8 +++++---
tests/test_cards.py | 7 +++++--
3 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/README.md b/README.md
index f36e317..27d402d 100644
--- a/README.md
+++ b/README.md
@@ -726,7 +726,7 @@ corresponding to when the API was available for use.
#### `answerCards`
-* Answer cards. Answers are between 1 (Again) and 4 (Easy). Will start the timer immediately before answering. Returns `true` if card exists, `false` otherwise.
+* Answer cards. Ease is between 1 (Again) and 4 (Easy). Will start the timer immediately before answering. Returns `true` if card exists, `false` otherwise.
Sample request:
@@ -736,8 +736,16 @@ corresponding to when the API was available for use.
"action": "answerCards",
"version": 6,
"params": {
- "cards": [1498938915662, 1502098034048],
- "answers": [2, 4],
+ "answers": [
+ {
+ "cardId": 1498938915662,
+ "ease": 2
+ },
+ {
+ "cardId": 1502098034048,
+ "ease": 4
+ }
+ ]
}
}
```
diff --git a/plugin/__init__.py b/plugin/__init__.py
index c09cd11..5ca8514 100644
--- a/plugin/__init__.py
+++ b/plugin/__init__.py
@@ -1531,14 +1531,16 @@ class AnkiConnect:
@util.api()
- def answerCards(self, cards, answers):
+ def answerCards(self, answers):
scheduler = self.scheduler()
success = []
- for i, cid in enumerate(cards):
+ for answer in answers:
try:
+ cid = answer['cardId']
+ ease = answer['ease']
card = self.getCard(cid)
card.start_timer()
- scheduler.answerCard(card, answers[i])
+ scheduler.answerCard(card, ease)
success.append(True)
except NotFoundError:
success.append(False)
diff --git a/tests/test_cards.py b/tests/test_cards.py
index dd69b44..0ce93b2 100755
--- a/tests/test_cards.py
+++ b/tests/test_cards.py
@@ -81,10 +81,13 @@ def test_relearnCards(setup):
class TestAnswerCards:
def test_answerCards(self, setup):
ac.scheduler().reset()
- result = ac.answerCards(cards=setup.card_ids, answers=[2, 1, 4, 3])
+ answers = [
+ {"cardId": a, "ease": b} for a, b in zip(setup.card_ids, [2, 1, 4, 3])
+ ]
+ result = ac.answerCards(answers)
assert result == [True] * 4
def test_answerCards_with_invalid_card_id(self, setup):
ac.scheduler().reset()
- result = ac.answerCards(cards=[123], answers=[2])
+ result = ac.answerCards([{"cardId": 123, "ease": 2}])
assert result == [False]