-
Notifications
You must be signed in to change notification settings - Fork 0
/
anki.py
151 lines (126 loc) · 4.83 KB
/
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import requests
import json
import shutil
import os
import time
def add_card(deck_name, front, back, card_model_name="Basic"):
# AnkiConnect API URL
url = "http://localhost:8765"
# Data for the AnkiConnect request
data = {
"action": "addNote",
"version": 6,
"params": {
"note": {
"deckName": deck_name,
"modelName": card_model_name,
"fields": {
"Front": front,
"Back": back
},
"tags": []
}
}
}
# Make the request to AnkiConnect
response = requests.post(url, data=json.dumps(data))
if response.status_code == 200:
# Check for errors in adding the card
res = response.json()
if not res.get('error'):
print("Card added successfully.")
else:
print("Error adding card:", res['error'])
else:
print("Failed to connect to AnkiConnect.")
# Example usage
#deck_name = "MyDeck"
#front_content = "What is the capital of France?"
#back_content = "Paris"
#add_card(deck_name, front_content, back_content)
def add_card_with_images(deck_name, front_image_files, back_image_files, card_model_name="Basic"):
# Format the front and back contents to include images
front_content = ''
for front_image_file in front_image_files:
front_content += f'<img src="{front_image_file}">'
back_content = ''
print(len(back_image_files))
for back_image_file in back_image_files:
back_content += f'<img src="{back_image_file}">'
# AnkiConnect API URL
url = "http://localhost:8765"
# Data for the AnkiConnect request
data = {
"action": "addNote",
"version": 6,
"params": {
"note": {
"deckName": deck_name,
"modelName": card_model_name,
"fields": {
"Front": front_content,
"Back": back_content
},
"tags": []
}
}
}
# Make the request to AnkiConnect
response = requests.post(url, data=json.dumps(data))
if response.status_code == 200:
# Check for errors in adding the card
res = response.json()
if not res.get('error'):
print("Card added successfully.")
else:
print("Error adding card:", res['error'])
else:
print("Failed to connect to AnkiConnect.")
def generate_unique_filename(base_path, original_name):
# Split the original name into name and extension
name, extension = os.path.splitext(original_name)
# Generate a potential new name by appending a timestamp
unique_name = f"{name}_{int(time.time())}{extension}"
# Check if this file already exists in the folder
if not os.path.exists(os.path.join(base_path, unique_name)):
return unique_name
else:
# If the generated name exists (very unlikely), recurse until a unique name is found
return generate_unique_filename(base_path, unique_name)
def copy_image_to_anki_media(source_image_path, anki_media_folder_path = "/home/len1218/.local/share/Anki2/User 1/collection.media"):
# Ensure the Anki media folder exists
if not os.path.exists(anki_media_folder_path):
print(f"Anki media folder does not exist: {anki_media_folder_path}")
return
# Copy the image to the Anki media folder
try:
shutil.copy(source_image_path, anki_media_folder_path)
print(f"Successfully copied {source_image_path} to {anki_media_folder_path}")
except Exception as e:
print(f"Failed to copy image: {e}")
deck_name = "MyDeck"
anki_media_folder_path = "/home/len1218/.local/share/Anki2/User 1/collection.media"
# Example usage
#original_image_name = "image.jpg"
#unique_image_name = generate_unique_filename(anki_media_folder, original_image_name)
#print(f"Unique image name: {unique_image_name}")
#front_image_file = "front.jpg"
#back_image_file = "back.jpg"
#copy_image_to_anki_media(front_image_file)
#copy_image_to_anki_media(back_image_file)
def add_image_card_wrapper(front_list, back_list):
front_names = []
back_names = []
for i in range(len(front_list)):
front = front_list[i]
front_name = generate_unique_filename(anki_media_folder_path,"Front.png")
front_image_file = anki_media_folder_path + "/" + front_name
front.save(front_image_file)
front_names.append(front_name)
for i in range(len(back_list)):
back = back_list[i]
back_name = generate_unique_filename(anki_media_folder_path,"Back.png")
back_image_file = anki_media_folder_path + "/"+ back_name
back.save(back_image_file)
back_names.append(back_name)
add_card_with_images(deck_name, front_names, back_names)