Accept multiple audio files in note creation and update

This commit is contained in:
Andreas Kienle 2020-02-23 11:59:19 +01:00
parent 08109dafbd
commit eafd3c94dd

View File

@ -449,7 +449,24 @@ class AnkiConnect:
def addNote(self, note): def addNote(self, note):
ankiNote = self.createNote(note) ankiNote = self.createNote(note)
audio = note.get('audio') audioObjectOrList = note.get('audio')
self.addAudio(ankiNote, audioObjectOrList)
collection = self.collection()
self.startEditing()
nCardsAdded = collection.addNote(ankiNote)
if nCardsAdded < 1:
raise Exception('The field values you have provided would make an empty question on all cards.')
collection.autosave()
self.stopEditing()
return ankiNote.id
def addAudio(self, ankiNote, audioObjectOrList):
if audioObjectOrList is not None:
audioList = audioObjectOrList if isinstance(audioObjectOrList, list) else [audioObjectOrList]
for audio in audioList:
if audio is not None and len(audio['fields']) > 0: if audio is not None and len(audio['fields']) > 0:
try: try:
data = util.download(audio['url']) data = util.download(audio['url'])
@ -473,16 +490,6 @@ class AnkiConnect:
if field in ankiNote: if field in ankiNote:
ankiNote[field] += errorMessage ankiNote[field] += errorMessage
collection = self.collection()
self.startEditing()
nCardsAdded = collection.addNote(ankiNote)
if nCardsAdded < 1:
raise Exception('The field values you have provided would make an empty question on all cards.')
collection.autosave()
self.stopEditing()
return ankiNote.id
@util.api() @util.api()
def canAddNote(self, note): def canAddNote(self, note):
@ -502,6 +509,9 @@ class AnkiConnect:
if name in ankiNote: if name in ankiNote:
ankiNote[name] = value ankiNote[name] = value
audioObjectOrList = note['audio']
self.addAudio(ankiNote, audioObjectOrList)
ankiNote.flush() ankiNote.flush()