Repository navigation
Description
Hi,
First of all, thank you for this wonderful piece of software. It can save a lot of time.
I'm using it to extract certain data from online Oxford dictionary: Pronunciation transcription in phonetics symbols. A friend of mine helped me to rewrite the code to get extract this type of information as by default the add-on was able to extract a different type of info (a type of speech: verb noun, etc.)
By adding last three line of code in 'oxford.py' we were able to fetch data I wanted to go into my anki deck.
#-*- coding:utf-8 -*-
import urllib2
from urllib2 import quote
import json
from aqt.utils import showInfo
from .base import WebService, export, register, with_styles
@register("Oxford")
class Oxford(WebService):
def __init__(self):
super(Oxford, self).__init__()
def _get_from_api(self, lang="en"):
word = self.word
baseurl = "https://od-api.oxforddictionaries.com/api/v1"
app_id = "45aecf84"
app_key = "bb36fd6a1259e5baf8df6110a2f7fc8f"
headers = {"app_id": app_id, "app_key": app_key}
word_id = quote(word.lower().replace(" ", "_"))
url = baseurl + "/entries/" + lang + "/" + word_id
url = urllib2.Request(url, headers=headers)
response = json.loads(urllib2.urlopen(url).read())
return response["results"]
@export("Lexical Category", 1)
def _fld_category(self):
s = ""
s += self._get_from_api()[0]["lexicalEntries"][0]["pronunciations"][0]["phoneticSpelling"]
return s
The problem occurs when addon fetches data from online dictionary and dictionary does not have a record for a particular word. This is the error message:
An error occurred in an add-on.
Please post on the add-on forum:
https://anki.tenderapp.com/discussions/add-ons
Traceback (most recent call last):
File "C:\Users\kodi\AppData\Roaming\Anki2\addons\wquery\query.py", line 324, in run
result = self.query(service_field, word)
File "C:\Users\kodi\AppData\Roaming\Anki2\addons\wquery\query.py", line 337, in query
return self.service.active(service_field, word)
File "C:\Users\kodi\AppData\Roaming\Anki2\addons\wquery\service\base.py", line 173, in active
result = each1
File "C:\Users\kodi\AppData\Roaming\Anki2\addons\wquery\service\base.py", line 58, in _deco
res = fld_func(cls, *args, **kwargs)
File "C:\Users\kodi\AppData\Roaming\Anki2\addons\wquery\service\oxford.py", line 32, in _fld_category
s += self._get_from_api()[0]["lexicalEntries"][0]["pronunciations"][0]["phoneticSpelling"]
KeyError: 'pronunciations'
After I click 'OK' on the error screen process is continued ... until next error message.
Is there are a way to get rid of this error message?
Activity