Fixups
This commit is contained in:
parent
b31341276f
commit
4edd72c103
@ -18,10 +18,56 @@
|
|||||||
|
|
||||||
import anki
|
import anki
|
||||||
import aqt
|
import aqt
|
||||||
|
import hashlib
|
||||||
|
import urllib2
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Audio helpers
|
||||||
|
#
|
||||||
|
|
||||||
|
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?kanji={}'.format(urllib2.quote(kanji.encode('utf-8')))
|
||||||
|
if kana:
|
||||||
|
url += '&kana={}'.format(urllib2.quote(kana.encode('utf-8')))
|
||||||
|
|
||||||
|
try:
|
||||||
|
resp = urllib2.urlopen(url)
|
||||||
|
except urllib2.URLError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if resp.code != 200:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return resp.read()
|
||||||
|
|
||||||
|
|
||||||
|
def audioIsPlaceholder(data):
|
||||||
|
m = hashlib.md5()
|
||||||
|
m.update(data)
|
||||||
|
return m.hexdigest() == '7e2c2f954ef6051373ba916f000168dc'
|
||||||
|
|
||||||
|
|
||||||
|
def audioInject(note, fields, filename):
|
||||||
|
for field in fields:
|
||||||
|
if field in note:
|
||||||
|
note[field] += u'[sound:{}]'.format(filename)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Anki
|
||||||
|
#
|
||||||
|
|
||||||
class Anki:
|
class Anki:
|
||||||
def addNote(self, deckName, modelName, fields, tags=[]):
|
def addNote(self, deckName, modelName, fields, tags, audio):
|
||||||
collection = self.collection()
|
collection = self.collection()
|
||||||
if collection is None:
|
if collection is None:
|
||||||
return
|
return
|
||||||
@ -30,8 +76,14 @@ class Anki:
|
|||||||
if note is None:
|
if note is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.startEditing()
|
if audio is not None and len(audio['fields']) > 0:
|
||||||
|
data = audioDownload(audio['kana'], audio['kanji'])
|
||||||
|
if data is not None and not audioIsPlaceholder(data):
|
||||||
|
filename = audioBuildFilename(audio['kana'], audio['kanji'])
|
||||||
|
audioInject(note, audio['fields'], filename)
|
||||||
|
self.media().writeData(filename, data)
|
||||||
|
|
||||||
|
self.startEditing()
|
||||||
collection.addNote(note)
|
collection.addNote(note)
|
||||||
collection.autosave()
|
collection.autosave()
|
||||||
|
|
||||||
@ -94,6 +146,12 @@ class Anki:
|
|||||||
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:
|
||||||
|
@ -70,7 +70,8 @@ class AnkiConnect:
|
|||||||
note['deckName'],
|
note['deckName'],
|
||||||
note['modelName'],
|
note['modelName'],
|
||||||
note['fields'],
|
note['fields'],
|
||||||
note['tags']
|
note['tags'],
|
||||||
|
note.get('audio')
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user