Skip to content

Instantly share code, notes, and snippets.

@m4hi2
Last active October 5, 2017 22:44
Show Gist options
  • Save m4hi2/568b5c2f844edc371808ace2a4bc2452 to your computer and use it in GitHub Desktop.
Save m4hi2/568b5c2f844edc371808ace2a4bc2452 to your computer and use it in GitHub Desktop.
Reads space separated origin & destination from the STDIN and returns driving duration.
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
Copy link

uroybd commented Apr 29, 2017

import requests

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment