From 6e4bbfa0d601119753583d6b643479e9fcbbe1e8 Mon Sep 17 00:00:00 2001 From: Austin Siew <17107540+Aquafina-water-bottle@users.noreply.github.com> Date: Mon, 5 Sep 2022 00:29:03 -0600 Subject: [PATCH 1/3] Added getReviewsOfCard --- README.md | 29 +++++++++++++++++++++++++++++ plugin/__init__.py | 7 +++++++ tests/test_stats.py | 2 ++ 3 files changed, 38 insertions(+) diff --git a/README.md b/README.md index 2cd6651..5caa86d 100644 --- a/README.md +++ b/README.md @@ -2759,6 +2759,35 @@ corresponding to when the API was available for use. } ``` +* **getReviewsOfCard** + + Requests all card reviews for a specific card ID. + Returns a list of 9-tuples in the same format as `cardReviews`: `(reviewTime, cardID, usn, buttonPressed, newInterval, previousInterval, newFactor, reviewDuration, reviewType)` + + *Sample request*: + ```json + { + "action": "getReviewsOfCard", + "version": 6, + "params": { + "card": "1653613948202" + } + } + ``` + + *Sample result*: + ```json + { + "result": [ + [1654102387663, 1653613948202, 1780, 3, 8, 3, 2500, 25796, 1], + [1654798974478, 1653613948202, 1861, 3, 20, 8, 2500, 18134, 1], + [1656556319328, 1653613948202, 2075, 3, 53, 20, 2500, 20530, 1], + [1661107990069, 1653613948202, 2478, 3, 131, 53, 2500, 24247, 1] + ], + "error": null + } + ``` + * **getLatestReviewID** Returns the unix time of the latest review for the given deck. 0 if no review has ever been made for the deck. diff --git a/plugin/__init__.py b/plugin/__init__.py index 4ff486b..661935e 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1373,6 +1373,13 @@ class AnkiConnect: ) + @util.api() + def getReviewsOfCard(self, card): + return self.database().all( + 'select id, cid, usn, ease, ivl, lastIvl, factor, time, type from revlog where cid = ?', card + ) + + @util.api() def reloadCollection(self): self.collection().reset() diff --git a/tests/test_stats.py b/tests/test_stats.py index a412ca1..3237161 100755 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -29,3 +29,5 @@ class TestReviews: assert len(ac.cardReviews(deck="test_deck", startID=0)) == 2 assert ac.getLatestReviewID(deck="test_deck") == 789 + assert ac.getReviewsOfCard(card=setup.card_ids[0]) == \ + [[456, setup.card_ids[0], -1, 3, 4, -60, 2500, 6157, 0]] From c0c922d27cf5477d80d5439f0d540ec0417f16cc Mon Sep 17 00:00:00 2001 From: Austin Siew <17107540+Aquafina-water-bottle@users.noreply.github.com> Date: Tue, 13 Sep 2022 17:12:20 -0600 Subject: [PATCH 2/3] changed getReviewsOfCard -> getReviewsOfCards, changed result to be formatted in a dictionary rather than a list --- README.md | 27 ++++++++++++++++----------- plugin/__init__.py | 13 +++++++++---- tests/test_stats.py | 4 ++-- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 5caa86d..87e631d 100644 --- a/README.md +++ b/README.md @@ -2759,18 +2759,21 @@ corresponding to when the API was available for use. } ``` -* **getReviewsOfCard** +* **getReviewsOfCards** - Requests all card reviews for a specific card ID. - Returns a list of 9-tuples in the same format as `cardReviews`: `(reviewTime, cardID, usn, buttonPressed, newInterval, previousInterval, newFactor, reviewDuration, reviewType)` + Requests all card reviews for each card ID. + Returns a dictionary mapping the card ID to 9-tuples in the same format as `cardReviews`: + `(reviewTime, cardID, usn, buttonPressed, newInterval, previousInterval, newFactor, reviewDuration, reviewType)` *Sample request*: ```json { - "action": "getReviewsOfCard", + "action": "getReviewsOfCards", "version": 6, "params": { - "card": "1653613948202" + "cards": [ + "1653613948202" + ] } } ``` @@ -2778,12 +2781,14 @@ corresponding to when the API was available for use. *Sample result*: ```json { - "result": [ - [1654102387663, 1653613948202, 1780, 3, 8, 3, 2500, 25796, 1], - [1654798974478, 1653613948202, 1861, 3, 20, 8, 2500, 18134, 1], - [1656556319328, 1653613948202, 2075, 3, 53, 20, 2500, 20530, 1], - [1661107990069, 1653613948202, 2478, 3, 131, 53, 2500, 24247, 1] - ], + "result": { + "1653613948202": [ + [1654102387663, 1653613948202, 1780, 3, 8, 3, 2500, 25796, 1], + [1654798974478, 1653613948202, 1861, 3, 20, 8, 2500, 18134, 1], + [1656556319328, 1653613948202, 2075, 3, 53, 20, 2500, 20530, 1], + [1661107990069, 1653613948202, 2478, 3, 131, 53, 2500, 24247, 1] + ] + }, "error": null } ``` diff --git a/plugin/__init__.py b/plugin/__init__.py index 661935e..edadf4b 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1374,10 +1374,15 @@ class AnkiConnect: @util.api() - def getReviewsOfCard(self, card): - return self.database().all( - 'select id, cid, usn, ease, ivl, lastIvl, factor, time, type from revlog where cid = ?', card - ) + def getReviewsOfCards(self, cards): + return { + card: self.database().all( + "select id, cid, usn, ease, ivl, lastIvl, factor, time, type from revlog where cid = ?", + card, + ) + for card in cards + } + @util.api() diff --git a/tests/test_stats.py b/tests/test_stats.py index 3237161..d3e94fd 100755 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -29,5 +29,5 @@ class TestReviews: assert len(ac.cardReviews(deck="test_deck", startID=0)) == 2 assert ac.getLatestReviewID(deck="test_deck") == 789 - assert ac.getReviewsOfCard(card=setup.card_ids[0]) == \ - [[456, setup.card_ids[0], -1, 3, 4, -60, 2500, 6157, 0]] + assert ac.getReviewsOfCards(cards=[setup.card_ids[0]]) == \ + {setup.card_ids[0]: [[456, setup.card_ids[0], -1, 3, 4, -60, 2500, 6157, 0]]} From a8905a4c22a6c4feb77c7064b37eba95e66ab9bc Mon Sep 17 00:00:00 2001 From: Austin Siew <17107540+Aquafina-water-bottle@users.noreply.github.com> Date: Tue, 20 Sep 2022 17:46:26 -0600 Subject: [PATCH 3/3] getReviewsOfCards: changed inner lists into dictionaries, removed cid within the inner list --- README.md | 41 +++++++++++++++++++++++++++++++++++------ plugin/__init__.py | 16 +++++++++------- tests/test_stats.py | 15 ++++++++++++++- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0b1d036..7516ac7 100644 --- a/README.md +++ b/README.md @@ -2767,8 +2767,21 @@ corresponding to when the API was available for use. * **getReviewsOfCards** Requests all card reviews for each card ID. - Returns a dictionary mapping the card ID to 9-tuples in the same format as `cardReviews`: - `(reviewTime, cardID, usn, buttonPressed, newInterval, previousInterval, newFactor, reviewDuration, reviewType)` + Returns a dictionary mapping each card ID to a list of dictionaries of the format: + ``` + { + "id": reviewTime, + "usn": usn, + "ease": buttonPressed, + "ivl": newInterval, + "lastIvl": previousInterval, + "factor": newFactor, + "time": reviewDuration, + "type": reviewType, + } + ``` + The reason why these key values are used instead of the more descriptive counterparts + is because these are the exact key values used in Anki's database. *Sample request*: ```json @@ -2788,10 +2801,26 @@ corresponding to when the API was available for use. { "result": { "1653613948202": [ - [1654102387663, 1653613948202, 1780, 3, 8, 3, 2500, 25796, 1], - [1654798974478, 1653613948202, 1861, 3, 20, 8, 2500, 18134, 1], - [1656556319328, 1653613948202, 2075, 3, 53, 20, 2500, 20530, 1], - [1661107990069, 1653613948202, 2478, 3, 131, 53, 2500, 24247, 1] + { + "id": 1653772912146, + "usn": 1750, + "ease": 1, + "ivl": -20, + "lastIvl": -20, + "factor": 0, + "time": 38192, + "type": 0 + }, + { + "id": 1653772965429, + "usn": 1750, + "ease": 3, + "ivl": -45, + "lastIvl": -20, + "factor": 0, + "time": 15337, + "type": 0 + } ] }, "error": null diff --git a/plugin/__init__.py b/plugin/__init__.py index edadf4b..e61e710 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1375,13 +1375,15 @@ class AnkiConnect: @util.api() def getReviewsOfCards(self, cards): - return { - card: self.database().all( - "select id, cid, usn, ease, ivl, lastIvl, factor, time, type from revlog where cid = ?", - card, - ) - for card in cards - } + COLUMNS = ['id', 'usn', 'ease', 'ivl', 'lastIvl', 'factor', 'time', 'type'] + QUERY = 'select {} from revlog where cid = ?'.format(', '.join(COLUMNS)) + + result = {} + for card in cards: + query_result = self.database().all(QUERY, card) + result[card] = [dict(zip(COLUMNS, row)) for row in query_result] + + return result diff --git a/tests/test_stats.py b/tests/test_stats.py index d3e94fd..6cf16df 100755 --- a/tests/test_stats.py +++ b/tests/test_stats.py @@ -30,4 +30,17 @@ class TestReviews: assert len(ac.cardReviews(deck="test_deck", startID=0)) == 2 assert ac.getLatestReviewID(deck="test_deck") == 789 assert ac.getReviewsOfCards(cards=[setup.card_ids[0]]) == \ - {setup.card_ids[0]: [[456, setup.card_ids[0], -1, 3, 4, -60, 2500, 6157, 0]]} + { + setup.card_ids[0]: [ + { + "id": 456, + "usn": -1, + "ease": 3, + "ivl": 4, + "lastIvl": -60, + "factor": 2500, + "time": 6157, + "type": 0, + } + ] + }