From 9108f1aa8bee35ea3946ba33e511d847fa2d0920 Mon Sep 17 00:00:00 2001 From: arpanrau Date: Sun, 23 Oct 2016 13:19:02 -0400 Subject: [PATCH] Completed toolbox --- mbta_finder.py | 54 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/mbta_finder.py b/mbta_finder.py index 3ce9df8..99ec2b6 100755 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -12,20 +12,16 @@ import json -# 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" -MBTA_DEMO_API_KEY = "wX9NwuHnZU2ToO7GmGR9uw" - - -# 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,8 +32,21 @@ def get_lat_long(place_name): See https://developers.google.com/maps/documentation/geocoding/ for Google Maps Geocode API URL formatting requirements. """ - pass - + placenamewords = place_name.split() + baseurl = "https://maps.googleapis.com/maps/api/geocode/json?address=" + first = True + #compose with %20 delimiter + for word in placenamewords: + if first: + first = False + baseurl = baseurl + word + else: + baseurl = baseurl + "%20" + word + json = get_json(baseurl) + #step thru jason to find lat and long + lat = json["results"][0]["geometry"]["location"]["lat"] + lng = json["results"][0]["geometry"]["location"]["lng"] + return [str(lat),str(lng)] def get_nearest_station(latitude, longitude): """ @@ -47,13 +56,32 @@ def get_nearest_station(latitude, longitude): See http://realtime.mbta.com/Portal/Home/Documents for URL formatting requirements for the 'stopsbylocation' API. """ - pass - + # the mbta api url (with key) in 3 base strings seperated by lat and lon + mbta_baseurl = "http://realtime.mbta.com/developer/api/v2/stopsbylocation?api_key=wX9NwuHnZU2ToO7GmGR9uw&lat=" + mbta_baseurl2 = "&lon=" + mbta_baseurl3 = "&format=json" + mbtajson = get_json(mbta_baseurl+latitude+mbta_baseurl2+longitude+mbta_baseurl3) + closeststopname = mbtajson + #try to index to nearest station, let user know if none exist + try: + distance = closeststopname['stop'][0]["distance"] + name = closeststopname['stop'][0]["stop_name"] + except: + print "No nearby stations." + return (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_long = get_lat_long(place_name) + nearest_station = get_nearest_station(lat_long[0],lat_long[1]) + print "Nearest station is : " + nearest_station[0] + "\n" + print "Distance:" + nearest_station[1] + " miles" + + +find_stop_near("Massachusetts Institute of Technology")