def handle_errors(self): super(WikipediaAPI, self).handle_errors() # Check general response content_type, data = self.content if "query" not in data: raise DSInvalidResource( 'Wrongly formatted Wikipedia response, missing "query"', resource=self) response = data['query'][ "tokens"] # Wiki has response hidden under single keyed dicts :( if not response: raise DSInvalidResource('Wikipedia response is empty', resource=self)
def content(self): content_type, data = super(GoogleText, self).content try: if data is not None: data["queries"]["request"][0]["searchTerms"] = data["queries"][ "request"][0]["searchTerms"][1:-1] except (KeyError, IndexError): raise DSInvalidResource( "Google Image resource does not specify searchTerms", self) return content_type, data
def content(self): """ This class gets revisions for a single page. So we only return the revisions and not all page info. :return: """ content_type, data = super(WikipediaQuery, self).content if data is None: return content_type, data try: page = next(iter(data["query"]["pages"].values())) except (KeyError, StopIteration, TypeError): raise DSInvalidResource( "{} resource did not contain 'query', 'pages' or a first page". format(self.__class__.__name__), resource=self) data["page"] = page return content_type, data
def handle_errors(self): super(WikipediaQuery, self).handle_errors() # Check general response content_type, data = self.content if "query" not in data: raise DSInvalidResource( 'Wrongly formatted Wikipedia response, missing "query"', resource=self) response = data['query'][ self. WIKI_RESULTS_KEY] # Wiki has response hidden under single keyed dicts :( # When searching for pages a dictionary gets returned # TODO: parse the dictionary and possibly return partial content if isinstance( response, dict) and "-1" in response and "missing" in response["-1"]: self.status = 404 raise DGHttpError40X(self.ERROR_MESSAGE, resource=self) # When making lists a list is returned elif isinstance(response, list) and not response: self.status = 404 raise DGHttpError40X(self.ERROR_MESSAGE, resource=self)