Domo also has the requirement of reporting weather upon voice query. Details about the weather in cities and counties across the world come from web APIs (Application Programming Interfaces). The first step to extracting information from an API is to get an API key which acts as a personal identifier when you request for information from a website. This allows the website to identify you and monitor your traffic flow. For openweathermap.org, the website that I will be using, you can receive your API key after signing up for an account but other websites may have slightly different processes. Below is an image of openweathermap’s instructions for receiving an API key and my own API key.
locu_api = 'af1d8792d555bdb9c3cc06d2167780cf'
For weather APIs, a city ID is required to identify the city that you request information about. Download a list of city IDs from openweathermap (the link below) or another source. This source also gives the corresponding city name, latitude, longitude, and country code for each city.
Next you want to construct the URL through string concatenation that will give you access to the web API’s information. A sample URL can be found on the API website (also shown below).
The number after “?id=” is where the city ID goes. Look at the list of city IDs to find the one for the city you are requesting information, and substitute it in for “524901” in the sample link. Right now, we will create a variable “url” that copies the sample API call’s URL up until the id portion.
In the code below, “city” is the city that the user is requesting information about. The code searches through the entire list of cities and adds the city’s corresponding ID to “url” if found.
with open('citylist.txt') as inputfile: for line in inputfile: dictionary = ast.literal_eval(line) if(dictionary['name'] == city): url += str(dictionary['_id']) found = 'true' break
Now the url is only missing the API key. To finish the URL, add the the last piece: your unique API key which you should’ve received after signing up for openweathermap.
url += '&APPID=af1d8792d555bdb9c3cc06d2167780cf'
In order to extract the API’s information from the internet, we must use a JSON object which translates the information into a workable text file. Using the code below, the variable “data” now holds the information received from the API in multidimensional arrays.
json_obj = urllib2.urlopen(url) data = json.load(json_obj)
Now all of the weather information retrieved about the city is in ‘data’. However, the format differs from API to API and may be hard to read. Use this JSON formatter (link below) if you are having difficulties. This lets you view the multidimensional arrays in a more organized manner.
Many APIs subdivide their data into different sections through lists, so you may need to go through embedded lists before reaching the information that you actually want. From here, you can pick and choose the data you want to use. Figuring out how to access certain data fields within the multidimensional arrays can be confusing but the information is all there. Below is an example of finding the degrees at which the wind is blowing from the API.
layer1 = data['list'] layer2 = layer1 wind = layer2['wind'] wind_degrees = float(wind['deg'])