-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
57 lines (42 loc) · 1.55 KB
/
utils.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
import os
import requests
from variables import ANKI_CONNECT_URL
def clear_and_create_file(directory, name_of_text_file):
# Delete all files in the specified directory
for file in os.listdir(directory):
file_path = os.path.join(directory, file)
if os.path.isfile(file_path):
os.remove(file_path)
# Delete the txt file
txt_file_path = os.path.join(name_of_text_file + ".txt")
if os.path.exists(txt_file_path):
os.remove(txt_file_path)
else:
print("The file does not exist. Please clear your text file manually before running the script again.")
# Make a new txt file
with open(txt_file_path, "w") as file:
file.write("")
def import_deck(deck_path, name_of_text_file):
# AnkiConnect endpoint URL to import the deck
anki_url = ANKI_CONNECT_URL
print(f"wow anki_url: {anki_url}")
# Get the absolute path of the deck file
absolute_deck_path = os.path.abspath(deck_path)
# Import deck request parameters
params = {
"action": "importPackage",
"params": {
"path": absolute_deck_path
}
}
# Send POST request to AnkiConnect
response = requests.post(anki_url, json=params)
# Check if the request was successful
if response.status_code == 200:
print("Deck imported successfully.")
# Use the new utility function
clear_and_create_file('./files', "words")
else:
print("Failed to import deck.")
def remove_duplicates(string_array):
return list(dict.fromkeys(string_array))