Skip to content

An error occurred in an add-on. #85

Open

Description

@kodi991

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?

Wrong behavior

When it happened

Whether it can be reproduced

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions