Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An error occurred in an add-on. #85

Open
kodi991 opened this issue Mar 18, 2018 · 0 comments
Open

An error occurred in an add-on. #85

kodi991 opened this issue Mar 18, 2018 · 0 comments

Comments

@kodi991
Copy link

kodi991 commented Mar 18, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant