This commit is contained in:
Alex Yatskov 2016-07-17 09:38:33 -07:00
parent d937fc4307
commit 82fd651374

View File

@ -57,18 +57,19 @@ def audioDownload(kana, kanji):
if resp.code != 200: if resp.code != 200:
return None return None
data = resp.read() return resp.read()
def audioIsPlaceholder(data):
m = hashlib.md5() m = hashlib.md5()
m.update(data) m.update(data)
if m.hexdigest() == '7e2c2f954ef6051373ba916f000168dc': return m.hexdigest() == '7e2c2f954ef6051373ba916f000168dc'
return None
return data
def audioFixupField(field, kana, kanji): def audioInject(note, kana, kanji, fields):
return field.replace(u'{audio}', audioBuildFilename(kana, kanji)) for field in fields:
if field in note:
note[field] += u'[sound:{}]'.format(audioBuildFilename(kana, kanji))
# #
@ -237,14 +238,20 @@ class AnkiBridge:
if collection is None: if collection is None:
return return
note = self.createNote(deckName, modelName, fields, tags, audio) note = self.createNote(deckName, modelName, fields, tags)
if note is None: if note is None:
return return
self.startEditing() if audio is not None:
data = audioDownload(**audio)
if data is not None and not audioIsPlaceholder(data):
audioInject(note, **audio)
self.media().writeData(audioBuildFilename(**audio), data)
self.startEditing()
collection.addNote(note) collection.addNote(note)
collection.autosave() collection.autosave()
self.stopEditing()
return note.id return note.id
@ -253,7 +260,7 @@ class AnkiBridge:
return bool(self.createNote(deckName, modelName, fields)) return bool(self.createNote(deckName, modelName, fields))
def createNote(self, deckName, modelName, fields, tags=[], audio=None): def createNote(self, deckName, modelName, fields, tags=[]):
collection = self.collection() collection = self.collection()
if collection is None: if collection is None:
return return
@ -272,8 +279,6 @@ class AnkiBridge:
for name, value in fields.items(): for name, value in fields.items():
if name in note: if name in note:
if audio is not None:
value = audioFixupField(value, **audio)
note[name] = value note[name] = value
if not note.dupeOrEmpty(): if not note.dupeOrEmpty():
@ -303,6 +308,12 @@ class AnkiBridge:
return self.window().col return self.window().col
def media(self):
collection = self.collection()
if collection is not None:
return collection.media
def modelNames(self): def modelNames(self):
collection = self.collection() collection = self.collection()
if collection is not None: if collection is not None: