Skip to content

Finished toolbox #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
31 changes: 27 additions & 4 deletions mbta_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +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)
Expand All @@ -19,13 +20,20 @@


# A little bit of scaffolding if you want to use it
def get_map_url(search):
url = GMAPS_BASE_URL + '?' + urllib.urlencode([('address', search)])
return url


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 +44,12 @@ def get_lat_long(place_name):
See https://developers.google.com/maps/documentation/geocoding/
for Google Maps Geocode API URL formatting requirements.
"""
pass
url = get_map_url(place_name)
data = get_json(url)
location = data["results"][0]["geometry"]["location"]
lat = location["lat"]
lng = location["lng"]
return (lat, lng)


def get_nearest_station(latitude, longitude):
Expand All @@ -47,13 +60,23 @@ def get_nearest_station(latitude, longitude):
See http://realtime.mbta.com/Portal/Home/Documents for URL
formatting requirements for the 'stopsbylocation' API.
"""
pass
params = [('api_key', MBTA_DEMO_API_KEY), ('lat', latitude), ('lon', longitude), ('format', 'json')]
url = MBTA_BASE_URL + '?' + urllib.urlencode(params)
response = get_json(url)
stop = response['stop'][0]
name = stop['stop_name']
distance = stop['distance']
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
try:
return get_nearest_station(*get_lat_long(place_name))
except:
return 'Not close enough to MBTA'

print find_stop_near("Olin College of Engineering, Needham, MA")