class WeatherAPI(object): def __init__(self, request_api=None): if request_api is None: self._requests_api = RequestsAPI() else: self._requests_api = request_api def ask_question(self, url, question, botid): payload = {'apikey': botid, 'q': question} locresponse = self._requests_api.get( url + '/locations/v1/cities/autocomplete', params=payload) if locresponse is None: raise Exception("No Such Location from weather service") receive_payload = json.loads(locresponse.text, encoding="utf-8") weresponse = self._requests_api.get( url + '/currentconditions/v1/' + receive_payload[0]['Key'], payload) weatherinfo = json.loads(weresponse.text, encoding="utf-8") return "Current Weather Conditions in " + receive_payload[0][ 'LocalizedName'] + ", " + receive_payload[0]['Country'][ 'ID'] + " is " + weatherinfo[0][ 'WeatherText'] + " and temperature is " + str( weatherinfo[0]['Temperature']['Imperial']['Value'] ) + weatherinfo[0]['Temperature']['Imperial']['Unit']
class PannousAPI(object): def __init__(self, request_api=None): if request_api is None: self._requests_api = RequestsAPI() else: self._requests_api = request_api def ask_question(self, url, question, login): payload = {'input': question, 'login': login} response = self._requests_api.get(url, params=payload) if response is None: raise Exception("No response from pannous service") json_data = response.json() if 'output' not in json_data: raise Exception("'output' section missing from pannous json_data") if json_data["output"] is None or not json_data["output"]: raise Exception("'output' section has no elements in pannous json_data") if 'actions' not in json_data["output"][0]: raise Exception("'actions' section in output[0] in pannous json_data") if 'say' not in json_data["output"][0]['actions']: raise Exception("'say' section missing from output[0]['actions'] in pannous json_data") if 'text' not in json_data["output"][0]['actions']['say']: raise Exception("'text' section missing from output[0]['actions']['say'] in pannous json_data") return json_data["output"][0]['actions']['say']['text']
class DuckDuckGoAPI(object): def __init__(self, request_api=None): if request_api is None: self._requests_api = RequestsAPI() else: self._requests_api = request_api # Provide a summary of a single article def ask_question(self, url, question, num_responses=1): # http://api.duckduckgo.com/?q=DuckDuckGo&format=json payload = {'q': question, 'format': 'json'} response = self._requests_api.get(url, params=payload) if response is None: raise Exception("No response from DuckDuckGo service") if response.status_code != 200: raise Exception("Error response from DuckDuckGo service [%d]" % response.status_code) json_data = json.loads(response.text) if 'RelatedTopics' not in json_data: raise Exception( "Invalid response from DuckDuckGo service, 'RelatedTopcis' missing from payload" ) topics = json_data['RelatedTopics'] if len(topics) == 0: raise Exception( "Invalid response from DuckDuckGo service, no topics in payload" ) if len(topics) < num_responses: num_responses = len(topics) responses = [] for i in range(num_responses): if 'Text' in topics[i]: sentences = topics[i]['Text'].split(".") responses.append(sentences[0]) return ". ".join(responses)
class DuckDuckGoAPI(object): def __init__(self, request_api=None): if request_api is None: self._requests_api = RequestsAPI() else: self._requests_api = request_api # Provide a summary of a single article def ask_question(self, url, question, num_responses=1): # http://api.duckduckgo.com/?q=DuckDuckGo&format=json payload = {'q': question, 'format': 'json'} response = self._requests_api.get(url, params=payload) if response is None: raise Exception("No response from DuckDuckGo service") if response.status_code != 200: raise Exception("Error response from DuckDuckGo service [%d]"%response.status_code) json_data = json.loads(response.text) if 'RelatedTopics' not in json_data: raise Exception("Invalid response from DuckDuckGo service, 'RelatedTopcis' missing from payload") topics = json_data['RelatedTopics'] if len(topics) == 0: raise Exception("Invalid response from DuckDuckGo service, no topics in payload") if len(topics) < num_responses: num_responses = len(topics) responses = [] for i in range(num_responses): if 'Text' in topics[i]: sentences = topics[i]['Text'].split(".") responses.append(sentences[0]) return ". ".join(responses)
class PandoraAPI(object): def __init__(self, request_api=None): if request_api is None: self._requests_api = RequestsAPI() else: self._requests_api = request_api def ask_question(self, url, question, botid): payload = {'botid': botid, 'input': question} response = self._requests_api.get(url, params=payload) if response is None: raise Exception("No response from pandora service") tree = ElementTree.fromstring(response.content) that = tree.find("that") if that is None: raise Exception("Invalid response from pandora service, no <that> element in xml") return that.text
class PandoraAPI(object): def __init__(self, request_api=None): if request_api is None: self._requests_api = RequestsAPI() else: self._requests_api = request_api def ask_question(self, url, question, botid): payload = {'botid': botid, 'input': question} response = self._requests_api.get(url, params=payload) if response is None: raise Exception("No response from pandora service") tree = ElementTree.fromstring(response.content) that = tree.find("that") if that is None: raise Exception( "Invalid response from pandora service, no <that> element in xml" ) return that.text
def __init__(self, request_api=None): if request_api is None: self._requests_api = RequestsAPI() else: self._requests_api = request_api
def test_requestsapi(self): api = RequestsAPI() with self.assertRaises(Exception): api.get("http://invalid.url.com", None)