Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions mbta_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,7 +25,11 @@ 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):
Expand All @@ -36,7 +40,12 @@ def get_lat_long(place_name):
See https://developers.google.com/maps/documentation/geocoding/
for Google Maps Geocode API URL formatting requirements.
"""
pass
place_name = place_name.replace(" " , "%20")
url = GMAPS_BASE_URL + "?address=" + place_name
response_data = get_json(url)
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):
Expand All @@ -47,13 +56,27 @@ def get_nearest_station(latitude, longitude):
See http://realtime.mbta.com/Portal/Home/Documents for URL
formatting requirements for the 'stopsbylocation' API.
"""
pass
url = "http://realtime.mbta.com/developer/api/v2/stopsbylocation?api_key=wX9NwuHnZU2ToO7GmGR9uw&lat=%s&lon=%s&format=json" % (str(latitude), str(longitude))
response_data = get_json(url)
try:
station_name = str(response_data["stop"][0]["parent_station_name"])
distance = float(response_data["stop"][0]["distance"])
except IndexError:
station_name = "Out of Range"
distance = "Out of Range"


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, lng = get_lat_long(place_name)
station_name, distance = get_nearest_station(lat, lng)
return station_name, distance


print find_stop_near("1000 Olin Way Needham Massachusetts")