Skip to content

Instantly share code, notes, and snippets.

@htlin222
Created February 11, 2023 19:53
Show Gist options
  • Save htlin222/86dcda28216100fdc195ed7527da8641 to your computer and use it in GitHub Desktop.
Save htlin222/86dcda28216100fdc195ed7527da8641 to your computer and use it in GitHub Desktop.
python add_to_anki --deck="DECK_NAME" --front="Question here" --back="answer..."
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# title: add_to_anki
# date: "2023-02-12"
import requests
import argparse
## python add_to_anki --deck="DECK_NAME" --front="Question here" --back="answer..."
def main(deck_name, front, back):
# Define the Anki Connect API endpoint
endpoint = "http://localhost:8765"
# Define the API request
request = {
"action": "addNote",
"version": 6,
"params": {
"note": {
"deckName": deck_name,
"modelName": "Basic",
"fields": {
"Front": front,
"Back": back
},
"options": {
"allowDuplicate": False
},
"tags": []
}
}
}
# Send the API request
response = requests.post(endpoint, json=request).json()
# Check the response for errors
if "error" in response and response["error"] is not None:
print("Error:", response["error"])
else:
print("Card added successfully!")
def arguments():
# Create an argument parser
parser = argparse.ArgumentParser(description="Add a card to an Anki deck using Anki Connect.")
# Add arguments for the deck name, front, and back
parser.add_argument("--deck", type=str, help="The name of the deck to add the card to.")
parser.add_argument("--front", type=str, help="The front of the card.")
parser.add_argument("--back", type=str, help="The back of the card.")
# Parse the command-line arguments
args = parser.parse_args()
return args
# Define the deck name and card information using the arguments
if __name__ == '__main__':
args = arguments()
deck_name = args.deck
front = args.front
back = args.back
main(deck_name, front, back)
@htlin222
Copy link
Author

python add_to_anki --deck="DECK_NAME" --front="Question here" --back="answer..."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment