API stands for Application Programming Interface and are essentially a way to access a piece of information from a website or service. REST stands for representational state transfer which is a way of describing how APIs should look and work. In most cases, especially for web APIs, a REST interface is generally a URL that you can click to get some information from the website.
We'll be using Python to make use of some web APIs! Lets get started!
In order to get started, we need Python version 2.7 or higher and access to the internet. We also need some basic knowledge of python: things like defining a function and how lists and strings work
We will be using a website called firebase to get data for some of our APIs. This website has a collection of some public datasets: like weather and bus routes. We will also be using the google maps images api
JSON is a format for representing data. It is a very common thing that a lot of APIs use. JSON essentially looks like this. Everything is inside {}
and you have multiple entires which contan keys and values
{
"key_name" : "value",
"numeric_key" : 42,
"list_items" : [1, 2, "three", "four"],
"dictionary" : { "another_key" : "another value" }
}
We are going to use these simple tricks
- If it is a number, its a number!
- If it is inside double quotes, it is a string
- If it has square brackets, it is a list!
- If it has curly braces, it is a dictionary
import json
import urllib2
# This is the API URL
api = "https://publicdata-weather.firebaseio.com/sanfrancisco/currently.json"
get = urllib2.urlopen(api).read()
print get
This gives us something like:
import json
import urllib2
api = "https://publicdata-weather.firebaseio.com/sanfrancisco/currently.json"
get = urllib2.urlopen(api).read()
data = json.loads(get)
print data
This gives us something like:
import json
import urllib2
def get_data(api):
get = urllib2.urlopen(api).read()
data = json.loads(get)
return data
api = "https://publicdata-weather.firebaseio.com/sanfrancisco/currently.json"
data = get_data(api)
print data
Reading is simple. We just need to define the key name and we get back it's value!
print data['temperature'] #=> 52.46
prnt data['summary'] #=> "Partly Cloudy"
- Lets print a few more properties
- Print the temperature in Celsius
- Use the formula:
(temperature -32) * 5.0/9.0
- Use the formula:
We are going to make things a little bit more interesting. We are going to find where our favorite muni bus is in the city. To do this, we are going to need just 1 thing:
- The
id
of a bus
Let's use firebase's MUNI API again
api = "https://publicdata-transit.firebaseio.com/sf-muni/data.json"
data = get_data(api)
api = "https://publicdata-transit.firebaseio.com/sf-muni/data.json"
data = get_data(api)
def find_bus(bus_name, data):
for bus_id, bus_info in data.iteritems():
if bus_info['routeTag']:
return bus_info
# for routes that are just numbers the input parameter
# should be int instead of string
number = 'N'
location = find_bus(number, data)
latitude = location['lat']
longitude = location['lon']
print latitude
print longitude
def get_map(latitude, longitude):
size = '600x450'
zoom = '16'
url = "http://maps.google.com/maps/api/staticmap?size=%s&maptype=roadmap&markers=size:mid|color:red|%s,%s&sensor=false&zoom=%s"
return url % (size, latitude, longitude, zoom)
print get_map(latitude, longitude)
- Add more than 1 location on the map