from google.transit import gtfs_realtime_pb2
from google.protobuf.json_format import MessageToDict
from google.protobuf.json_format import MessageToJson
from google.protobuf.json_format import ParseDict
import requests
import json
import pandas as pd
from collections import OrderedDict
import datetime
newdict = {
'header': {'gtfsRealtimeVersion': '2.0',
'incrementality': 'FULL_DATASET',
'timestamp': '1531806015'
},
'entity': [{
'id': 'y1897',
'vehicle': {
'trip': {
'tripId': '37696116',
'startDate': '20180717',
'scheduleRelationship': 'UNSCHEDULED',
'routeId': '47',
'directionId': 1
},
'position': {
'latitude': 42.33042907714844,
'longitude': -71.08306884765625,
'bearing': 0.0
},
'timestamp': '1531805943',
'vehicle': {'id': 'y1897', 'label': '1897'}
}
}]
}
feed3 = gtfs_realtime_pb2.FeedMessage()
# now, use the ParseDict function to check if the data has been encoded properly
ParseDict(newdict, feed3)
# write it out to disk
with open('VehiclePositions.pb','wb') as f:
f.write(feed3.SerializeToString())
https://developers.google.com/transit/gtfs-realtime/guides/vehicle-positions