Reputation: 339
I'm trying to retrieve information on route times using the TomTom API.
By providing the API url, an API key (that you can get by registering) and a source and destination latitude/longitude I want to return the travel time.
I thought that I was getting a dictionary of dictionaries from the response, and should be able to access the data using - jsonTomTomString['routes']['summary']['travelTimeInSeconds’]
but I'm getting ...
TypeError: list indices must be integers, not str
My code is as follows:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import ConfigParser
import requests
import json
import sys
import time
import datetime
from urllib import urlopen
# URL to the tomtom api
apiURL = "https://api.tomtom.com/routing/1/calculateRoute/"
# apiKey
apiKey = "get this from link in description"
#[coordinates]
sourceLat = 51.5560241
sourceLon = -0.2817075
destLat = 53.4630621
destLon = -2.2935288
tomtomURL = "%s/%s,%s:%s,%s/json?key=%s" % (apiURL,sourceLat,sourceLon,destLat,destLon,apiKey)
getData = urlopen(tomtomURL).read()
jsonTomTomString = json.loads(getData)
totalTime = jsonTomTomString['routes']['summary']['totalTimeSeconds']
print ("time to destination is: ", totalTime)
The JSON response looks like this...
{"formatVersion":"0.0.12","copyright":"Copyright 2018 TomTom International BV. All rights reserved. This navigation data is the proprietary copyright of TomTom International BV and may be used only in accordance with the terms of a fully executed license agreement entered into between TomTom International BV, or an authorised reseller and yourself. If you have not entered into such a license agreement you are not authorised to use this data in any manner and should immediately return it to TomTom International BV.","privacy":"TomTom keeps information that tells us how and when you use our services. This includes information about the device you are using and the information we receive while you use the service, such as locations, routes, destinations and search queries. TomTom is unable to identify you based on the information it collects, and will not try to. TomTom uses the information for technical diagnostics, to detect fraud and abuse, to create usage reports, and to improve its services. The information is kept only for these purposes and for a limited period of time, after which it is destroyed. TomTom applies security methods based on industry standards to protect the information against unauthorised access. TomTom will not give anyone else access to the information or use it for any other purpose, unless explicitly and lawfully ordered to do so following due legal process. You can find out more at http://tomtom.com/privacy. You can contact TomTom by going to http://tomtom.com/support.","routes":[{"summary":{"lengthInMeters":326856,"travelTimeInSeconds":13018,"trafficDelayInSeconds":818,"departureTime":"2018-01-08T17:10:31Z","arrivalTime":"2018-01-08T20:47:28Z"},"legs":[{"summary":{"lengthInMeters":326856,"travelTimeInSeconds":13018,"trafficDelayInSeconds":818,"departureTime":"2018-01-08T17:10:31Z","arrivalTime":"2018-01-08T20:47:28Z"},"points":[{"latitude":51.55598,"longitude":-0.28216},{"latitude":51.55601,"longitude":-0.28292},{"latitude":51.55602,"longitude":-0.28384},{"latitude":51.55602,"longitude":-0.28395},{"latitude":51.55602,"longitude":-0.28442},{"latitude":51.55603,"longitude":-0.28493},{"latitude":51.55603,"longitude":-0.28564},{"latitude":51.55603,"longitude":-0.28611},
Grateful for help with getting this to work.
Upvotes: 1
Views: 3301
Reputation: 422
I see a [
after "routes":
, so routes is an array.
Try jsonTomTomString['routes'][0]['summary']['totalTimeSeconds']
Upvotes: 1