From 337d78568a05e1a8e26c1e3efbf47f715ea429c0 Mon Sep 17 00:00:00 2001 From: Subeen-Kim Date: Sun, 26 Nov 2017 20:25:18 -0500 Subject: [PATCH] my 4th toolbox --- mbta_finder.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/mbta_finder.py b/mbta_finder.py index f6e7b07..3f3dd3a 100755 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -15,7 +15,7 @@ GMAPS_BASE_URL = "https://maps.googleapis.com/maps/api/geocode/json" MBTA_BASE_URL = "http://realtime.mbta.com/developer/api/v2/stopsbylocation" MBTA_DEMO_API_KEY = "wX9NwuHnZU2ToO7GmGR9uw" - +""" The demo key does not work, hence I will finsh my code after I get the key""" # A little bit of scaffolding if you want to use it @@ -24,8 +24,10 @@ 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 - + JSON_object = urlopen(url) + response_text = JSON_object.read() + response_data = json.loads(str(response_text, "utf-8")) + return response_data def get_lat_long(place_name): """ @@ -35,8 +37,19 @@ def get_lat_long(place_name): See https://developers.google.com/maps/documentation/geocoding/ for Google Maps Geocode API URL formatting requirements. """ - pass + place_word = place_name.split() + JSON_BASE_URL = GMAPS_BASE_URL + "?address=" + index = 0 + for word in place_word: + if index == 0: + temp_URL = JSON_BASE_URL + word + index = 1 + else: + temp_URL = temp_URL + "%20" + word + + place_data = get_json(temp_URL) + return place_data['results'][0]['geometry']['location']['lat'],place_data['results'][0]['geometry']['location']['lng'] def get_nearest_station(latitude, longitude): """ @@ -46,12 +59,20 @@ def get_nearest_station(latitude, longitude): See http://realtime.mbta.com/Portal/Home/Documents for URL formatting requirements for the 'stopsbylocation' API. """ - pass + location_URL = MBTA_BASE_URL + "?api key=" + MBTA_DEMO_API_KEY + "&lat=" + str(latitude) + "&lon=" + str(longitude) + "&format=json" + station_data = get_json(location_URL) + station_name = station_data["stop"][0]["stop_name"] + distance = station_data["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 distance from the given place to that stop. """ - pass + lat, lon = get_lat_long(place_name) + name, dist = get_nearest_station(lat,lon) + return name, dist + +print(find_stop_near(Olin College of Engineering))