How to use the zdict.exceptions.QueryError function in zdict

To help you get started, we’ve selected a few zdict examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zdict / zdict / zdict / dictionaries / oxford.py View on Github external
def query(self, word: str):
        try:
            app_id, app_key = self._get_app_key()
            content = self._get_raw(word, headers={
                'app_id': app_id,
                'app_key': app_key
            })
        except QueryError as exception:
            msg = self.status_code.get(exception.status_code,
                                       'Some bad thing happened')
            self.color.print('Oxford: ' + msg, 'red')
            raise NotFoundError(exception.word)

        record = Record(
            word=word,
            content=content,
            source=self.provider,
        )
        return record
github zdict / zdict / zdict / dictionaries / wiktionary.py View on Github external
def query(self, word: str):
        try:
            content = self._get_raw(word)
        except QueryError as exception:
            raise NotFoundError(exception.word)

        content = json.loads(content)

        try:
            # Get the first definition string from JSON.
            content = content['en']
        except KeyError:
            # API can return JSON that does not contain 'en' language.
            raise NotFoundError(word)

        # Define a list that will be used to create a Record.
        r_content = []

        # For every part of speech append r_content corresponding list.
        for i, d in enumerate(content):
github zdict / zdict / zdict / dictionaries / yandex.py View on Github external
def query(self, word: str):
        try:
            content = self._get_raw(word)
        except QueryError as exception:
            raise NotFoundError(exception.word)

        content_json = json.loads(content)

        status = content_json.get('code')
        if status != 200:
            # https://tech.yandex.com/translate/doc/dg/reference/translate-docpage/#codes
            message = self.status_code.get(
                status,
                'Some bad thing happened with Yandex'
            )
            print('Yandex: ' + message)
            raise NotFoundError(word)

        record = Record(
            word=word,
github zdict / zdict / zdict / dictionaries / moe.py View on Github external
def query(self, word: str):
        try:
            content = self._get_raw(word)
        except QueryError as exception:
            raise NotFoundError(exception.word)

        record = Record(
            word=word,
            content=content,
            source=self.provider,
        )

        return record
github zdict / zdict / zdict / dictionary.py View on Github external
errors = {
                "BlockingIOError(36, 'Operation now in progress')":
                    "exceptions.TimeoutError()",
                "Failed to establish a new connection":
                    "exceptions.NoNetworkError()",
            }
            for error, exception in errors.items():
                if error in str(e.args):
                    raise eval(exception)
            else:
                raise exceptions.UnexpectedError()
        except Exception:
            raise exceptions.UnexpectedError()

        if res.status_code != 200:
            raise exceptions.QueryError(word, res.status_code)

        if self.args.debug:
            from bs4 import BeautifulSoup
            soup = BeautifulSoup(res.text, 'html.parser')
            print('=' * 30 + ' Start of debug info ' + '=' * 30)
            print(soup.prettify())
            print('=' * 30 + ' End of debug info ' + '=' * 30)

        return res.text