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
34 changes: 31 additions & 3 deletions mbta_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
import urllib # urlencode function
import urllib2 # urlopen function (better than urllib version)
import json
from pprint import pprint
# url = "https://maps.googleapis.com/maps/api/geocode/json?address=Fenway%20Park"
# f = urllib2.urlopen(url)
# response_text = f.read()
# response_data = json.loads(response_text)
# pprint(response_data)
# print response_data["results"][0]["formatted_address"]


# 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"
GMAPS_API_KEY = 'AIzaSyDvLrXdD3pmGbvsc4MJa8DuUmv4Cd2vc_o'
MBTA_DEMO_API_KEY = "wX9NwuHnZU2ToO7GmGR9uw"


Expand All @@ -26,7 +34,10 @@ def get_json(url):
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 @@ -37,7 +48,15 @@ def get_lat_long(place_name):
for Google Maps Geocode API URL formatting requirements.
"""
pass

# poi = place_name.replace(' ','%20')
address = place_name.replace(' ','+')
url = GMAPS_BASE_URL + '?address=' + address + '&key=' + GMAPS_API_KEY
# print url
# break
response_data = get_json(url)
latitude = str(response_data["results"][0]["geometry"]["location"]['lat'])
longitude = str(response_data["results"][0]["geometry"]["location"]['lng'])
return (latitude,longitude)

def get_nearest_station(latitude, longitude):
"""
Expand All @@ -48,12 +67,21 @@ def get_nearest_station(latitude, longitude):
formatting requirements for the 'stopsbylocation' API.
"""
pass
url = MBTA_BASE_URL + '&key=' + MBTA_DEMO_API_KEY + '&lat=' + latitude + '&lon=' + longitude + '&format=json/'
# nearby = stopsbylocation(latitude,longitude)
nearby = get_json(url)
station_name = str(nearby['stop'][0]['stop_name'])
distance = str(nearby['stop'][0]['distance'])
return (station_name,distance)


def find_stop_near(place_name):
"""
Given a place name or address, print the nearest MBTA stop and the
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)
return get_nearest_station(location[0],location[1])

find_stop_near('Faneuil Hall')