From 707a77302399b1316f62c4ea0320350efa94311c Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 13 Nov 2016 22:48:59 -0500 Subject: [PATCH] Turning in my ToolBox-Geocoding --- mbta_finder.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) mode change 100755 => 100644 mbta_finder.py diff --git a/mbta_finder.py b/mbta_finder.py old mode 100755 new mode 100644 index 3ce9df8..5284f06 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -10,6 +10,7 @@ import urllib # urlencode function import urllib2 # urlopen function (better than urllib version) import json +from pprint import pprint # Useful URLs (you need to add the appropriate parameters for your requests) @@ -19,13 +20,15 @@ # A little bit of scaffolding if you want to use it - def get_json(url): """ Given a properly formatted URL for a JSON web API request, return a Python JSON object containing the response to that request. """ - pass + f = urllib2.urlopen(url) + response_text = f.read() + response_data = json.loads(response_text) + return response_data def get_lat_long(place_name): @@ -36,7 +39,12 @@ def get_lat_long(place_name): See https://developers.google.com/maps/documentation/geocoding/ for Google Maps Geocode API URL formatting requirements. """ - pass + address = place_name.replace(' ', '+') + url = GMAPS_BASE_URL + '?address=' + address + response_data = get_json(url) + lat = str(response_data['results'][0]['geometry']['location']['lat']) + lng = str(response_data['results'][0]['geometry']['location']['lng']) + return lat, lng def get_nearest_station(latitude, longitude): @@ -47,13 +55,21 @@ def get_nearest_station(latitude, longitude): See http://realtime.mbta.com/Portal/Home/Documents for URL formatting requirements for the 'stopsbylocation' API. """ - pass - + url = MBTA_BASE_URL + '?api_key=' + MBTA_DEMO_API_KEY + '&lat=' + latitude + '&lon=' + longitude + '&format=json' + response_data = get_json(url) + print url + stop_name = response_data['stop'][0]['stop_name'] + distance = response_data['stop'][0]['distance'] + return stop_name, distance def find_stop_near(place_name): """ - Given a place name or address, print the nearest MBTA stop and the + Given a place name or address, print the nearest MBTA stop and the distance from the given place to that stop. """ - pass + lat, lng = get_lat_long(place_name) + name, distance = get_nearest_station(lat, lng) + print name + ' is ' + distance + ' miles from ' + place_name + +find_stop_near('Fenway Park Boston')