Created
November 15, 2016 19:38
-
-
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.
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 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: