Created
April 7, 2017 02:51
-
-
Save brandomr/a33316c030e777492e64a0aeb785f22c 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
from datetime import datetime | |
import requests | |
import json | |
def find_traffic_time(dt): | |
epoch = datetime(1970,1,1) | |
delta_time = (dt - epoch).total_seconds() | |
epoch_t = str(delta_time).split('.')[0] | |
# build query | |
base = 'https://maps.googleapis.com/maps/api/distancematrix/json?' | |
origin='origins=Austin' | |
dest='destinations=Dallas' | |
time='departure_time=' | |
traffic='traffic_model=best_guess' | |
key='key=GET_YOUR_OWN_DARN_KEY' | |
query = '{0}{1}&{2}&{3}{4}&{5}&{6}'.format(base,origin,dest,time,epoch_t,traffic,key) | |
try: | |
resp = requests.get(query) | |
data = json.loads(resp.text) | |
return data['rows'][0]['elements'][0]['duration_in_traffic']['text'] | |
except: | |
return "Unable to calculate time" | |
times = {} | |
for i in range(0,24): | |
dt = datetime(2017,4,9,i) | |
dt_str = datetime.strftime(dt, '%m/%d/%Y %H:%M') | |
result = find_traffic_time(dt) | |
times[dt_str] = result | |
""" RESULTS | |
{'04/09/2017 00:00': u'2 hours 53 mins', | |
'04/09/2017 01:00': u'2 hours 53 mins', | |
'04/09/2017 02:00': u'2 hours 53 mins', | |
'04/09/2017 03:00': u'2 hours 53 mins', | |
'04/09/2017 04:00': u'2 hours 55 mins', | |
'04/09/2017 05:00': u'2 hours 56 mins', | |
'04/09/2017 06:00': u'2 hours 55 mins', | |
'04/09/2017 07:00': u'2 hours 54 mins', | |
'04/09/2017 08:00': u'2 hours 53 mins', | |
'04/09/2017 09:00': u'2 hours 52 mins', | |
'04/09/2017 10:00': u'2 hours 51 mins', | |
'04/09/2017 11:00': u'2 hours 48 mins', | |
'04/09/2017 12:00': u'2 hours 46 mins', | |
'04/09/2017 13:00': u'2 hours 45 mins', | |
'04/09/2017 14:00': u'2 hours 45 mins', | |
'04/09/2017 15:00': u'2 hours 46 mins', | |
'04/09/2017 16:00': u'2 hours 48 mins', | |
'04/09/2017 17:00': u'2 hours 48 mins', | |
'04/09/2017 18:00': u'2 hours 49 mins', | |
'04/09/2017 19:00': u'2 hours 50 mins', | |
'04/09/2017 20:00': u'2 hours 52 mins', | |
'04/09/2017 21:00': u'2 hours 50 mins', | |
'04/09/2017 22:00': u'2 hours 49 mins', | |
'04/09/2017 23:00': u'2 hours 50 mins'} | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment