-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathForvoTts.py
242 lines (214 loc) · 11.4 KB
/
ForvoTts.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
from aqt.qt import *
import aqt.sound
#from aqt.QtCore import QFile, QObject
from aqt import mw
from .AnkiAudioTools import languages, download_Audio, AnkiAudioGlobals, AnkiAudioObject, getDefiniteConfigPath
from .bs4Scraper import *
import os
import glob
from threading import *
import re
# TODO: To anyone even remotely familiar with QT, this probably looks horrendous. Revamp encouraged.
class ForvoTts(QDialog):
finalResult = None # to be a
openrussian_session = ""
def __init__(self, mw, targetNote, parent, focusedField, selectedText):
QDialog.__init__(self, parent or mw)
#super(ForvoTts, self).__init__(parent)
self.textBox = QLineEdit(self)
self.textBox.setGeometry(QRect(200, 50, 150, 30))
self.textBox.setMinimumWidth(170)
self.textBox.setPlaceholderText("Search...")
self.config = mw.addonManager.getConfig(__name__)
self.focusedField = focusedField
self.textBox.setText(selectedText)
self.targetNote = targetNote
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.setObjectName("TTSDialog")
Dialog.resize(320, 250) #w h
Dialog.setWindowTitle("Forvo TTS for")
# finish button
self.pushButtonStart = QPushButton(Dialog)
self.pushButtonStart.setGeometry(QRect(195, 350, 150, 28))
self.pushButtonStart.setObjectName("pushButtonStart")
self.pushButtonStart.clicked.connect(self.start_lookup)
self.pushButtonStart.setText("Search")
# Lang selection
self.languageSelectBox = QComboBox(Dialog)
self.languageSelectBox.addItems(languages)
self.languageSelectBox.setStyleSheet("combobox-popup: 0;")
try:
self.languageSelectBox.setCurrentIndex(self.languageSelectBox.findText(self.config["LastSelectedLanguage"]))
except:
pass
# label destination field
self.lblDestinationField = QLabel(Dialog)
self.lblDestinationField.setGeometry(QRect(20, 45, 120, 30))
self.lblDestinationField.setText("Destination Field:")
# destination field combobox
self.destinationFieldComboBox = QComboBox(Dialog)
self.destinationFieldComboBox.setGeometry(QRect(140, 45, 150, 30))
self.destinationFieldComboBox.addItems(self.targetNote.keys())
try:
if(self.config["LastSelectedField"] in self.targetNote.keys()):
self.destinationFieldComboBox.setCurrentIndex(self.destinationFieldComboBox.findText(self.config["LastSelectedField"]))
elif(isinstance(self.focusedField, int)):
self.destinationFieldComboBox.setCurrentIndex(self.focusedField)
except:
if(isinstance(self.focusedField, int)):
self.destinationFieldComboBox.setCurrentIndex(self.focusedField)
if(eval(self.config["Remember language on a per deck basis"])):
try:
deck = mw.col.decks.get(self.targetNote.cards()[0].did)
except:
deck = mw.col.decks.current()
# Get current deck's description
description = deck["desc"]
# Lazy lookup for string inside brackers
pattern = r"\[(.*?)\]"
matches = re.findall(pattern, description) # Find all matches
if matches:
self.languageSelectBox.setCurrentIndex(self.languageSelectBox.findText(matches[0]))
#vbox
#self.mainContainerLayout = QVBoxLayout(Dialog)
#self.mainContainerLayout.setGeometry(QRect(0, 20, 320, 35))
# search hbox
self.searchHbox = QHBoxLayout(Dialog)
self.searchHbox.setGeometry(QRect(0, 30, 320, 35))
self.searchHbox.addWidget(self.textBox)
self.searchHbox.addWidget(self.languageSelectBox)
self.searchHbox.addWidget(self.pushButtonStart)
self.searchHbox.setAlignment(Qt.AlignmentFlag.AlignTop)
#self.mainContainerLayout.addLayout(self.searchHbox)
#label scrollfield results
self.lblScrollFieldResults = QLabel(Dialog)
self.lblScrollFieldResults.setGeometry(QRect(20, 70, 110, 30))
self.lblScrollFieldResults.setText("Results:")
#scroll area
self.scrollAreaFields = QScrollArea(Dialog)
self.scrollAreaFields.setEnabled(True)
self.scrollAreaFields.setGeometry(QRect(20, 100, 441, 121))
sizePolicy = QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scrollAreaFields.sizePolicy().hasHeightForWidth())
self.scrollAreaFields.setSizePolicy(sizePolicy)
self.scrollAreaFields.setFrameShape(QFrame.Shape.Box)
self.scrollAreaFields.setFrameShadow(QFrame.Shadow.Plain)
self.scrollAreaFields.setLineWidth(2)
self.scrollAreaFields.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
self.scrollAreaFields.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
self.scrollAreaFields.setWidgetResizable(True)
self.scrollAreaFields.setAlignment(Qt.AlignmentFlag.AlignLeading|Qt.AlignmentFlag.AlignLeft|Qt.AlignmentFlag.AlignTop)
self.scrollAreaFields.setObjectName("scrollAreaFields")
self.createScrollAreaWidgetContents()
self.setListVBox()
self.scrollAreaFields.setWidget(self.scrollAreaWidgetContents)
def setListVBox(self):
self.verticalLayout = QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setSizeConstraint(QLayout.SizeConstraint.SetDefaultConstraint)
self.verticalLayout.setContentsMargins(0, 10, 0, 0)
self.verticalLayout.setSpacing(6)
self.verticalLayout.setObjectName("verticalLayout")
def createScrollAreaWidgetContents(self):
self.scrollAreaWidgetContents = QWidget()
self.scrollAreaWidgetContents.setGeometry(QRect(0, 100, 300, 100))
sizePolicy = QSizePolicy(QSizePolicy.Policy.Ignored, QSizePolicy.Policy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents.sizePolicy().hasHeightForWidth())
self.scrollAreaWidgetContents.setSizePolicy(sizePolicy)
self.scrollAreaWidgetContents.setSizeIncrement(QSize(0, 0))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
def start_lookup(self, context):
self.currentSearchResults = {}
self.deleteItemsOfLayout(self.verticalLayout)
results = []
if(eval(self.config["Use sources besides Forvo"])):
results.extend(lookup_word_lingua_libre(self.textBox.text(), self.languageSelectBox.currentText().split("_")[1]))
results.extend(lookup_word(self.textBox.text(), self.languageSelectBox.currentText().split("_")[1]))
if(len(results) == 0 and self.languageSelectBox.currentText() == "Russian_ru" and eval(self.config["Use sources besides Forvo"])):
# Additional yandex translation.
results.extend(scrape_yandex_tts(self.textBox.text()))
self.lblScrollFieldResults.setText("OpenRussian:")
elif(len(results) == 0):
results.extend(forga_lookup(self.textBox.text(), self.languageSelectBox.currentText()))
if len(results) == 0:
self.lblScrollFieldResults.setText("No results found...")
else:
self.lblScrollFieldResults.setText("Results:")
else:
self.lblScrollFieldResults.setText("Results:")
for result in results:
print(result.getBucketFilename())
self.currentSearchResults[result.getBucketFilename()] = result
self.addResult(result) #add result to the scroll
def addResult(self, ankiAudioObject):
print("adding result")
#Hbox
container = QHBoxLayout()
#Label with the name
label = QLabel(self.scrollAreaWidgetContents)
label.setText(ankiAudioObject.getBucketFilename())
# Preview button
previewButton = QPushButton(self.scrollAreaWidgetContents)
previewButton.setText("Preview Audio")
previewButton.clicked.connect(lambda: self.previewAudio(ankiAudioObject))
# Add button
addButton = QPushButton(self.scrollAreaWidgetContents)
addButton.setText("Choose and close")
addButton.clicked.connect(lambda: self.insertIntoCard(ankiAudioObject))
# add them to the Hbox
container.addWidget(label)
container.addWidget(previewButton)
container.addWidget(addButton)
self.verticalLayout.addLayout(container)
def previewAudio(self, audioObject):
#download as temp, maybe do an exists check first?
fullpath = getDefiniteConfigPath() + AnkiAudioGlobals.TEMP_FILE_PREFIX + audioObject.getBucketFilename()
if(os.path.isfile(fullpath)):
print(fullpath + " is a file, playing...")
aqt.sound.play(fullpath)
else:
print(fullpath + " is not a file, downloading...")
download_Audio(audioObject.word, audioObject.link, getDefiniteConfigPath(), audioObject.getBucketFilename(), True)
aqt.sound.play(fullpath)
def insertIntoCard(self, ankiAudioObject):
#select the forvo audio to add.
fullpath = getDefiniteConfigPath() + AnkiAudioGlobals.TEMP_FILE_PREFIX + ankiAudioObject.getBucketFilename()
if(os.path.isfile(fullpath)): # if it was a temp file, rename it
print("Renaming temporary file ", ankiAudioObject.getBucketFilename() , "to permanent file")
os.replace(fullpath, getDefiniteConfigPath() + ankiAudioObject.getBucketFilename())
else: # else download it without temp prefix
print("Downloading ", ankiAudioObject.word, " to ", getDefiniteConfigPath() + ankiAudioObject.getBucketFilename())
download_Audio(ankiAudioObject.word, ankiAudioObject.link, getDefiniteConfigPath(), ankiAudioObject.getBucketFilename())
if(eval(self.config["Remember language on a per deck basis"])):
# Get current deck's description and adjust it if language
try:
deck = mw.col.decks.get(self.targetNote.cards()[0].did)
except:
deck = mw.col.decks.current()
description = deck["desc"]
if(not description.startswith("[" + self.languageSelectBox.currentText() +"]")):
deck["desc"] = "[" + self.languageSelectBox.currentText() +"]" + deck["desc"]
mw.col.decks.save(deck)
self.config["LastSelectedLanguage"] = self.languageSelectBox.currentText()
self.config["LastSelectedField"] = self.destinationFieldComboBox.currentText()
mw.addonManager.writeConfig(__name__, self.config)
self.deleteTempFiles() #TODO: maybe in seperate thread
self.finalResult = ankiAudioObject
self.accept()
def deleteItemsOfLayout(self, layout):
if layout is not None:
while layout.count():
item = layout.takeAt(0)
widget = item.widget()
if widget is not None:
widget.setParent(None)
else:
self.deleteItemsOfLayout(item.layout())
def deleteTempFiles(self):
for tempFile in glob.glob(getDefiniteConfigPath() + AnkiAudioGlobals.TEMP_FILE_PREFIX + '*'):
print("Deleting: ", tempFile)
os.remove(tempFile)