Skip to content

Instantly share code, notes, and snippets.

@brool
Created November 15, 2016 19:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save brool/01268032460afaa83584459ce268f669 to your computer and use it in GitHub Desktop.
Stupid little command line utility to give the ETA to work/home for various times in the future.
import requests
import datetime
import time
import urllib
import sys
origin = "1234 Main Street, Anywhere CA 12345"
dest = "4567 Corporation Way, Anywhere CA 12345"
key = "your-api-key-for-distance-matrix"
time_spans = range(0, 61, 15)
if len(sys.argv) > 1 and sys.argv[1].lower() == 'work':
(origin, dest) = (dest, origin)
def unix_to_hm(u):
return datetime.datetime.fromtimestamp(u).strftime("%H:%M")
now = int(time.time())
now_dt = datetime.datetime.fromtimestamp(now)
now_hm = now_dt.strftime("%H:%M")
results = []
for offset in time_spans:
departure_time = now + offset * 60
uri = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=%s&destinations=%s&key=%s&departure_time=%s&traffic_model=best_guess" % (urllib.quote(origin), urllib.quote(dest), key, str(departure_time))
rv = requests.get(uri)
results.append([ departure_time, rv.json() ])
for row in results:
traffic = row[1]['rows'][0]['elements'][0]['duration_in_traffic']['value']
print "%-6s: arrive %-6s traffic %2.2f" % (unix_to_hm(row[0]), unix_to_hm(row[0] + traffic), int(traffic/60))
@brool
Copy link
Author

brool commented Nov 15, 2016

Example output:

11:36 : arrive 11:57  traffic 21.00
11:51 : arrive 12:13  traffic 22.00
12:06 : arrive 12:28  traffic 21.00
12:21 : arrive 12:42  traffic 21.00
12:36 : arrive 12:57  traffic 21.00

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