Hello! I’ve been trying to learn how to use the traffic APIs. To do so, I’ve been trying to get the examplary code to work, as it is already pretty close to what I’m trying to do. However, the code doesn’t work as is. (Visualizing TomTom Traffic Index Data with Data Science Tools | TomTom Developer Blog)
Here’s what I managed to make of it:
import pandas as pd
import datetime
import urlparse
import requests
API_Key = '****'
base_url = 'https://api.tomtom.com'
date = datetime.datetime(2021, 5, 1)
departure_time_start_2021 = datetime.datetime(date.year, date.month , date.day, 6, 0, 0)
day_range = range(0,7)
hour_range = range (0,12)
for i in day_range:
for j in hour_range:
# Update the month
departure_time_2021 = departure_time_start_2021.replace(day=departure_time_start_2021.day + i, hour=departure_time_start_2021.hour +j)
# Format datetime string
departure_time_2021 = departure_time_2021.strftime('%Y-%m-%dT%H:%M:%S')
# Create request URL
request_params_2021 = (
urlparse.quote(start) + ":" + urlparse.quote(end)
+ "/json?departAt=" + urlparse.quote(departure_time_2021))
request_url_2021 = base_url + request_params_2021 + "&key=" + API_Key
# Get data
response2021 = requests.get(request_url_2021)
# Convert to JSON
json_result_2021 = response2021.json()
# Get summary
route_summary_2021 = json_result_2021['routes'][0]['summary']
# Convert to data frame and append
if((i == 0) and (j==0)):
df_2021_daily = pd.json_normalize(route_summary_2021)
else:
df_2021_daily = df_2021_daily.append(pd.json_normalize(route_summary_2021), ignore_index=True)
print(f"Retrieving data: {i+1} / {len(day_range)}")
But I don’t know what to make out of the following line:
request_params_2021 = (urlparse.quote(start) + ":" + urlparse.quote(end)+ "/json?departAt=" + urlparse.quote(departure_time_2021))
as the variables “start” and “end” aren’t defined. Does anybody know what could be meant by that?