From 06dd9a47d91f89b4a2422d6cc715b59806e6907a Mon Sep 17 00:00:00 2001 From: arianaolson419 Date: Wed, 16 Mar 2016 17:44:09 -0400 Subject: [PATCH 1/2] submitting the toolbox --- mbta_finder.py | 56 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/mbta_finder.py b/mbta_finder.py index 3ce9df8..c745d8b 100755 --- a/mbta_finder.py +++ b/mbta_finder.py @@ -10,7 +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) GMAPS_BASE_URL = "https://maps.googleapis.com/maps/api/geocode/json" @@ -21,11 +21,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 + """ + Given a properly formatted URL for a JSON web API request, return + a Python JSON object containing the response to that request. + url: url of the API request + """ + f = urllib2.urlopen(url) + response_text = f.read() + response_data = json.loads(response_text) + return response_data def get_lat_long(place_name): @@ -35,25 +39,43 @@ def get_lat_long(place_name): See https://developers.google.com/maps/documentation/geocoding/ for Google Maps Geocode API URL formatting requirements. + place_name: address of place to look up """ - pass - + place_name_f = place_name.replace(' ', '+') + place_url = GMAPS_BASE_URL + '?address={kwarg}&components=administrative_area:MA|country:US'.format(kwarg=place_name_f) + latitude = get_json(place_url)["results"][0]["geometry"]["location"]["lat"] + longitude = get_json(place_url)["results"][0]["geometry"]["location"]["lng"] + return (latitude, longitude) def get_nearest_station(latitude, longitude): - """ - Given latitude and longitude strings, return a (station_name, distance) - tuple for the nearest MBTA station to the given coordinates. - - See http://realtime.mbta.com/Portal/Home/Documents for URL - formatting requirements for the 'stopsbylocation' API. - """ - pass + """ + Given latitude and longitude strings, return a (station_name, distance) + tuple for the nearest MBTA station to the given coordinates. + + See http://realtime.mbta.com/Portal/Home/Documents for URL + formatting requirements for the 'stopsbylocation' API. + latitude, longitude: geographic coordinates of the place to find nearest station to + """ + key = MBTA_DEMO_API_KEY + station_url = MBTA_BASE_URL + '?api_key={}&lat={}&lon={}&format=json'.format(key, str(latitude), str(longitude)) + try: + station_name = get_json(station_url)["stop"][0]["stop_name"] + distance = get_json(station_url)["stop"][0]["distance"] + distance_f = str(round(float(distance), 2)) + return (station_name, distance_f) + except: + return 'No stations close by' 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. + place_name: address of place to lookup """ - pass + lat = get_lat_long(place_name)[0] + lon = get_lat_long(place_name)[1] + return get_nearest_station(lat, lon) + + From f0e672329acffa09e029b7bb6718c00efa7229c7 Mon Sep 17 00:00:00 2001 From: arianaolson419 Date: Wed, 16 Mar 2016 17:44:50 -0400 Subject: [PATCH 2/2] testing if I could import the mbta finder --- test.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..9ed3c08 --- /dev/null +++ b/test.py @@ -0,0 +1,3 @@ +import mbta_finder + +print mbta_finder.find_stop_near('Fenway Park') \ No newline at end of file