-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (43 loc) · 1.66 KB
/
main.py
File metadata and controls
59 lines (43 loc) · 1.66 KB
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
import requests
import json
from aqt import mw
from aqt.utils import showInfo, qconnect
from aqt.qt import *
from anki.importing import TextImporter
# set these 3 vars
tempFilePath = "/Users/gleb/Library/Application Support/Anki2/addons21/anki_reference_addon"
pathForResource = "/Users/gleb/Library/Application Support/Anki2/testuser/collection.media"
publicUrl = 'http://localhost:3000'
tempFileName = "/temp.txt"
pathForTitles = tempFilePath + tempFileName
def opener(path, flags):
return os.open(path, flags, 0o777)
def importTitles():
ti = TextImporter(mw.col, pathForTitles)
ti.initMapping()
ti.run()
def loadTitlesAndSave() -> None:
responseJson = requests.get(publicUrl + '/title')
response = json.loads(responseJson.content)
text = response['text']
if "".__eq__(text):
showInfo('All titles have been saved')
return
with open(pathForTitles , "wb", opener=opener) as f:
f.write(text.encode('utf-8'))
uuids = response['audioUUIDs']
for uuid in uuids:
responseResource = requests.get(publicUrl + '/resource/' + uuid)
resourceFileName = "/" + uuid + ".mp3"
with open(pathForResource + resourceFileName, "wb", opener=opener) as f:
f.write(responseResource.content)
importTitles()
# confirm titles were saved
requests.post(publicUrl + '/title', params={'titles': response['titleIds']})
showInfo(str(response['titleNames']))
# TODO make separate button for french?
action = QAction("Load new cards", mw)
# set it to call testFunction when it's clicked
qconnect(action.triggered, loadTitlesAndSave)
# and add it to the tools menu
mw.form.menuTools.addAction(action)