Created
August 6, 2016 02:08
-
-
Save Gricha/4269753d78bd03fce915f290602411f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import googlemaps | |
from datetime import datetime | |
def extractDurationInTrafficFromResponse(response): | |
return response[0]['legs'][0]['duration_in_traffic'] | |
def timesBetweenPoints(client, origin, destination): | |
best_guess_response = client.directions(origin, | |
destination, | |
departure_time=datetime.now(), | |
traffic_model="best_guess") | |
pessimistic_response = client.directions(origin, | |
destination, | |
departure_time=datetime.now(), | |
traffic_model="pessimistic") | |
return {"best_guess":extractDurationInTrafficFromResponse(best_guess_response), | |
"pessimistic":extractDurationInTrafficFromResponse(pessimistic_response)} | |
def currentState(client, home, work): | |
""" | |
home/work - 2 element tuple of (latitude, longitude), or a string describing location | |
""" | |
return json.dumps({ | |
"home_to_work": timesBetweenPoints(client, origin = home, destination = work), | |
"work_to_home": timesBetweenPoints(client, destination = home, origin = work), | |
}) | |
def main(): | |
# Load up API key, home and work address, keeping it in config.json file | |
# on the same level as script file | |
with open("config.json") as f: | |
config = json.loads(f.read()) | |
f.close() | |
client = googlemaps.Client(key=config['api_key']) | |
home = config['home'] | |
work = config['work'] | |
print currentState(client, work, home) | |
if __name__=="__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment