Created
June 17, 2015 08:43
-
-
Save Xifax/f36002ddf910993d6bfb to your computer and use it in GitHub Desktop.
Simple lookup addon for Anki.
This file contains 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 | |
""" | |
Simple addon to quickly lookup words in online Japanese thesaurus (jp-jp). | |
By default goo.ne.jp (国語 and 類語) is used. | |
Author: Artiom Basenko <demi.log@gmail.com> | |
License: The MIT License (MIT) | |
NB: One may easily change goo.ne.jp to something else. | |
""" | |
# Stdlib | |
import urllib | |
# Anki | |
from aqt.qt import * | |
from aqt.webview import AnkiWebView | |
from anki.hooks import addHook | |
# Qt | |
from PyQt4.QtGui import * | |
from PyQt4.QtCore import * | |
class Goo: | |
"""goo.ne.jp lookuper | |
May lookup either definition or synonym. | |
""" | |
# Lookup options | |
BASE_URL = 'http://dictionary.goo.ne.jp/srch/' | |
MEANING_URL = 'jn/%s/m0u/' | |
SYNONYM_URL = 'thsrs/%s/m1u/' | |
def get_selected(self, view): | |
"""Copy selected text""" | |
return view.page().selectedText() | |
def lookup_selected(self, view, url): | |
"""Open default browser and lookup selected item""" | |
url = QUrl.fromEncoded(self.BASE_URL + url % | |
urllib.quote( | |
self.get_selected(view).encode('utf8', 'ignore'))) | |
QDesktopServices.openUrl(url) | |
def add_action(self, view, menu, action, url): | |
"""Add 'lookup' action to context menu""" | |
action = menu.addAction(action) | |
action.connect(action, SIGNAL('triggered()'), | |
lambda view=view: self.lookup_selected(view, url)) | |
def lookup_meaning_action(self, view, menu): | |
"""Lookup meaning action""" | |
self.add_action(view, menu, | |
u'「%s」の意味' % self.get_selected(view)[:10], | |
self.MEANING_URL) | |
def lookup_synonym_action(self, view, menu): | |
"""Lookup synonym action""" | |
self.add_action(view, menu, | |
u'「%s」の類義語' % self.get_selected(view)[:10], | |
self.SYNONYM_URL) | |
# Add lookup actions to context menu | |
goo = Goo() | |
addHook("AnkiWebView.contextMenuEvent", goo.lookup_meaning_action) | |
addHook("AnkiWebView.contextMenuEvent", goo.lookup_synonym_action) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment