Last active
October 5, 2017 22:44
-
-
Save m4hi2/568b5c2f844edc371808ace2a4bc2452 to your computer and use it in GitHub Desktop.
Reads space separated origin & destination from the STDIN and returns driving duration.
This file contains hidden or 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 requests | |
api_key = "ADD YOUR OWN API KEY" | |
def get_duration(source, destination, key): | |
query_url = "https://maps.googleapis.com/maps/api/distancematrix/json" | |
try: | |
data = requests.get(query_url, params={ | |
"destinations": destination, | |
"origins": source, | |
"key": key, | |
"units":"imperial" | |
}).json() | |
duration = data["rows"][0]["elements"][0]["duration"]["text"] | |
return duration | |
except KeyError as e: | |
print("Key {} not available".format(e)) | |
origin = input("Origin: ").split() | |
destination = input("Destination: ").split() | |
origins = "+".join(origin) | |
destinations = "+".join(destination) | |
print(get_duration(origins, destinations, api_key)) | |
uroybd
commented
Apr 29, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment