예제 #1
0
    def get_definitions(self, term):
        # if ' ' in term:
        #     Won't work so hot at this time, return an error
        # return 'Only single words are supported right now.'

        logging.info('Searching dictionary.com for term: %s', term)

        try:
            result = urllib2.urlopen(
                self.DICTIONARY_URI_TEMPLATE.format(urllib2.quote(term)))
            dat_soup = get_soup(result)
            definitions = get_definitions(
                dat_soup, self.DICTIONARY_DOT_COM_DEFINITIONS_CLASS)
            if not definitions:
                raise NoDefinitionFoundError
            word_difficulty = get_difficulty_index(dat_soup)
            word_type = get_word_type(dat_soup)

            return self._format_response(term, definitions, word_difficulty,
                                         word_type)
        except (NoDefinitionFoundError, urllib2.URLError) as ue:
            if ue.code == 404:
                return 'Can\'t find anything for *{}*!'.format(term)
            # We don't know (read: haven't implemented) what went wrong, return something generic.
            return 'Something went wrong with the request.'
    def get_definitions(self, term):
        # if ' ' in term:
        #     Won't work so hot at this time, return an error
            # return 'Only single words are supported right now.'

        logging.info('Searching dictionary.com for term: %s', term)

        try:
            result = urllib2.urlopen(self.DICTIONARY_URI_TEMPLATE.format(urllib2.quote(term)))
            dat_soup = get_soup(result)
            definitions = get_definitions(dat_soup, self.DICTIONARY_DOT_COM_DEFINITIONS_CLASS)
            if not definitions:
                raise NoDefinitionFoundError
            word_difficulty = get_difficulty_index(dat_soup)
            word_type = get_word_type(dat_soup)

            return self._format_response(term, definitions, word_difficulty, word_type)
        except (NoDefinitionFoundError, urllib2.URLError) as ue:
            if ue.code == 404:
                return 'Can\'t find anything for *{}*!'.format(term)
            # We don't know (read: haven't implemented) what went wrong, return something generic.
            return 'Something went wrong with the request.'