-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_anki.py
78 lines (62 loc) · 2.29 KB
/
create_anki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from flask import Flask, request
import json
import urllib.request
app = Flask(__name__)
# Set up AnkiConnect URL
anki_url = "http://localhost:8765"
def anki_request(action, **params):
return {"action": action, "params": params, "version": 6}
def invoke(action, **params):
requestJson = json.dumps(anki_request(action, **params)).encode("utf-8")
response = json.load(
urllib.request.urlopen(urllib.request.Request(anki_url, requestJson))
)
if len(response) != 2:
raise Exception("response has an unexpected number of fields")
if "error" not in response:
raise Exception("response is missing required error field")
if "result" not in response:
raise Exception("response is missing required result field")
if response["error"] is not None:
raise Exception(response["error"])
return response["result"]
def get_notes_info(deck_name):
notes_ids = invoke("findNotes", query=f'deck:"{deck_name}"')
if notes_ids:
return invoke("notesInfo", notes=notes_ids)
return []
# Route for creating a new Anki note
@app.route("/anki/notes", methods=["POST"])
def create_anki_notes():
data = request.get_json()
deck_name = data.get("deck")
notes = data.get("notes")
if deck_name and notes:
invoke("createDeck", deck=deck_name)
existing_notes = get_notes_info(deck_name)
existing_backs = {note["fields"]["Back"]["value"] for note in existing_notes}
for note in notes:
front = note.get("Front")
back = note.get("Back")
if back in existing_backs:
print(f"Note {front}/{back[:10]}... already exists")
continue
anki_note = {
"deckName": deck_name,
"modelName": "Basic",
"fields": note,
"options": {"allowDuplicate": True},
"tags": [], # you can set tags here if needed
}
try:
invoke("addNote", note=anki_note)
except Exception as e:
print(f"Error adding note {front}: {str(e)}")
return "OK"
else:
return "Missing deck name or notes data", 400
@app.route("/")
def index():
return "FLASK CONNECTED"
if __name__ == "__main__":
app.run(debug=True, port=3001)