Skip to content

Instantly share code, notes, and snippets.

@amarinelli
Created August 28, 2014 02:53
Show Gist options
  • Save amarinelli/2b9dd8623848dbc55a37 to your computer and use it in GitHub Desktop.
Save amarinelli/2b9dd8623848dbc55a37 to your computer and use it in GitHub Desktop.
Attempt to calculate commuting time trends to work and back during rush hour
# Recording Daily Commute History
# West Queen West <----> North York
# Approx Route: Gardiner eastbound - D.V.P. northboutd - Eglinton Ave. E.
# Google Distance Matrix API
# https://developers.google.com/maps/documentation/distancematrix/
'''The departure time may be specified by Google Maps API for Business customers
for to specify the departure_time to receive trip duration considering current
traffic conditions. The departure_time must be set to within a few minutes of the current time.'''
'''Use of the Distance Matrix API must relate to the display of information on a Google Map;
for example, to determine origin-destination pairs that fall within a specific driving time
from one another, before requesting and displaying those destinations on a map. Use of the
service in an application that doesn't display a Google map is prohibited.'''
from datetime import datetime
import time
API_KEY = ""
def CalcCommute():
origin = ""
destination = ""
mode = "driving"
depart_time = int(time.mktime(datetime.now().timetuple()))
url = ("https://maps.googleapis.com/maps/api/distancematrix/json?"
"origins={0}&destinations={1}&mode={2}&departure_time={3}&key={4}").format(origin,
destination,
mode,
depart_time,
API_KEY)
current_hour = int(datetime.datetime.now().strftime('%H'))
week_day = datetime.today().weekday() # Returns integer, where Monday is 0 and Sunday is 6.
if 7 <= current_hour <= 10:
CalcCommute()
elif 15 <= current_hour <= 18:
CalcCommute()
else:
# outside window of interest
pass
@amarinelli
Copy link
Author

As far as I can tell, drive times is not available unless you have Google Maps API for Business ($)

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