|
3 | 3 |
|
4 | 4 | ### Introduction to REST API parsing with python!
|
5 | 5 |
|
| 6 | +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. |
| 7 | + |
| 8 | +We'll be using Python to make use of some web APIs! Lets get started! |
6 | 9 |
|
7 |
| -## Introduction |
8 | 10 |
|
9 | 11 | ## Prerequisites
|
10 | 12 |
|
11 |
| -## What is REST |
| 13 | +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 |
| 14 | + |
| 15 | +## Introduction |
| 16 | + |
| 17 | +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 |
| 18 | + |
| 19 | +## JSON |
| 20 | + |
| 21 | +### Understanding JSON |
| 22 | + |
| 23 | +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 |
| 24 | + |
| 25 | +```json |
| 26 | +{ |
| 27 | + "key_name" : "value", |
| 28 | + "numeric_key" : 42, |
| 29 | + "list_items" : [1, 2, "three", "four"], |
| 30 | + "dictionary" : { "another_key" : "another value" } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Reading JSON |
| 35 | + |
| 36 | +We are going to use these simple tricks |
| 37 | + |
| 38 | +- If it is a **number**, its a **number**! |
| 39 | +- If it is inside **double quotes**, it is a **string** |
| 40 | +- If it has **square brackets**, it is a **list**! |
| 41 | +- If it has **curly braces**, it is a **dictionary** |
12 | 42 |
|
13 | 43 | ## Reading the Weather
|
14 | 44 |
|
| 45 | +### Opening the website in python |
| 46 | + |
| 47 | +```py |
| 48 | +import json |
| 49 | +import urllib2 |
| 50 | + |
| 51 | +# This is the API URL |
| 52 | +api = "https://publicdata-weather.firebaseio.com/sanfrancisco/currently.json" |
| 53 | + |
| 54 | +get = urllib2.urlopen(api).read() |
| 55 | + |
| 56 | +print get |
| 57 | +``` |
| 58 | + |
| 59 | +**This gives us something like:** |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +### Parsing JSON in python |
| 64 | + |
| 65 | +```py |
| 66 | +import json |
| 67 | +import urllib2 |
| 68 | + |
| 69 | +api = "https://publicdata-weather.firebaseio.com/sanfrancisco/currently.json" |
| 70 | +get = urllib2.urlopen(api).read() |
| 71 | +data = json.loads(get) |
| 72 | + |
| 73 | +print data |
| 74 | +``` |
| 75 | + |
| 76 | +**This gives us something like:** |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +### Writing a function to return our data |
| 84 | + |
| 85 | +```py |
| 86 | +import json |
| 87 | +import urllib2 |
| 88 | + |
| 89 | +def get_data(api): |
| 90 | + get = urllib2.urlopen(api).read() |
| 91 | + data = json.loads(get) |
| 92 | + |
| 93 | + return data |
| 94 | + |
| 95 | +api = "https://publicdata-weather.firebaseio.com/sanfrancisco/currently.json" |
| 96 | +data = get_data(api) |
| 97 | + |
| 98 | +print data |
| 99 | +``` |
| 100 | + |
| 101 | +### Reading JSON in Python |
| 102 | + |
| 103 | +Reading is simple. We just need to define the key name and we get back it's value! |
| 104 | + |
| 105 | +```py |
| 106 | +print data['temperature'] #=> 52.46 |
| 107 | +prnt data['summary'] #=> "Partly Cloudy" |
| 108 | +``` |
| 109 | + |
| 110 | +#### Tasks |
| 111 | + |
| 112 | +- **Lets print a few more properties** |
| 113 | +- **Print the temperature in Celsius** |
| 114 | + - Use the formula: `(temperature -32) * 5.0/9.0` |
| 115 | + |
15 | 116 | ## Checking MUNI Status
|
16 | 117 |
|
| 118 | +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: |
| 119 | + |
| 120 | +- The `id` of a bus |
| 121 | + |
| 122 | + |
| 123 | +Let's use firebase's MUNI API again |
| 124 | + |
| 125 | + |
| 126 | +```py |
| 127 | +api = "https://publicdata-transit.firebaseio.com/sf-muni/data.json" |
| 128 | +data = get_data(api) |
| 129 | +``` |
| 130 | + |
| 131 | +#### DONT PRINT THIS `data` VARIABLE |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +## Let's find our favorite bus |
| 136 | + |
| 137 | +```py |
| 138 | +api = "https://publicdata-transit.firebaseio.com/sf-muni/data.json" |
| 139 | +data = get_data(api) |
| 140 | + |
| 141 | +def find_bus(bus_name, data): |
| 142 | + for bus_id, bus_info in data.iteritems(): |
| 143 | + if bus_info['routeTag']: |
| 144 | + return bus_info |
| 145 | + |
| 146 | +# for routes that are just numbers the input parameter |
| 147 | +# should be int instead of string |
| 148 | +number = 'N' |
| 149 | +location = find_bus(number, data) |
| 150 | + |
| 151 | +latitude = location['lat'] |
| 152 | +longitude = location['lon'] |
| 153 | + |
| 154 | +print latitude |
| 155 | +print longitude |
| 156 | +``` |
| 157 | + |
| 158 | + |
| 159 | + |
17 | 160 | ## Lets add Maps!
|
| 161 | + |
| 162 | +```py |
| 163 | +def get_map(latitude, longitude): |
| 164 | + size = '600x450' |
| 165 | + zoom = '16' |
| 166 | + url = "http://maps.google.com/maps/api/staticmap?size=%s&maptype=roadmap&markers=size:mid|color:red|%s,%s&sensor=false&zoom=%s" |
| 167 | + |
| 168 | + return url % (size, latitude, longitude, zoom) |
| 169 | + |
| 170 | +print get_map(latitude, longitude) |
| 171 | +``` |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | +## Homework |
| 176 | + |
| 177 | +- Add more than 1 location on the map |
0 commit comments