Pyhton - API - Read

Hey Team,

I am using python to read the API records.

import requests
import json

url = ‘https://api.transport.nsw.gov.au/v1/gtfs/vehiclepos/buses?debug=true
headers = {‘Authorization’: ‘apikey ************************************************’}
resp = requests.get(url,headers=headers)
print(resp.content.decode(“utf-8”))

To get a resonse like

header {
gtfs_realtime_version: “1.0”
incrementality: FULL_DATASET
timestamp: 1519083040
}
entity {
id: “33553_13279962_2436_610X_1”
vehicle {
trip {
trip_id: “606068”
start_time: “09:15:00”
start_date: “20180220”
schedule_relationship: SCHEDULED
route_id: “2436_610X”
}
position {
latitude: -33.703274
longitude: 150.9475
bearing: 247.0
speed: 0.0
}
timestamp: 1519083025
congestion_level: RUNNING_SMOOTHLY
vehicle {
id: “33553_13279962_2436_610X_1”
[transit_realtime.tfnsw_vehicle_descriptor] {
air_conditioned: true
wheelchair_accessible: 0
vehicle_model: “Volvo~B10MARTIC~Custom Coaches~CB50”
special_vehicle_attributes: 0
}
}
occupancy_status: MANY_SEATS_AVAILABLE
}
}

Now can anyone suggest a way for me to save this in Dataframe without going through the exercise of making a variable for each field indivisually.

Hi @Mithun, hopefully one of our awesome developers out there can help you out. We provide very limited coding support since we like to focus on the data :slightly_smiling_face:

Thanks,
Alex

Hey @Mithun,

You’re accessing the API using the debug flag which provides a serialisation of the data… but it’s intended as a one-way serialization method and should really be used for debugging only (i.e. not for consuming the data).
Note that the output of the debugging feed may also be truncated.

The supported way to use the data is provided in the GTFS-realtime standard which uses Protocol Buffers. If you drop the debug=true, the data is provided as a protocol buffer.

There are many Python libraries out that can decode protocol buffers. I haven’t used any of them… so can’t really provide suggestions. @timbennett had also put this handy guide together, which may help point you in the right direction.

Hope that helps!

Ken

Hey, just to note that I haven’t updated that notebook since it was first published, and significant sections (eg authorisation) are now out of date. You may have some success with the “Parsing the response” section related to the protobuf library, but it wouldn’t surprise me if the field hierarchy has been modified.

1 Like

Thank you so much guys. I was a bit occupied last couple of hence was not able to reply.

thank you so much for the information

This may be a stupid question, but is the tutorial by timbennett using Python 2 or 3?