Fix audio

This commit is contained in:
Alex Yatskov 2020-03-05 19:45:13 -08:00
parent c64e78addf
commit dc8494f0fe

View File

@ -464,31 +464,37 @@ class AnkiConnect:
def addAudio(self, ankiNote, audioObjectOrList): def addAudio(self, ankiNote, audioObjectOrList):
if audioObjectOrList is not None: if audioObjectOrList is None:
audioList = audioObjectOrList if isinstance(audioObjectOrList, list) else [audioObjectOrList] return
for audio in audioList:
if audio is not None and len(audio['fields']) > 0:
try:
data = util.download(audio['url'])
skipHash = audio.get('skipHash')
if skipHash is None:
skip = False
else:
m = hashlib.md5()
m.update(data)
skip = skipHash == m.hexdigest()
if not skip: if isinstance(audioObjectOrList, list):
audioFilename = self.media().writeData(audio['filename'], data) audioList = audioObjectOrList
for field in audio['fields']: else:
if field in ankiNote: audioList = [audioObjectOrList]
ankiNote[field] += u'[sound:{}]'.format(audioFilename)
except Exception as e: for audio in audioList:
errorMessage = str(e).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;') if audio is not None and len(audio['fields']) > 0:
try:
data = util.download(audio['url'])
skipHash = audio.get('skipHash')
if skipHash is None:
skip = False
else:
m = hashlib.md5()
m.update(data)
skip = skipHash == m.hexdigest()
if not skip:
audioFilename = self.media().writeData(audio['filename'], data)
for field in audio['fields']: for field in audio['fields']:
if field in ankiNote: if field in ankiNote:
ankiNote[field] += errorMessage ankiNote[field] += u'[sound:{}]'.format(audioFilename)
except Exception as e:
errorMessage = str(e).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
for field in audio['fields']:
if field in ankiNote:
ankiNote[field] += errorMessage
@util.api() @util.api()
@ -509,12 +515,11 @@ class AnkiConnect:
if name in ankiNote: if name in ankiNote:
ankiNote[name] = value ankiNote[name] = value
audioObjectOrList = note['audio'] audioObjectOrList = note.get('audio')
self.addAudio(ankiNote, audioObjectOrList) self.addAudio(ankiNote, audioObjectOrList)
ankiNote.flush() ankiNote.flush()
@util.api() @util.api()
def addTags(self, notes, tags, add=True): def addTags(self, notes, tags, add=True):
self.startEditing() self.startEditing()