diff --git a/mbta_finder.py b/mbta_finder.py index 3ce9df8..a7fe964 100755 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -25,7 +25,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 + f=urllib2.urlopen(url) + response_text = f.read() + response_data = json.loads(response_text) + return response_data def get_lat_long(place_name): @@ -36,10 +39,21 @@ def get_lat_long(place_name): See https://developers.google.com/maps/documentation/geocoding/ for Google Maps Geocode API URL formatting requirements. """ - pass + formatted = GMAPS_BASE_URL + '?address=' + place_list = place_name.split() + for word in place_list: + formatted+=(word+'+') + formatted=formatted.rstrip('+') + formatted+='/' + response_data = get_json(formatted) + lat = response_data["results"][0]["geometry"]['location']['lat'] + lng = response_data["results"][0]["geometry"]['location']['lng'] + return (lat,lng) + -def get_nearest_station(latitude, longitude): + +def get_nearest_station(location): """ Given latitude and longitude strings, return a (station_name, distance) tuple for the nearest MBTA station to the given coordinates. @@ -47,7 +61,13 @@ def get_nearest_station(latitude, longitude): See http://realtime.mbta.com/Portal/Home/Documents for URL formatting requirements for the 'stopsbylocation' API. """ - pass + latitude=location[0] + longitude=location[1] + formatted=MBTA_BASE_URL+'?api_key='+MBTA_DEMO_API_KEY+'&lat='+str(latitude)+'&lon='+str(longitude)+'&format=json/' + response_data = get_json(formatted) + station_name = response_data['stop'][0]['stop_name'] + distance = response_data['stop'][0]['distance'] + return (station_name, distance) def find_stop_near(place_name): @@ -55,5 +75,11 @@ 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 - + location = get_lat_long(place_name) + print get_nearest_station(location)[0], + print 'is', + print get_nearest_station(location)[1], + print 'miles away from', + print place_name + +find_stop_near('First UU Church of Boston')