-
-
Save Kevin-De-Koninck/8cbf770d16cf195ef4587de3348029b0 to your computer and use it in GitHub Desktop.
#!/usr/bin/python | |
# This script wil get the real-time travel time between your home and your work (in both directions at the current time). | |
# | |
# You could use this script to map the real time travel times of the past day (cron job, every minute) | |
# To plot grpahs you could use: http://plot.ly | |
# | |
# | |
# If you want to plat the estimated driving times for each minute of a given day in the future, check out my gist: | |
# Get estimated travel times using Python and Google's distance matrix | |
import simplejson | |
import urllib | |
from datetime import datetime | |
# To get cooordinates -> Google maps -> right-click -> What's here -> coordinates see bottom of card | |
home_coord = "51.238525,4.816091" # Home | |
work_coord = "51.190670,4.399222" # Work | |
# API keys (Google) | |
# request them here: https://developers.google.com/maps/documentation/distance-matrix/get-api-key | |
# I use 2 API's to bypass the limitations (if you run this script every minute, you'll need 2 keys) | |
API1 = "AIzaSyD8pZR17Qno65XqYI-6XlZAfRzeovzvzpP" # Non existing example key | |
API2 = "AIzaSyD8p1111Qno65XqYI-6XlZAfRvrhRUldvM" # Non existing example key | |
# Create the GET requests | |
url_home2work = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + home_coord + "&destinations=" + work_coord + "&mode=driving&traffic_model=best_guess&departure_time=now&language=en-EN&sensor=false&key=" + API1 | |
url_work2home = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + work_coord + "&destinations=" + home_coord + "&mode=driving&traffic_model=best_guess&departure_time=now&language=en-EN&sensor=false&key=" + API2 | |
# Preform the request and read in the JSON data | |
result_home2work = simplejson.load(urllib.urlopen(url_home2work)) | |
result_work2home = simplejson.load(urllib.urlopen(url_work2home)) | |
# Get the driving time in seconds | |
driving_time_seconds_home2work = result_home2work['rows'][0]['elements'][0]['duration_in_traffic']['value'] | |
driving_time_seconds_work2home = result_work2home['rows'][0]['elements'][0]['duration_in_traffic']['value'] | |
# Append results to the file | |
target = open("Results.csv", 'a') | |
target.write( datetime.now().strftime('%Y-%m-%d %H:%M') + ";" + str(driving_time_seconds_home2work) + ";" + str(driving_time_seconds_work2home) + "\n") | |
target.close() |
IndexError Traceback (most recent call last)
in
22
23 # Get the driving time in seconds
---> 24 driving_time_seconds_home2work = result_home2work['rows'][0]['elements'][0]['duration_in_traffic']['value']
25 driving_time_seconds_work2home = result_work2home['rows'][0]['elements'][0]['duration_in_traffic']['value']
26IndexError: list index out of range
How to fix that in other version program?
This is due to the list being empty i.e with the API keys being invalid , Your request was denied.
Try to procure an API key from Google Develop Project and substitute the values for API1 & API2.
I have a problem, can some help me out?
Traceback (most recent call last):
File "C:\Users***\Desktop\Python\Get_current_real-time_travel_time.py", line 35, in
driving_time_seconds_home2work = result_home2work['rows'][0]['elements'][0]['duration_in_traffic']['value']
IndexError: list index out of range
Traceback (most recent call last):
File "C:/Users/***/Desktop/Python/Get_current_real-time_travel_time.py", line 35, in
driving_time_seconds_home2work = result_home2work['rows'][0]['elements'][0]['duration_in_traffic']['value']
KeyError: 'duration_in_traffic'
I have a question,
I have coords in a CSV file, How can I read through it instead of a single coord?
Thanks
IndexError Traceback (most recent call last)
in
22
23 # Get the driving time in seconds
---> 24 driving_time_seconds_home2work = result_home2work['rows'][0]['elements'][0]['duration_in_traffic']['value']
25 driving_time_seconds_work2home = result_work2home['rows'][0]['elements'][0]['duration_in_traffic']['value']
26
IndexError: list index out of range
How to fix that in other version program?