This commit is contained in:
Alex Yatskov 2016-07-16 22:56:02 -07:00
parent 42882667db
commit d937fc4307

View File

@ -33,10 +33,18 @@ API_VERSION = 1
# #
# Helpers # Audio helpers
# #
def downloadAudio(kana, kanji): def audioBuildFilename(kana, kanji):
filename = u'yomichan_{}'.format(kana)
if kanji:
filename += u'_{}'.format(kanji)
filename += u'.mp3'
return filename
def audioDownload(kana, kanji):
url = 'http://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kana={}'.format(urllib2.quote(kana.encode('utf-8'))) url = 'http://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kana={}'.format(urllib2.quote(kana.encode('utf-8')))
if kanji: if kanji:
url += '&kanji={}'.format(urllib2.quote(kanji.encode('utf-8'))) url += '&kanji={}'.format(urllib2.quote(kanji.encode('utf-8')))
@ -53,17 +61,14 @@ def downloadAudio(kana, kanji):
m = hashlib.md5() m = hashlib.md5()
m.update(data) m.update(data)
digest = m.hexdigest() if m.hexdigest() == '7e2c2f954ef6051373ba916f000168dc':
if digest == '7e2c2f954ef6051373ba916f000168dc':
return None return None
filename = u'yomichan_{}'.format(kana) return data
if kanji:
filename += u'_{}'.format(kanji)
filename += u'.mp3'
return filename, data
def audioFixupField(field, kana, kanji):
return field.replace(u'{audio}', audioBuildFilename(kana, kanji))
# #
@ -232,7 +237,7 @@ class AnkiBridge:
if collection is None: if collection is None:
return return
note = self.createNote(deckName, modelName, fields, audio, tags) note = self.createNote(deckName, modelName, fields, tags, audio)
if note is None: if note is None:
return return
@ -248,7 +253,7 @@ class AnkiBridge:
return bool(self.createNote(deckName, modelName, fields)) return bool(self.createNote(deckName, modelName, fields))
def createNote(self, deckName, modelName, fields, audio=[], tags=[]): def createNote(self, deckName, modelName, fields, tags=[], audio=None):
collection = self.collection() collection = self.collection()
if collection is None: if collection is None:
return return
@ -267,6 +272,8 @@ 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():
@ -371,8 +378,7 @@ class AnkiConnect:
results.append(self.anki.canAddNote( results.append(self.anki.canAddNote(
note['deckName'], note['deckName'],
note['modelName'], note['modelName'],
note['fields'], note['fields']
note.get('audio')
)) ))
return results return results