Creation of New Addon (very innovative idead, need someone to help)

I aim to create a addon that makes anki review like playing pool / carrom i.e. one needs to finish it’s 25 existing cards in a deck then automatically 25 more cards will be unlocked for unlimited times / rounds. i have few python codes regarding same one who known how to get it uploaded as addon contact : ). Grateful

overall concept
I want to replicate something like a “carom or pool” style approach, where you have a set of “balls” (cards) to pocket (review). Once those are finished, the system automatically increases new card and review limits so you can do more—until the next day, when everything resets to the default limits.

Concretely:

  1. Initial Daily Settings:
  • New Cards/Day = 15
  • Maximum Reviews/Day = 10
  1. After Finishing a Batch:
  • For example, once you finish your first 25 “new + review” cards, the add-on automatically adds +15 new cards and +10 to max reviews for that day.
  • You keep going in increments until you’re done.
  1. Next Day Reset:
  • At the start of a new day, the add-on resets your deck back to your base daily limits of 15 new cards and 10 reviews.

Main Code Below

import requests
import json
from datetime import date
import os

# -------------------------------
# Configuration & Global Constants
# -------------------------------
ANKICONNECT_URL = "http://localhost:8765"
CONFIG_FILE = "daily_config.json"  # File to keep our persistent counter

# Default deck limit values
DEFAULT_NEW = 15
DEFAULT_REV = 10

# Increase increments when a batch is finished
INCREMENT_NEW = 15
INCREMENT_REV = 10

# Threshold for the batch (e.g., after reviewing 25 cards, increase limits)
BATCH_THRESHOLD = 25

# Specify the target deck name (change this to your deck's name)
TARGET_DECK = "MyDeck"  # <-- Replace with your actual deck name

# -------------------------------
# Helper Functions for AnkiConnect API
# -------------------------------

def invoke(action, **params):
    """
    Sends a request to AnkiConnect and returns the result.
    """
    request_json = {"action": action, "version": 6, "params": params}
    response = requests.post(ANKICONNECT_URL, json=request_json).json()
    if "error" in response and response["error"]:
        raise Exception(f"AnkiConnect error: {response['error']}")
    return response["result"]

def set_deck_limits(deck_name, new_cards, max_reviews):
    """
    Updates the deck configuration for the given deck via AnkiConnect.
    This function assumes that AnkiConnect supports an action called 'updateDeckConfig'.
    If not, you may need to adjust or implement an alternative.
    """
    config = {
        "name": deck_name,
        "new": {"perDay": new_cards},
        "rev": {"perDay": max_reviews}
    }
    # The action "updateDeckConfig" is used here as a hypothetical example.
    result = invoke("updateDeckConfig", config=config)
    print(f"Deck '{deck_name}' config updated: new cards = {new_cards}, reviews = {max_reviews}")
    return result

# -------------------------------
# Persistence Functions
# -------------------------------

def load_persistent_config():
    """
    Loads persistent state from disk (counter and last update date).
    """
    if os.path.exists(CONFIG_FILE):
        with open(CONFIG_FILE, "r") as f:
            return json.load(f)
    else:
        # Initialize with today's date and zero batch count
        return {"date": str(date.today()), "batch_count": 0}

def save_persistent_config(config):
    """
    Saves persistent state to disk.
    """
    with open(CONFIG_FILE, "w") as f:
        json.dump(config, f)

def reset_if_new_day(persistent_config):
    """
    Checks if a new day has started. If so, resets the persistent counter
    and restores the default deck limits.
    """
    today_str = str(date.today())
    if persistent_config["date"] != today_str:
        # New day: update persistent data
        persistent_config["date"] = today_str
        persistent_config["batch_count"] = 0
        save_persistent_config(persistent_config)
        
        # Reset deck configuration to defaults
        set_deck_limits(TARGET_DECK, DEFAULT_NEW, DEFAULT_REV)
        print("A new day has begun; deck limits have been reset to default values.")

# -------------------------------
# Main Script Logic
# -------------------------------

def main():
    # Load persistent state (batch count and last update date)
    persistent_config = load_persistent_config()
    
    # Check for a new day and reset if necessary
    reset_if_new_day(persistent_config)
    
    # === STEP A: Gather Progress Information ===
    # For demonstration, we will prompt the user to input how many cards were reviewed
    # in the current batch.
    try:
        finished = int(input(f"Enter number of cards reviewed in this batch (threshold is {BATCH_THRESHOLD}): "))
    except Exception:
        print("Invalid input. Please enter a number.")
        return

    # Add the finished cards to our persistent batch counter
    persistent_config["batch_count"] += finished
    print(f"Accumulated reviewed cards for today: {persistent_config['batch_count']}")

    # === STEP B: Check if the threshold has been reached ===
    if persistent_config["batch_count"] >= BATCH_THRESHOLD:
        # Increase deck limits (for demonstration, we add our defined increments to defaults)
        new_limit = DEFAULT_NEW + INCREMENT_NEW
        rev_limit = DEFAULT_REV + INCREMENT_REV
        set_deck_limits(TARGET_DECK, new_limit, rev_limit)
        
        # Optionally: reset the counter or subtract the threshold so that partial progress counts
        persistent_config["batch_count"] = 0  # Resetting after increment
        print("Threshold reached: deck limits increased for today.")
    else:
        print("Threshold not reached yet. Keep reviewing to unlock extra cards!")
    
    # Save the updated persistent configuration
    save_persistent_config(persistent_config)

if __name__ == "__main__":
    main()
1 Like

You can find explanations in the manual on how to write add-ons. Sharing Add-ons - Writing Anki Add-ons

If you don’t use AnkiConnect, you may need to look for gui_hooks with the same functionality. Hooks and Filters - Writing Anki Add-ons

It is common to save Add-on data in Config. Otherwise, the data should be saved in a folder named user_files. (If not all the data will be deleted when the user updates the add-on.) Add-on Config - Writing Anki Add-ons

2 Likes