From 54a7105bf9da31dabe884a96464206a0fc87c58d Mon Sep 17 00:00:00 2001 From: thiswillbeyourgithub Date: Wed, 23 Feb 2022 02:31:43 +0000 Subject: [PATCH] implement setSpecificValueOfCard for user @TobiasWehrum (#303) Co-authored-by: thiswillbeyourgithub --- README.md | 29 +++++++++++++++++++++++++++++ plugin/__init__.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/README.md b/README.md index 3a94a72..a7966d4 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,35 @@ corresponding to when the API was available for use. } ``` + +* **setSpecificValueOfCard** + + Sets specific value of a single card. Given the risk of wreaking havor in the database when changing some of the values of a card, some of the keys require the argument "warning_check" set to True. + This can be used to set a card's flag, change it's ease factor, change the review order in a filtered deck and change the column "data" (not currently used by anki apparantly), and many other values. + A list of values and explanation of their respective utility can be found at [AnkiDroid's wiki](https://github.com/ankidroid/Anki-Android/wiki/Database-Structure). + + *Sample request*: + ```json + { + "action": "setSpecificValueOfCard", + "version": 6, + "params": { + "card": 1483959291685, + "keys": ["flags", "odue"], + "newValues": ["1", "-100"] + } + } + ``` + + *Sample result*: + ```json + { + "result": [true, true], + "error": null + } + ``` + + * **suspend** Suspend cards by card ID; returns `true` if successful (at least one card wasn't already suspended) or `false` diff --git a/plugin/__init__.py b/plugin/__init__.py index df09e4a..7f4ddf5 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -833,6 +833,38 @@ class AnkiConnect: return couldSetEaseFactors + @util.api() + def setSpecificValueOfCard(self, card, keys, + newValues, warning_check=False): + if isinstance(card, list): + print("card has to be int, not list") + return False + + if not isinstance(keys, list) or not isinstance(newValues, list): + print("keys and newValues have to be lists.") + return False + + if len(newValues) != len(keys): + print("Invalid list lengths.") + return False + + for key in keys: + if key in ["did", "id", "ivl", "lapses", "left", "mod", "nid", + "odid", "odue", "ord", "queue", "reps", "type", "usn"]: + if warning_check is False: + return False + + result = [] + try: + ankiCard = self.getCard(card) + for i, key in enumerate(keys): + setattr(ankiCard, key, newValues[i]) + ankiCard.flush() + result.append(True) + except Exception as e: + result.append([False, str(e)]) + return result + @util.api() def getEaseFactors(self, cards):