From 140aa2f85151efa4982cad17bf5eef237c3354d2 Mon Sep 17 00:00:00 2001 From: KatieButler Date: Mon, 14 Mar 2016 05:41:53 -0400 Subject: [PATCH 1/2] turning in? need to double check API keys --- mbta_finder.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/mbta_finder.py b/mbta_finder.py index 3ce9df8..d4c1ed6 100755 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -10,11 +10,19 @@ import urllib # urlencode function import urllib2 # urlopen function (better than urllib version) import json +from pprint import pprint +# url = "https://maps.googleapis.com/maps/api/geocode/json?address=Fenway%20Park" +# f = urllib2.urlopen(url) +# response_text = f.read() +# response_data = json.loads(response_text) +# pprint(response_data) +# print response_data["results"][0]["formatted_address"] # Useful URLs (you need to add the appropriate parameters for your requests) GMAPS_BASE_URL = "https://maps.googleapis.com/maps/api/geocode/json" MBTA_BASE_URL = "http://realtime.mbta.com/developer/api/v2/stopsbylocation" +GMAPS_API_KEY = 'AIzaSyDvLrXdD3pmGbvsc4MJa8DuUmv4Cd2vc_o' MBTA_DEMO_API_KEY = "wX9NwuHnZU2ToO7GmGR9uw" @@ -26,7 +34,10 @@ def get_json(url): 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): """ @@ -37,7 +48,15 @@ def get_lat_long(place_name): for Google Maps Geocode API URL formatting requirements. """ pass - + # poi = place_name.replace(' ','%20') + address = place_name.replace(' ','+') + url = GMAPS_BASE_URL + '?address=' + address + '&key=' + GMAPS_API_KEY + # print url + # break + response_data = get_json(url) + latitude = response_data["results"][0]["geometry"]["location"]['lat'] + longitude = response_data["results"][0]["geometry"]["location"]['lng'] + return (latitude,longitude) def get_nearest_station(latitude, longitude): """ @@ -48,12 +67,21 @@ def get_nearest_station(latitude, longitude): formatting requirements for the 'stopsbylocation' API. """ pass + url = MBTA_BASE_URL + '&key=' + MBTA_DEMO_API_KEY + 'lat=' + str(latitude) + '&lon=' + str(longitude) + '&format=json/' + # nearby = stopsbylocation(latitude,longitude) + nearby = get_json(url) + station_name = nearby['stop'][0]['stop_name'] + distance = nearby['stop'][0]['distance'] + return (station_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 + location = get_lat_long(place_name) + return get_nearest_station(location[0],location[1]) +find_stop_near('Faneuil Hall') From e9872b2fdd8fac319442db5d68251f3fcb2d379b Mon Sep 17 00:00:00 2001 From: KatieButler Date: Mon, 14 Mar 2016 05:58:08 -0400 Subject: [PATCH 2/2] turning in for real I guess --- mbta_finder.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mbta_finder.py b/mbta_finder.py index d4c1ed6..6f8e909 100755 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -54,8 +54,8 @@ def get_lat_long(place_name): # print url # break response_data = get_json(url) - latitude = response_data["results"][0]["geometry"]["location"]['lat'] - longitude = response_data["results"][0]["geometry"]["location"]['lng'] + latitude = str(response_data["results"][0]["geometry"]["location"]['lat']) + longitude = str(response_data["results"][0]["geometry"]["location"]['lng']) return (latitude,longitude) def get_nearest_station(latitude, longitude): @@ -67,11 +67,11 @@ def get_nearest_station(latitude, longitude): formatting requirements for the 'stopsbylocation' API. """ pass - url = MBTA_BASE_URL + '&key=' + MBTA_DEMO_API_KEY + 'lat=' + str(latitude) + '&lon=' + str(longitude) + '&format=json/' + url = MBTA_BASE_URL + '&key=' + MBTA_DEMO_API_KEY + '&lat=' + latitude + '&lon=' + longitude + '&format=json/' # nearby = stopsbylocation(latitude,longitude) nearby = get_json(url) - station_name = nearby['stop'][0]['stop_name'] - distance = nearby['stop'][0]['distance'] + station_name = str(nearby['stop'][0]['stop_name']) + distance = str(nearby['stop'][0]['distance']) return (station_name,distance)