def __make_trip_updates(cls, predictions): trip_updates = [] for idx, prediction in enumerate(predictions): entity_id = str(idx + 1) relationships = prediction['relationships'] attributes = prediction['attributes'] stop_id = relationships['stop']['data']['id'] route_id = relationships['route']['data']['id'] trip_id = relationships['trip']['data']['id'] raw_arrival_time = attributes['arrival_time'] raw_departure_time = attributes['departure_time'] if raw_arrival_time and raw_departure_time: arrival_time = cls.__to_unix_time(attributes['arrival_time']) departure_time = cls.__to_unix_time( attributes['departure_time']) trip_update = TripUpdate.create(entity_id=entity_id, route_id=route_id, stop_id=stop_id, trip_id=trip_id, arrival_time=arrival_time, departure_time=departure_time) trip_updates.append(trip_update) return trip_updates
def test_models_schema_output(): entity_id = '1' arrival_time = 1234 trip_id = '1234' stop_id = '2345' route_id = '3456' trip_update = TripUpdate.create(entity_id=entity_id, arrival_time=arrival_time, trip_id=trip_id, stop_id=stop_id, route_id=route_id) entities = [ trip_update ] message = FeedMessage.create(entities=entities) assert type(message) == gtfs_realtime.FeedMessage assert type(message.header) == gtfs_realtime.FeedHeader assert isinstance(message.entity, Iterable) assert len(message.entity) == 1 entity = message.entity[0] assert type(entity) == gtfs_realtime.FeedEntity trip_update = entity.trip_update assert type(trip_update) == gtfs_realtime.TripUpdate assert isinstance(trip_update.stop_time_update, Iterable) assert len(trip_update.stop_time_update) == 1 assert isinstance(trip_update.trip, gtfs_realtime.TripDescriptor) stop_time_update = trip_update.stop_time_update[0] assert type(stop_time_update) == gtfs_realtime.TripUpdate.StopTimeUpdate assert type(stop_time_update.arrival) == gtfs_realtime.TripUpdate.StopTimeEvent assert type(stop_time_update.departure) == gtfs_realtime.TripUpdate.StopTimeEvent
def generate_trip_updates(self, entities): trip_updates = [] for idx, entity in enumerate(entities): entity_id = str(idx+1) trip_update = entity['trip_update'] trip = trip_update.get('trip') trip_id = trip.get('trip_id') route_id = trip.get('route_id') stop_time_update = trip_update.get('stop_time_update') for update in stop_time_update: stop_id = update.get("stop_id") arrival = update.get("arrival") departure = update.get("departure") if stop_id == self.stop_id: arrival_delay = arrival.get('delay',None) departure_delay = departure.get('delay',None) trip_update = TripUpdate.create( entity_id=entity_id, arrival_delay=arrival_delay, departure_delay=departure_delay, trip_id=trip_id, route_id=route_id, stop_id=stop_id ) trip_updates.append(trip_update) return trip_updates
def __make_trip_updates(cls, data): trip_updates = [] station_data_item = data['STATION']['ITEMS'].values() for value in station_data_item: for idx, item_entry in enumerate(value): # Intersection Extensions headsign = item_entry['DESTINATION'] route_short_name = cls.__get_route_short_name(item_entry) route_long_name = cls.__get_route_long_name(item_entry) route_color = item_entry['BACKCOLOR'] route_text_color = item_entry['FORECOLOR'] block_id = item_entry['TRAIN_ID'] track = item_entry['TRACK'] stop_id = data['STATION']['STATION_2CHAR'] stop_name = data['STATION']['STATIONNAME'] scheduled_datetime = cls.__to_unix_time( item_entry['SCHED_DEP_DATE']) departure_time = int( scheduled_datetime.add( seconds=int(item_entry['SEC_LATE'])).timestamp()) scheduled_departure_time = int(scheduled_datetime.timestamp()) custom_status = item_entry['STATUS'] origin_and_destination = None for stop in item_entry['STOPS'].values(): origin_and_destination = [stop[i] for i in (0, -1)] route_id = cls.__get_route_id(item_entry, origin_and_destination) trip_update = TripUpdate.create( entity_id=str(idx + 1), departure_time=departure_time, scheduled_departure_time=scheduled_departure_time, arrival_time=departure_time, scheduled_arrival_time=scheduled_departure_time, route_id=route_id, route_short_name=route_short_name, route_long_name=route_long_name, route_color=route_color, route_text_color=route_text_color, stop_id=stop_id, stop_name=stop_name, headsign=headsign, track=track, block_id=block_id, agency_timezone=cls.TIMEZONE, custom_status=custom_status) trip_updates.append(trip_update) return trip_updates
def __make_trip_update(cls, _id, stop_id, arrival): entity_id = str(_id + 1) now = int(pendulum.now().timestamp()) arrival_time = now + math.floor(arrival['seconds'] / 60) * 60 trip_id = cls.calculate_trip_id(arrival['trip_id']) route_id = arrival.get('route_id', '') return TripUpdate.create(entity_id=entity_id, arrival_time=arrival_time, trip_id=trip_id, route_id=route_id, stop_id=stop_id)
def test_arrival_time_is_used_if_no_departure_time(): entity_id = '1' arrival_time = 1234 trip_id = '1234' stop_id = '2345' route_id = '3456' entity = TripUpdate.create(entity_id=entity_id, arrival_time=arrival_time, trip_id=trip_id, stop_id=stop_id, route_id=route_id) assert entity.trip_update.stop_time_update[0].arrival.time == arrival_time assert entity.trip_update.stop_time_update[0].departure.time == arrival_time
def __make_trip_update(cls, _id, stop_id, arrival): entity_id = str(_id + 1) route_id = cls.ROUTE_ID_LOOKUP.get(arrival['line'], None) arrival_time = cls.calculate_realtime(arrival['sched_time'], arrival['status']) departure_time = cls.calculate_realtime(arrival['depart_time'], arrival['status']) return TripUpdate.create( entity_id=entity_id, arrival_time=arrival_time, departure_time=departure_time, stop_id=stop_id, route_id=route_id, scheduled_arrival_time=arrival['sched_time'], scheduled_departure_time=arrival['depart_time'], track=arrival['track'], headsign=arrival['destination'])
def __make_trip_update(cls, _id, prediction): entity_id = str(_id + 1) route_id = prediction['rt'] stop_id = prediction['stpId'] is_scheduled = prediction['isSch'] == '1' parsed_arrival_time = cls.__to_unix_time(prediction['arrT']) arrival_time = None if is_scheduled else parsed_arrival_time ##### Intersection Extensions headsign = prediction['destNm'] scheduled_arrival_time = parsed_arrival_time if is_scheduled else None return TripUpdate.create(entity_id=entity_id, route_id=route_id, stop_id=stop_id, arrival_time=arrival_time, headsign=headsign, scheduled_arrival_time=scheduled_arrival_time)
def __make_trip_update(cls, _id, prediction): entity_id = str(_id + 1) route_id = prediction['rt'] stop_id = prediction['stpid'] # Per the docs, the prediction is either for a scheduled departure, or realtime arrival departure_type = 'D' is_scheduled = prediction['typ'] == departure_type parsed_arrival_time = cls.__to_unix_time(prediction['prdtm']) arrival_time = None if is_scheduled else parsed_arrival_time ##### Intersection Extensions headsign = prediction['des'] scheduled_arrival_time = parsed_arrival_time if is_scheduled else None return TripUpdate.create(entity_id=entity_id, route_id=route_id, stop_id=stop_id, arrival_time=arrival_time, headsign=headsign, scheduled_arrival_time=scheduled_arrival_time)
def __make_trip_updates(cls, data): trip_updates = [] stations = data['stations'] for station in stations: station_shortkey = station['abbrv'] tracks = station.get('tracks') if station_shortkey == 'systemwide': continue for track in tracks: track_id = track.get('trackId') trains = track.get('trains', {}) for idx, train in enumerate(trains): if not train.get('trainId'): continue train_info = train.get('trainId').split('_') service_id = train.get('service') destination = train.get('destination') arrival_time = train.get('depArrTime') scheduled_arrival_time = cls.__to_unix_time(train_info[0]) arrival_data = cls.__route_lookup(service_id, station_shortkey, track_id, destination) if cls.__should_skip_update(arrival_data): continue trip_update = TripUpdate.create( entity_id=train.get('trainId').strip(), departure_time=arrival_time, arrival_time=arrival_time, scheduled_arrival_time=scheduled_arrival_time, scheduled_departure_time=scheduled_arrival_time, track=track_id, route_id=arrival_data.get('route_id'), stop_id=arrival_data.get('stop_id'), headsign=arrival_data.get('headsign'), stop_name=arrival_data.get('stop_name')) trip_updates.append(trip_update) return trip_updates
def __make_trip_update(cls, _id, route_id, stop_name, arrival): entity_id = str(_id + 1) arrival_time = arrival['serviceDay'] + arrival['realtimeArrival'] departure_time = arrival['serviceDay'] + arrival['realtimeDeparture'] trip_id = cls.parse_id(arrival['tripId']) stop_id = cls.parse_id(arrival['stopId']) ##### Intersection Extensions headsign = arrival['tripHeadsign'] scheduled_arrival_time = arrival['serviceDay'] + arrival['scheduledArrival'] scheduled_departure_time = arrival['serviceDay'] + arrival['scheduledDeparture'] track = arrival['track'] return TripUpdate.create(entity_id=entity_id, arrival_time=arrival_time, departure_time=departure_time, trip_id=trip_id, route_id=route_id, stop_id=stop_id, stop_name=stop_name, headsign=headsign, scheduled_arrival_time=scheduled_arrival_time, scheduled_departure_time=scheduled_departure_time, track=track)
def __make_trip_updates(cls, data, filtered_stops): trip_updates = [] trips = data['SCHEDULEROWSET'].values() for value in trips: for idx, item_entry in enumerate(value): # Intersection Extensions headsign = item_entry['busheader'] trip_id = item_entry['gtfs_trip_id'] route_id = item_entry['gtfs_route_id'] if item_entry.get('STOP', None) is not None: stops = item_entry['STOP'] # If there is only one <STOP> for the <TRIP> we have to explicitly create a list if not isinstance(stops, list): stops = [stops] # Process Stops for stop in stops: stop_code = stop['gtfs_stop_Code'] # Get Stop ID for the given Stop Code From the Mapping stop_id = NJTBusStopCodeIdMappings.get_stop_id( stop_code) if cls.__skip_processing(stop_id, filtered_stops): continue stop_name = stop['stopname'] track = stop['manual_lane_gate'] if not track: track = stop['scheduled_lane_gate'] scheduleddeparturedate = stop['scheduleddeparturedate'] scheduleddeparturetime = stop['scheduleddeparturetime'] scheduled_datetime = cls.__to_unix_time("{} {}".format( scheduleddeparturedate.title(), scheduleddeparturetime)) scheduled_departure_time = int( scheduled_datetime.timestamp()) sec_late = 0 if stop['sec_late']: sec_late = int(stop['sec_late']) arrival_time = int( scheduled_datetime.add( seconds=sec_late).timestamp()) trip_update = TripUpdate.create( entity_id=str(idx + 1), route_id=route_id, trip_id=trip_id, stop_id=stop_id, headsign=headsign, stop_name=stop_name, track=track, arrival_time=arrival_time, departure_time=arrival_time, scheduled_departure_time=scheduled_departure_time, scheduled_arrival_time=scheduled_departure_time, agency_timezone=cls.TIMEZONE) trip_updates.append(trip_update) return trip_updates