-
Notifications
You must be signed in to change notification settings - Fork 3
/
goldendict.py
319 lines (268 loc) · 10.3 KB
/
goldendict.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import time
import os
from PyQt5.QtCore import QMimeData
from PyQt5.Qt import QApplication
import aqt, anki
from aqt.utils import showInfo
from functools import partial
from aqt import mw
# from aqt.qt import *
from .global_hotkeys import *
CONFIG = mw.addonManager.getConfig(__name__)
doCrossProfileSearch = CONFIG['crossProfileName']
AUTO_PASTE_FIELDS = {
"image": CONFIG['image'],
"audio": CONFIG["audio"],
"text": CONFIG["text"],
"vocab": CONFIG["vocab"],
"definition":CONFIG["definition"]
}
"""
from importlib import reload
gd = __import__('247820692')
reload(gd.goldendict)
gd.goldendict.start()
# from aqt.utils import showCritical
# import traceback
# # showInfo(str(ankiNote))
# d = aqt.dialogs.open('AddCards', self.window())
# dialog.activateWindow()
# try:
# from autobulk import autobulk
# autobulk.run_images([d.editor.note.id])
# except Exception as e:
# showCritical(str( traceback.print_exc()))
"""
from aqt.qt import QObject, pyqtSignal
class AutoThread(QObject):
release = pyqtSignal(dict)
def __init__(self, mw):
super(AutoThread, self).__init__(mw)
GLOBAL_AUTO_COPY = CONFIG['hotkey']
bindings = [
[GLOBAL_AUTO_COPY, None, self.on_release],
]
register_hotkeys(bindings)
start_checking_hotkeys()
def on_release(self):
self.release.emit({})
return True
def runAutoBulk(ankiNote):
try:
autobulk = __import__("autobulk")
autobulk.autobulk.run_add_card_routine([ankiNote.id])
except:
pass
def getDialog(openNew=True):
# for k, d in aqt.DialogManager._dialogs.items():
# currDialog = d[1]
# if currDialog:
# a = 'act' + str(currDialog.windowState() == aqt.qt.Qt.WindowActive)
# b = 'no' + str(currDialog.windowState() == aqt.qt.Qt.WindowNoState)
# c = 'ful' + str(currDialog.windowState() == aqt.qt.Qt.WindowFullScreen)
# dd = 'min' + str(currDialog.windowState() == aqt.qt.Qt.WindowMinimized)
# e = 'max' + str(currDialog.windowState() == aqt.qt.Qt.WindowMaximized)
# showInfo(f'{k}\n{a}\n{b}\n{c}\n{dd}\n{e}')
# if currDialog and currDialog.windowState() == aqt.qt.Qt.WindowActive:
# currDialog.activateWindow()
# return currDialog
dialog = aqt.DialogManager._dialogs["EditCurrent"][1]
# showInfo(str(aqt.DialogManager._dialogs))
if dialog is None:
dialog = aqt.DialogManager._dialogs["AddCards"][1]
if dialog is None:
dialog = aqt.DialogManager._dialogs["Browser"][1]
if dialog is None and openNew:
mw.activateWindow()
dialog = aqt.dialogs.open('AddCards', mw.window())
dialog.activateWindow()
return dialog
def getEditor():
return getDialog().editor
def getAutoPasteContent(editor, mime):
html, internal = editor.web._processMime(mime)
html = editor._pastePreFilter(html, internal)
return html
def getAutoPasteField(fields, content, mime, noText=True):
field = None
if mime.hasImage():
field = fields['image']
elif '[sound:' in content:
field = fields['audio']
elif not noText and mime.hasText():
field = fields['text']
return field
def doAutoPaste(editor, noText=True, isRerun=False):
if editor.note.id != 0: # editor in add mode
editor.note.flush()
mime = editor.mw.app.clipboard().mimeData()
autoPasteContent = getAutoPasteContent(editor, mime)
autoPasteField = getAutoPasteField(AUTO_PASTE_FIELDS, autoPasteContent, mime, noText)
if autoPasteField in editor.note and (autoPasteContent not in editor.note[autoPasteField]):
editor.loadNote()
same = getAutoPasteContent(editor, editor.mw.app.clipboard().mimeData()) == autoPasteContent
if not same:
if not isRerun:
doAutoPaste(editor, noText, True)
return
editor.note[autoPasteField] += autoPasteContent
editor.loadNote()
def runSendToAnki(receivedContent):
if CONFIG['model'] is not None:
model = mw.col.models.byName(CONFIG['model'])
if model is None:
raise Exception('model was not found: {}'.format(CONFIG['model']))
mw.col.models.setCurrent(model)
mw.col.models.update(model)
addCards = None
receivedFields = receivedContent['fields']
ankiNote = anki.notes.Note(mw.col, model)
# showInfo(str(note) + AUTO_PASTE_FIELDS["vocab"])
if 'fields' in receivedContent:
dialog = getDialog()
editor = dialog.editor
# doAutoPaste(editor)
fields = [AUTO_PASTE_FIELDS['vocab'], AUTO_PASTE_FIELDS['definition']]
for fieldName in fields:
existingValue = editor.note[fieldName]
if fieldName in ankiNote:
if fieldName == AUTO_PASTE_FIELDS["vocab"]:
# Don't Copy Vocab Again
if not existingValue:
editor.note[fieldName] = receivedFields['vocab']
else:
if receivedFields['definition'] not in existingValue:
if existingValue:
editor.note[fieldName] += '<br/><br/>'+receivedFields['definition']
else:
editor.note[fieldName] = receivedFields['definition']
editor.loadNote()
# doAutoPaste(editor)
dialog.activateWindow()
def autoPasteListener(receivedContent):
noText = False
if receivedContent:
runSendToAnki(receivedContent)
noText = True
d = getDialog()
# showInfo('listener')
if d:
doAutoPaste(d.editor, noText)
def focusWindowChangedListener(focuWindow):
showInfo('here')
showInfo(str(focuWindow))
def start():
try:
ac = __import__('2055492159')
except:
raise Exception('Failed to import AnkiConnect module')
@ac.util.api()
def copyToClipboard(self, text):
clipboard = QApplication.clipboard()
clipboard.clear()
reportRichTextMime = QMimeData()
reportRichTextMime.setHtml(text)
reportRichTextMime.setText(text)
clipboard.setMimeData(reportRichTextMime)
if clipboard.text():
return True
else:
return False
@ac.util.api()
def sendToAnki(self, note={}):
mw.gdThread.release.emit(note)
return 0
@ac.util.api()
def _searchCollection(self, query, desiredFields, collection=None):
cards = collection.findCards(query)
result = []
suspended = []
new = []
scheduler = self.scheduler()
for cid in cards:
# try:
card = collection.getCard(cid)
model = card.model()
note = card.note()
fields = {}
for info in model['flds']:
order = info['ord']
name = info['name']
if name in desiredFields:
fields[name] = {'value': note.fields[order], 'order': order}
entry = {
'cardId': card.id,
'fields': fields,
'fieldOrder': card.ord,
# 'question': util.getQuestion(card),
# 'answer': util.getAnswer(card),
'modelName': model['name'],
'ord': card.ord,
'deckName': collection.decks.get(card.did)['name'],
# 'css': model['css'],
'factor': card.factor,
# This factor is 10 times the ease percentage,
# so an ease of 310% would be reported as 3100
'interval': card.ivl,
'note': card.nid,
'type': card.type,
'queue': card.queue,
'due': card.due,
'reps': card.reps,
'lapses': card.lapses,
'left': card.left,
}
dueDate = "N/A"
if card.queue <= 0:
dueDate = card.due
else:
dueDate = time.time() + ((card.due - scheduler.today) * 86400)
try:
dueDate = time.strftime("%Y-%m-%d", time.localtime(dueDate))
except:
pass
if card.queue < 0:
dueDate = f'({dueDate})'
entry['dueDate'] = dueDate
if card.queue < 0:
suspended.append(entry)
elif card.queue == 0:
new.append(entry)
else:
result.append(entry)
# except TypeError as e:
# # Anki will give a TypeError if the card ID does not exist.
# # Best behavior is probably to add an 'empty card' to the
# # returned result, so that the items of the input and return
# # lists correspond.
# result.append({})
# return result
# return sorted(result, key=lambda k: (k['reps'], k['due']), reverse=False)
result = sorted(result, key=lambda k: (k['reps'], k['due']), reverse=True)
suspended = sorted(suspended, key=lambda k: (k['queue'], k['due']), reverse=False)
new = sorted(new, key=lambda k: (k['queue'], k['due']), reverse=False)
return result + new + suspended
@ac.util.api()
def pickCollection(self, pickCrossProfile=False):
"""
returns either the current collection or a specified cross-profile collection
"""
if pickCrossProfile:
crossCollectionFilename = os.path.join(mw.pm.base, CONFIG['crossProfileName'], 'collection.anki2')
crossCollection = anki.Collection(crossCollectionFilename)
return crossCollection
else:
return self.collection()
@ac.util.api()
def goldenCardsInfo(self, query, desiredFields):
result = _searchCollection(self, query, desiredFields, collection=pickCollection(self, pickCrossProfile=False))
crossResult = []
if doCrossProfileSearch:
crossResult = _searchCollection(self, query, desiredFields, collection=pickCollection(self, pickCrossProfile=True))
return result + crossResult
ac.AnkiConnect.copyToClipboard = copyToClipboard
ac.AnkiConnect.sendToAnki = sendToAnki
ac.AnkiConnect.goldenCardsInfo = goldenCardsInfo
def initGlobalHotkeys():
mw.gdThread = AutoThread(mw)
mw.gdThread.release.connect(autoPasteListener)