Created
June 16, 2017 19:20
-
-
Save chase9/a186ce04a2fac2aa49a4ba93256aa731 to your computer and use it in GitHub Desktop.
Simple Python script to find traffic times for the next 24 hours
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 googlemaps | |
from datetime import datetime | |
import time | |
start_time = time.time() | |
# You're going to need your own API key for Google Maps, specificly a distance matrix key. | |
# See https://github.com/googlemaps/google-maps-services-python for details on obtaining a key. | |
gmaps = googlemaps.Client(key='') | |
# Ask for / open the results file to append to | |
file_loc = raw_input('Enter path to results file: ') | |
results = open(file_loc, 'a') | |
# Get Start and End location from user | |
start = raw_input('Enter starting address: ') | |
end = raw_input('Enter ending address: ') | |
results.write("Starting Address: " + start) | |
results.write("Ending Address: " + end) | |
# Find the current time since epoch (Midnight 1/1/1970) in seconds to send to google | |
time_since_epoch = int(time.time()) | |
# Until the user presses ctrl-c... | |
try: | |
# Get the times for a full 24 hours | |
for i in range(0, 288): | |
directions_result = gmaps.distance_matrix(start, end, mode='driving', units='imperial', | |
departure_time=time_since_epoch, traffic_model="pessimistic") | |
# Append current time & duration of trip | |
current_time = str(datetime.fromtimestamp(time_since_epoch)) | |
# Write the time and trip duration to file and print to console | |
to_write = ( | |
current_time + ' ' + directions_result['rows'][0]['elements'][0]['duration_in_traffic']['text'] + '\n') | |
print(to_write) | |
results.write(to_write) | |
time_since_epoch += 300 | |
except KeyboardInterrupt: | |
results.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment