Skip to content

turing in toolbox #14

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 1 commit 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
43 changes: 39 additions & 4 deletions mbta_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import urllib # urlencode function
import urllib2 # urlopen function (better than urllib version)
import json
from pprint import pprint
import urllib


# 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"
GMAPS_API_KEY = 'AIzaSyCrfI3z_PcorRZ-nCJl3T0NKfl4zU6eDFM'


# A little bit of scaffolding if you want to use it
Expand All @@ -25,7 +28,12 @@ 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,16 @@ def get_lat_long(place_name):
See https://developers.google.com/maps/documentation/geocoding/
for Google Maps Geocode API URL formatting requirements.
"""
pass

address = place_name.replace(" ", ",")

place_url = GMAPS_BASE_URL + '?' + 'address=' + address + GMAPS_API_KEY

response_data = get_json(place_url)
latlng = response_data["results"][0]["geometry"]["location"]

return (latlng.get(u'lat'), latlng.get(u'lng'))



def get_nearest_station(latitude, longitude):
Expand All @@ -47,13 +64,31 @@ def get_nearest_station(latitude, longitude):
See http://realtime.mbta.com/Portal/Home/Documents for URL
formatting requirements for the 'stopsbylocation' API.
"""
pass
try:
stop_nearby_url = '{}?api_key={}&lat={}&lon={}&format=json'.format(MBTA_BASE_URL, MBTA_DEMO_API_KEY, latitude, longitude)

data = get_json(stop_nearby_url)
info = data[u'stop'][0]
place_info = [info.get(u'parent_station_name'), info.get(u'distance')]
station_name = place_info[0].replace('u', '')
distance = place_info[1].replace('u', '')

name_dist = (station_name, distance)
print station_name + ' is ' + distance + ' miles away.'

except:
"There are no T-stops 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.
"""
pass
latlng_place = get_lat_long(place_name)

get_nearest_station(latlng_place[0], latlng_place[1])


find_stop_near('Boston University')