Created
October 19, 2014 19:15
-
-
Save eddie/dfa1f2a49689925fbf62 to your computer and use it in GitHub Desktop.
Kanji Meaning Anki Addon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # LookupKanji 0.2 | |
| # Anki1 Version based on jmrGloss | |
| # Ported to Anki2 with help of AwesomeTTS | |
| # By shufps.wordpress.com (shufps80@gmail.com) | |
| # Fixes (eblundell@gmail.com) | |
| import subprocess, re, urllib, urllib2 | |
| from PyQt4.QtCore import * | |
| from PyQt4.QtGui import * | |
| from aqt import mw, utils | |
| from aqt.utils import tooltip | |
| from anki.hooks import addHook | |
| from aqt.utils import showInfo, askUser | |
| from aqt.qt import * | |
| import string | |
| srcField_name="Expression" | |
| dstField_name="KanjiX" | |
| def fetchKanji_find(x, s, e): | |
| sidx = x.find(s) | |
| if sidx == -1: | |
| return '' | |
| eidx = x.find(e, sidx) | |
| if eidx == -1: | |
| return '' | |
| return x[sidx+len(s):eidx] | |
| def fetchKanjiSub(exp): | |
| expx = u'' | |
| for ch in unicode(exp): | |
| if ord(ch) < 19968: | |
| continue | |
| expx = expx + ch | |
| try: | |
| expx = string.replace(expx, u'\uff5e', u'\u301c').encode('utf-8') | |
| except UnicodeEncodeError: | |
| pass | |
| if expx == '': | |
| return '' | |
| url = 'http://www.csse.monash.edu.au/~jwb/cgi-bin/wwwjdic.cgi?1D' | |
| s = { 'kanjsel' : 'X', 'ksrchkey' : expx, 'jouyoulim' : 'on', 'strcnt' : '' } | |
| data = urllib.urlencode(s) | |
| u = urllib.urlopen(url, data).read().decode('utf-8') | |
| html = '' | |
| while True: | |
| start = u.find('<INPUT TYPE="radio" NAME="dsrchkey" VALUE="') | |
| end = u.find('<br>', start) | |
| if start == -1 or end == -1: | |
| break; | |
| part = u[start:end] | |
| kanji = fetchKanji_find(part, '<font size="+3">', '</font>') | |
| meaning = fetchKanji_find(part, '<td>English meanings</td><td><b>', '</b></td>') | |
| html = html + kanji + ": "+meaning+"<br>\n" | |
| u = u[end:-1] | |
| return html | |
| #### Update facts with Kanjis | |
| def onFetchKanji( self ): | |
| sf = self.selectedNotes() | |
| if not sf: | |
| utils.showInfo("Select the notes and then use the Lookup Kanjis plugin") | |
| return | |
| self.mw.checkpoint(_("Lookup Kanjis")) | |
| self.mw.progress.start(immediate=True, label="Looking up Kanjis ...") | |
| self.model.beginReset() | |
| (n, missing, ok, nop, failed) = fetchKanji( sf ) | |
| self.model.endReset() | |
| self.mw.progress.finish() | |
| utils.showInfo( | |
| str(n)+" total notes\n"+ | |
| str(ok)+" notes sucessfully fetched\n"+ | |
| str(missing)+" notes are missing tags "+srcField_name+" and/or "+dstField_name+"\n"+ | |
| str(nop)+" notes not updated (nothing overwritten)\n"+ | |
| str(failed)+" notes failed" | |
| ) | |
| def fetchKanji( fids ): | |
| nelements = len(fids) | |
| failed=0 | |
| notupdated=0 | |
| missingtag=0 | |
| ok=0 | |
| for c, id in enumerate( fids ): | |
| note = mw.col.getNote(id) | |
| if (not srcField_name in note.keys() or not dstField_name in note.keys()): | |
| missingtag+=1 | |
| note.flush() | |
| continue | |
| mw.progress.update(label="Looking up Kanji ... \n%s of %s\n%s" % (c+1, nelements,note[srcField_name])) | |
| try: | |
| old=note[dstField_name].strip() | |
| if old == "" or old == "<br />" or old == "<br/>": | |
| note[dstField_name] = fetchKanjiSub( note[srcField_name]) | |
| ok+=1 | |
| else: | |
| print "not updated: "+note[dstField_name].encode('utf-8') | |
| notupdated+=1 | |
| continue | |
| except: | |
| import traceback | |
| print 'lookupKanji failed:' | |
| traceback.print_exc() | |
| failed+=1 | |
| note.flush() | |
| return (nelements, missingtag, ok, notupdated, failed) | |
| def setupMenu(editor): | |
| a = QAction("Lookup Kanjis", editor) | |
| editor.form.menuEdit.addAction(a) | |
| editor.connect(a, SIGNAL("triggered()"), lambda e=editor: onFetchKanji(e)) | |
| addHook("browser.setupMenus", setupMenu) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment