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
54 changes: 41 additions & 13 deletions mbta_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@
import json


# 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"


# 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
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,8 +32,21 @@ def get_lat_long(place_name):
See https://developers.google.com/maps/documentation/geocoding/
for Google Maps Geocode API URL formatting requirements.
"""
pass

placenamewords = place_name.split()
baseurl = "https://maps.googleapis.com/maps/api/geocode/json?address="
first = True
#compose with %20 delimiter
for word in placenamewords:
if first:
first = False
baseurl = baseurl + word
else:
baseurl = baseurl + "%20" + word
json = get_json(baseurl)
#step thru jason to find lat and long
lat = json["results"][0]["geometry"]["location"]["lat"]
lng = json["results"][0]["geometry"]["location"]["lng"]
return [str(lat),str(lng)]

def get_nearest_station(latitude, longitude):
"""
Expand All @@ -47,13 +56,32 @@ def get_nearest_station(latitude, longitude):
See http://realtime.mbta.com/Portal/Home/Documents for URL
formatting requirements for the 'stopsbylocation' API.
"""
pass

# the mbta api url (with key) in 3 base strings seperated by lat and lon
mbta_baseurl = "http://realtime.mbta.com/developer/api/v2/stopsbylocation?api_key=wX9NwuHnZU2ToO7GmGR9uw&lat="
mbta_baseurl2 = "&lon="
mbta_baseurl3 = "&format=json"
mbtajson = get_json(mbta_baseurl+latitude+mbta_baseurl2+longitude+mbta_baseurl3)
closeststopname = mbtajson
#try to index to nearest station, let user know if none exist
try:
distance = closeststopname['stop'][0]["distance"]
name = closeststopname['stop'][0]["stop_name"]
except:
print "No nearby stations."
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
lat_long = get_lat_long(place_name)
nearest_station = get_nearest_station(lat_long[0],lat_long[1])
print "Nearest station is : " + nearest_station[0] + "\n"
print "Distance:" + nearest_station[1] + " miles"



find_stop_near("Massachusetts Institute of Technology")