Example #1
0
    def produce_trip_updates(self):
        # initialise the feed message parser from Google
        feed = gtfs_realtime_pb2.FeedMessage()
        # get the response from the api
        response = requests.get(self.act_lr_api_url)
        # pass the response to the Parser
        feed.ParseFromString(response.content)

        # loop through feed entity
        for entity in feed.entity:
            if entity.HasField('trip_update'):  # only get trip updates
                trip_id = entity.trip_update.trip.trip_id
                print('**Trip ID**: ', trip_id)
                stop_time_updates = entity.trip_update.stop_time_update
                print('Count of updates: ', len(stop_time_updates))
                print('--------')

                update_json = MessageToDict(entity.trip_update)
                update_json['id'] = int(entity.id)
                update_json = json.dumps(update_json)
                self.kafka_producer.produce(self.kafka_topic,
                                            update_json.encode('utf-8'))

            self.kafka_producer.flush()