1
yomichan-anki/yomi_base/anki_bridge.py

175 lines
4.4 KiB
Python
Raw Normal View History

2011-08-28 18:01:32 +00:00
# -*- coding: utf-8 -*-
2011-10-27 15:22:26 +00:00
# Copyright (C) 2013 Alex Yatskov
2011-08-28 18:01:32 +00:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2012-12-25 16:25:10 +00:00
import anki
import aqt
2016-07-17 18:38:03 +00:00
import hashlib
import urllib2
2011-08-28 18:01:32 +00:00
2016-07-17 18:38:03 +00:00
#
# 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
#
2011-08-28 18:01:32 +00:00
class Anki:
2016-07-17 18:38:03 +00:00
def addNote(self, deckName, modelName, fields, tags, audio):
collection = self.collection()
if collection is None:
return
2012-12-25 02:53:41 +00:00
note = self.createNote(deckName, modelName, fields, tags)
if note is None:
return
2016-07-17 18:38:03 +00:00
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)
2016-07-17 18:38:03 +00:00
self.startEditing()
collection.addNote(note)
collection.autosave()
return note.id
2011-08-28 18:01:32 +00:00
2012-12-24 17:24:02 +00:00
def canAddNote(self, deckName, modelName, fields):
return bool(self.createNote(deckName, modelName, fields))
2011-08-28 18:01:32 +00:00
2016-05-02 01:42:17 +00:00
def createNote(self, deckName, modelName, fields, tags=[]):
collection = self.collection()
if collection is None:
return
model = collection.models.byName(modelName)
2012-12-24 03:13:45 +00:00
if model is None:
return
2011-08-28 18:01:32 +00:00
deck = collection.decks.byName(deckName)
2012-12-24 17:24:02 +00:00
if deck is None:
return
2012-12-24 17:24:02 +00:00
note = anki.notes.Note(collection, model)
2012-12-24 17:24:02 +00:00
note.model()['did'] = deck['id']
note.tags = tags
2012-12-24 03:13:45 +00:00
2012-12-24 02:54:05 +00:00
for name, value in fields.items():
if name in note:
note[name] = value
2012-12-24 02:54:05 +00:00
2012-12-25 16:25:10 +00:00
if not note.dupeOrEmpty():
return note
2011-08-28 18:01:32 +00:00
2012-12-25 02:58:39 +00:00
def browseNote(self, noteId):
2012-12-25 16:25:10 +00:00
browser = aqt.dialogs.open('Browser', self.window())
browser.form.searchEdit.lineEdit().setText('nid:{0}'.format(noteId))
browser.onSearch()
2011-08-28 18:01:32 +00:00
2012-12-25 02:53:41 +00:00
def startEditing(self):
self.window().requireReset()
2011-08-28 18:01:32 +00:00
2012-12-25 02:53:41 +00:00
def stopEditing(self):
if self.collection() is not None:
2012-12-25 16:39:21 +00:00
self.window().maybeReset()
2012-12-25 02:53:41 +00:00
def window(self):
return aqt.mw
2011-08-28 18:01:32 +00:00
2012-12-25 17:11:51 +00:00
def addUiAction(self, action):
self.window().form.menuTools.addAction(action)
2011-08-28 18:01:32 +00:00
def collection(self):
return self.window().col
2011-08-28 18:01:32 +00:00
2016-07-17 18:38:03 +00:00
def media(self):
collection = self.collection()
if collection is not None:
return collection.media
2012-12-24 00:02:00 +00:00
def modelNames(self):
collection = self.collection()
if collection is not None:
return collection.models.allNames()
2012-12-24 00:02:00 +00:00
2012-12-25 02:53:41 +00:00
def modelFieldNames(self, modelName):
collection = self.collection()
if collection is None:
return
model = collection.models.byName(modelName)
2012-12-25 16:25:10 +00:00
if model is not None:
return [field['name'] for field in model['flds']]
2012-12-24 02:11:14 +00:00
def deckNames(self):
collection = self.collection()
if collection is not None:
return collection.decks.allNames()