Example #1
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " + str(e)

        timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp, self.TIMEZONE)

        aaa = 0
        for entity in feed.entity:
            # Trip update represents a change in timetable
            if entity.HasField('trip_update'):
                self.tripUpdates.append([])
                update = tripupdate.tripupdate()
                self.tripUpdates[len(self.tripUpdates) - 1].append(
                    entity.trip_update.trip.trip_id)
                self.tripUpdates[len(self.tripUpdates) - 1].append(
                    entity.trip_update.trip.route_id)
                self.tripUpdates[len(self.tripUpdates) - 1].append(
                    entity.trip_update.trip.start_date)
                self.tripUpdates[len(self.tripUpdates) - 1].append(
                    entity.trip_update.stop_time_update[0].stop_id[-1])
                for stop in entity.trip_update.stop_time_update:
                    update.futureStops[stop.stop_id] = [{
                        'arrivaltime':
                        stop.arrival.time or None
                    }, {
                        'departuretime':
                        stop.departure.time or None
                    }]
                self.tripUpdates[len(self.tripUpdates) - 1].append(
                    update.futureStops)
                self.tripUpdates[len(self.tripUpdates) - 1].append(timestamp)
                self.tripUpdates[len(self.tripUpdates) - 1].append("-1")
                self.tripUpdates[len(self.tripUpdates) - 1].append(-1)
                self.tripUpdates[len(self.tripUpdates) - 1].append(-1)

            if entity.HasField('vehicle'):
                self.tripUpdates[len(self.tripUpdates) -
                                 1][6] = entity.vehicle.stop_id
                #print "$$$$$$$$$$" +newItem.currentStopId+ "$$$$$$$$$$"
                self.tripUpdates[len(self.tripUpdates) -
                                 1][7] = entity.vehicle.timestamp
                #print "!!!!!!!!!!" +str(newItem.vehicleTimeStamp)+ "!!!!!!!!!!"
                self.tripUpdates[len(self.tripUpdates) -
                                 1][8] = entity.vehicle.current_status
                #print "**********" +str(newItem.currentStopStatus)+ "**********"

            if entity.HasField('alert'):
                a = alert.alert()
                a.alertMessage = entity.alert.header_text.translation
                #a.alertMessage = entity.alert.header_text.translation.text
                self.alerts.append(a)

            #### INSERT ALERT CODE HERE #####

        return self.tripUpdates
Example #2
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " +str(e)
	

	timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp,self.TIMEZONE)
	
	for entity in feed.entity:
	    # Trip update represents a change in timetable
	    if entity.trip_update and entity.trip_update.trip.trip_id:
		update = tripupdate.tripupdate()

		##### INSERT TRIPUPDATE CODE HERE ####	

	    if entity.vehicle and entity.vehicle.trip.trip_id:
	    	v = vehicle.vehicle()

		##### INSERT VEHICLE CODE HERE #####
	    
	    if entity.alert:
                a = alert.alert()=

                #### INSERT ALERT CODE HERE #####

	return self.tripUpdates
Example #3
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " +str(e)
	

	timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp,self.TIMEZONE)
	
	for entity in feed.entity:
	    # Trip update represents a change in timetable
	    if entity.trip_update and entity.trip_update.trip.trip_id:
		update = tripupdate.tripupdate()
		
		##### INSERT TRIPUPDATE CODE HERE ####			

	    if entity.vehicle and entity.vehicle.trip.trip_id:
	    	v = vehicle.vehicle()
		##### INSERT VEHICLE CODE HERE #####
	    
	    if entity.alert:
                a = alert.alert()
		#### INSERT ALERT CODE HERE #####

	return self.tripUpdates
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " + str(e)

        self.timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(self.timestamp, self.TIMEZONE)

        for entity in feed.entity:
            # Trip update represents a change in timetable
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()
                ##### INSERT TRIPUPDATE CODE HERE ####
                update.tripId = str(entity.trip_update.trip.trip_id)
                update.routeId = str(entity.trip_update.trip.route_id)
                update.startDate = entity.trip_update.trip.start_date
                update.direction = str(entity.trip_update.trip.trip_id[10])
                curr_vehicle = self.returnVehicle(update.tripId)
                num = 0
                for i in entity.trip_update.stop_time_update:
                    update.futureStops[num] = (
                        '"'
                        + str(i.stop_id)
                        + '": [{"arrivalTime": '
                        + str(i.arrival.time)
                        + '}, {"departureTime": '
                        + str(i.departure.time)
                        + "}]"
                    )
                    #                    print update.futureStops[num]
                    num += 1
                if curr_vehicle is not None:
                    update.vehicleData = curr_vehicle
                self.tripUpdates.append(update)
            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                ##### INSERT VEHICLE CODE HERE #####
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.currentStopId = str(entity.vehicle.stop_id)
                v.timestamp = entity.vehicle.timestamp
                v.currentStopStatus = entity.vehicle.current_status
                v.trip_id = str(entity.vehicle.trip.trip_id)
                v.start_date = entity.vehicle.trip.start_date
                v.route_id = entity.vehicle.trip.route_id
                self.vehicles.append(v)
                cor_trip = self.returnTrip(v.trip_id)
                cor_alert = self.returnAlert(v.trip_id)
                if cor_trip is not None:
                    cor_trip.vehicleData = v

            if entity.alert:
                a = alert.alert()
                #### INSERT ALERT CODE HERE #####
                a.alertMessage = entity.alert.header_text.translation
                self.alerts.append(a)

        return self.tripUpdates
Example #5
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " +str(e)

        timestamp = feed.header.timestamp
        print "Timestamp: ", timestamp
        nytime = datetime.fromtimestamp(timestamp, self.TIMEZONE)
        print "NYC Time: ", nytime

        self.timestamp = timestamp
        self.tripUpdates = []
        self.alerts = []

        for entity in feed.entity:
            # Trip update represents a change in timetable
            if entity.trip_update and entity.trip_update.trip.trip_id:
                # Assign the tripupdate fields
                t = tripupdate.tripupdate()
                t.tripId = entity.trip_update.trip.trip_id
                t.routeId = entity.trip_update.trip.route_id
                t.startDate = entity.trip_update.trip.start_date
                t.direction = entity.trip_update.trip.direction_id

                # There can be many StopTimeUpdate messages
                for st_update in entity.trip_update.stop_time_update:
                    times = []
                    times.append({"arrivalTime": st_update.arrival.time})
                    times.append({"departureTime": st_update.departure.time})
                    t.futureStops[st_update.stop_id] = times

                self.tripUpdates.append(t)

            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.currentStopId = entity.vehicle.stop_id
                v.timestamp = entity.vehicle.timestamp
                v.currentStopStatus = entity.vehicle.current_status

                # Using the trip_id on a vehicle, associate that vehicle with the trip update object
                vehicleTripId = entity.vehicle.trip.trip_id
                self.setVehicleDataOnTripUpdate(vehicleTripId, v)

            if entity.HasField('alert') and entity.alert:
                a = alert.alert()
                a.alertMessage = entity.alert.header_text.translation[0].text
                for informed_entity in entity.alert.informed_entity:
                    tripId = informed_entity.trip.trip_id
                    a.tripId.append(tripId)
                    a.routeId[tripId] = informed_entity.trip.route_id
                    a.startDate[tripId] = informed_entity.trip.start_time
                self.alerts.append(a)

        return self.tripUpdates
Example #6
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())

        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " + str(e)

        timestamp = feed.header.timestamp
        #nytime = datetime.fromtimestamp(timestamp,self.TIMEZONE)

        for entity in feed.entity:
            #Trip update represents a change in timetable
            if entity.trip_update and entity.trip_update.trip.trip_id:
                trip_id = entity.trip_update.trip.trip_id
                update = tripupdate.tripupdate()

                update.tripId = entity.trip_update.trip.trip_id
                update.routeId = entity.trip_update.trip.route_id
                update.startDate = entity.trip_update.trip.start_date

                # parse trip id for direction
                if (re.search('N', str(entity.trip_update.trip.trip_id))):
                    update.direction = 'N'
                else:
                    update.direction = 'S'

                ### stop_time_update is a LIST ###
                for stop_time_updates in entity.trip_update.stop_time_update:
                    departureTime = None
                    arrivalTime = stop_time_updates.arrival.time
                    stopId = stop_time_updates.stop_id
                    if stop_time_updates.departure:
                        departureTime = stop_time_updates.departure.time
                    update.futureStops[stopId] = [{
                        "arrivalTime":
                        arrivalTime,
                        "departureTime":
                        departureTime
                    }]

                self.tripUpdates[trip_id] = update

            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()

                v.currentStopId = entity.vehicle.stop_id
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.timestamp = entity.vehicle.timestamp
                v.currentStopStatus = entity.vehicle.current_status

                update.vehicleData = v

                self.tripUpdates[str(entity.vehicle.trip.trip_id)] = update

        return self.tripUpdates, timestamp
Example #7
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " +str(e)
	

        timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp,self.TIMEZONE)
	
        for entity in feed.entity:
            # Trip update represents a change in timetable
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()
		
                ##### INSERT TRIPUPDATE CODE HERE ####	

                update.tripId       = entity.trip_update.trip.trip_id
                update.routeId      = entity.trip_update.trip.route_id
                update.startDate    = entity.trip_update.trip.start_date
                update.direction    = entity.trip_update.stop_time_update[-1].stop_id[-1]
                update.vehicleData  = entity.trip_update.vehicle
		newkey = entity.trip_update.stop_time_update[-1].stop_id
		low    = entity.trip_update.stop_time_update[-1].arrival.time
		hi     = entity.trip_update.stop_time_update[-1].departure.time
                update.futureStops.update({ newkey : [low,hi] })# Format {stopId : [arrivalTime,departureTime]})

            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                
                ##### INSERT VEHICLE CODE HERE #####
                
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.currentStopId     = entity.vehicle.stop_id
                v.timestamp         = entity.vehicle.timestamp
                v.currentStopStatus = entity.vehicle.current_status
            if entity.alert:
                a = alert.alert()
                
                #### INSERT ALERT CODE HERE #####
                #print entity.alert.informed_entity
                a.tripId            = entity.trip_update.trip.trip_id
                a.routeId           = entity.trip_update.trip.route_id
                a.startDate         = entity.trip_update.trip.start_date
		try:
                    a.alertMessage  = entity.alert.header_text.translation.text
		except:
		    a.alertMessage  = ''
	
	self.tripUpdates.append(update)
	self.tripUpdates.append(v)
	self.tripUpdates.append(a)

	return self.tripUpdates
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " + str(e)

        timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp, self.TIMEZONE)

        for entity in feed.entity:
            # Trip update represents a change in timetable
            update = None
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()
                update.tripId = entity.trip_update.trip.trip_id
                update.routeId = entity.trip_update.trip.route_id
                update.startDate = entity.trip_update.trip.start_date
                update.direction = entity.trip_update.trip.trip_id[
                    entity.trip_update.trip.trip_id.find("..") + 2]
                update.vehicleData = entity.vehicle

                for e in entity.trip_update.stop_time_update:
                    arrivalTime = e.arrival.time
                    departureTime = e.departure.time
                    v1 = {"arrivalTime": arrivalTime}
                    v2 = {"departureTime": departureTime}
                    value = [v1, v2]
                    update.futureStops[e.stop_id] = value

            v = None
            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.currentStopId = entity.vehicle.stop_id
                v.timestamp = entity.vehicle.timestamp
                v.currentStopStatus = entity.vehicle.current_status

            a = None
            if entity.alert:
                a = alert.alert()
                for trip in entity.alert.informed_entity:
                    a.tripId.append(trip.trip_id)
                    a.routeId[trip.trip_id] = trip.route_id
                    a.startDate[trip.trip_id] = trip.start_date

            self.tripUpdates.append([update, v, a])

        return self.tripUpdates
Example #9
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " + str(e)

        timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp, self.TIMEZONE)

        for entity in feed.entity:
            # Trip update represents a change in timetable
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()

                ##### INSERT TRIPUPDATE CODE HERE ####
                update.tripId = entity.trip_update.trip.trip_id
                update.routeId = entity.trip_update.trip.route_id
                update.startDate = entity.trip_update.trip.start_date
                update.direction = entity.trip_update.trip.trip_id[10]
                update.futureStop[
                    entity.trip_update.stop_time_update.stop_id] = [
                        entity.trip_update.stop_time_update.arrival.time,
                        entity.trip_update.stop_time_update.departure.time
                    ]

            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                ##### INSERT VEHICLE CODE HERE #####
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.currentStopId = entity.vehicle.stop_id
                v.timestamp = entity.vehicle.timestamp
                v.currentStopStatus = entity.vehicle.current_status

            if entity.alert:
                a = alert.alert()
                #### INSERT ALERT CODE HERE #####
                alert.tripId = entity.alert.informed_entity.trip.trip_id
                alert.routeId = entity.alert.informed_entity.trip.route_id
                alert.startDate = entity.trip_update.trip.start_date

        return self.tripUpdates
Example #10
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " +str(e)

        timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp,self.TIMEZONE)

        for entity in feed.entity:
            # Trip update represents a change in timetable
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()
                ##### INSERT TRIPUPDATE CODE HERE ####
                header_time = feed.header.timestamp
                update.tripId = entity.trip_update.trip.trip_id
                update.routeId = entity.trip_update.trip.route_id
                update.startDate = entity.trip_update.trip.start_date
                directionId = update.tripId.rfind('.')
                directionInt = int(directionId)

                update.direction = update.tripId[int(directionInt+1):int(directionInt+2)]
                update.time = header_time
                update.futureStops = OrderedDict()

                for stop in entity.trip_update.stop_time_update:
                    stopId = stop.stop_id
                    stopInfo = OrderedDict()
                    stopInfo['arrivalTime'] = stop.arrival.time
                    stopInfo['departureTime'] = stop.departure.time
                    stopInfo['arrivalTime'] = stop.arrival.time
                    stopInfo['departureTime'] = stop.departure.time
                    update.futureStops[stopId] = stopInfo
                    #print "check1"
                self.tripUpdates.append(update)
                    #print self.tripUpdates

            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                ##### INSERT VEHICLE CODE HERE #####
                header_time_v = entity.vehicle.timestamp
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.currentStopId = entity.vehicle.stop_id
                v.timestamp = header_time_v
                v.currentStopStatus = entity.vehicle.current_status

                for trip in self.tripUpdates:
                    if trip.tripId == entity.vehicle.trip.trip_id:
                        trip.vehicleData = v
                        #print "check2"

            if entity.alert:
                a = alert.alert()
                #### INSERT ALERT CODE HERE #####
                if entity.alert and entity.alert.header_text and entity.alert.header_text.translation and hasattr(entity.alert.header_text.translation,'text'):
                    a.alertMessage = entity.alert.header_text.translation.text
                if entity.alert and entity.alert.informed_entity and hasattr(entity.alert.informed_entity,'trip'):
                    if hasattr(entity.alert.informed_entity.trip,'trip_id'):
                        a.tripId = entity.alert.informed_entity.trip.trip_id
                        for tripUpdate in self.tripUpdates:
                            if tripUpdate.tripId == entity.alert.informed_entity.trip.trip_id:
                                a.startDate = tripUpdate.startDate
                    if entity.alert.informed_entity.trip.route_id:
                        a.routeId = entity.alert.informed_entity.trip.route_id
                        #print "check3"

                self.alerts.append(a)

        return self.tripUpdates
Example #11
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " +str(e)
    
        print "timestamp = " + str(feed.header.timestamp)
        ts = datetime.datetime.fromtimestamp(int(feed.header.timestamp), self.TIMEZONE)
        ts = ts.replace(tzinfo=None)
        #timestamp = datetime.datetime.fromtimestamp(int(nytime))

        start_time = ts.replace(hour=0, minute=0, second=0, microsecond=0)

        #start_time = datetime.datetime(year, month, day, tzinfo=self.TIMEZONE) 
        print "start_time = " + start_time.strftime("%Y-%m-%d %H:%M");
        print "cur_time = " + ts.strftime("%Y-%m-%d %H:%M");
        timestamp = int((ts - start_time).total_seconds() / 60)
        dayOfWeek = ts.weekday()
        print "minutes = " + str(timestamp)
        print "dayOfWeek = " + str(dayOfWeek)
        if (int(dayOfWeek) >= 5):
            dayOfWeek = "weekend"
        else:
            dayOfWeek = "weekday"


        for entity in feed.entity:
        # Trip update represents a change in timetable
            if entity.HasField('trip_update'):
                tmp = []
                #self.tripUpdates.append([])
                find = 0
                update = tripupdate.tripupdate()
                tmp.append(entity.trip_update.trip.trip_id)
                route_id = entity.trip_update.trip.route_id
                try:
                    route_id = int(route_id)
                except ValueError:
                    continue
                if (route_id > 3):
                    continue
                tmp.append(entity.trip_update.trip.route_id)
                tmp.append(timestamp)
                tmp.append(dayOfWeek)
                tmp.append(-1)
                tmp.append(-1)
                # find 96th and 42th street
                for stop in entity.trip_update.stop_time_update:
                    if (stop.stop_id == '120S'):
                        find = find + 1 
                        stamp = datetime.datetime.fromtimestamp(int(stop.arrival.time))
                        t = int((stamp - start_time).total_seconds() / 60)
                        tmp[4] = (t)
                    if (stop.stop_id in self.stopAt42):
                        find = find + 1
                        stamp = datetime.datetime.fromtimestamp(int(stop.arrival.time))
                        t = int((stamp - start_time).total_seconds() / 60)
                        tmp[5] = (t)

                print "entity = " + entity.id + " find = " + str(find)
                if (find < 2):
                    continue
                self.tripUpdates.append(tmp)

            if entity.HasField('vehicle'):
                if ((entity.vehicle.stop_id in self.stopAt42) and (find == 2)):
                    stamp = datetime.datetime.fromtimestamp(int(entity.vehicle.timestamp))
                    t = int((stamp - start_time).total_seconds() / 60)
                    self.tripUpdates[len(self.tripUpdates)-1][5] = t

            #### INSERT ALERT CODE HERE #####

        
        return self.tripUpdates
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " +str(e)
	

	timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp,self.TIMEZONE)
	
	for entity in feed.entity:
	    # Trip update represents a change in timetable
	    if entity.trip_update and entity.trip_update.trip.trip_id:
		update = tripupdate.tripupdate()
		update.tripId = entity.trip_update.trip.trip_id
		update.routeId = entity.trip_update.trip.route_id
		update.startDate = entity.trip_update.trip.start_date
		# We can get direction from tripId
		update.direction = update.tripId[10]
		
		# Route id could be 1,2,3,4,5,6 or S.
                # However for S they use  GS
                if update.routeId == 'GS':
                    update.routeId = 'S'
		
		# Create an ordered dictionary
		for stopUpdate in entity.trip_update.stop_time_update:
		    arrivalTime = {"arrivalTime" : stopUpdate.arrival.time}
		    departureTime = {"departureTime" : stopUpdate.departure.time}    
		    update.futureStops[stopUpdate.stop_id] = [arrivalTime, departureTime]
		
		
		self.tripUpdates.append(update)

	    if entity.vehicle and entity.vehicle.trip.trip_id:
	    	v = vehicle.vehicle()
		vehicleData = entity.vehicle
		tripId = vehicleData.trip.trip_id
		v.currentStopNumber = vehicleData.current_stop_sequence
		v.currentStopId = vehicleData.stop_id
		v.timestamp   =  vehicleData.timestamp
		if not vehicleData.current_status:
		    v.currentStopStatus = self.VCS[1]
		else:
		    v.currentStopStatus = self.VCS[vehicleData.current_status]
		# Find the tripUpdate object with the exact tripId
	    	tripUpdateObject = next((trip for trip in self.tripUpdates if trip.tripId == tripId), None)
		tripUpdateObject.vehicleData = v
	    
	    if entity.alert:
                a = alert.alert()
		for item in entity.alert.informed_entity:
		    trip = item.trip.trip_id
                    a.tripId.append(trip)
                    a.routeId[trip] = item.trip.route_id  
		a.alertMessage = entity.alert.header_text
		self.alerts.append(a)

	return self.tripUpdates
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        #print type(time)
	try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " +str(e)
    

        timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp,self.TIMEZONE)
        tripUpdates = []
	uList = []
        vList = []
    	aList = []
        for entity in feed.entity:
            # Trip update represents a change in timetable
            
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()
                update.tripId = entity.trip_update.trip.trip_id
                update.routeId = entity.trip_update.trip.route_id
                update.startDate = entity.trip_update.trip.start_date
                tripId = entity.trip_update.trip.trip_id
                
                update.direction = tripId[tripId.rfind('.')+1]# n/s  parse from trip id

                #update.vehicleData = None

                d = OrderedDict()
                for stop in entity.trip_update.stop_time_update:
                    arrivalDict = {}
		    departDict = {}
		    arrive = None
                    depart = None
            
                    arrive = stop.arrival.time
                    depart = stop.departure.time

                    arrivalDict["arrival_time"] = arrive
		    departDict["depart_time"] = depart
		    d[stop.stop_id] = [arrivalDict, departDict]
                update.futureStops = d # Format {stopId : [arrivalTime,departureTime]}
                
                uList.append(update)            
            ##### INSERT TRIPUPDATE CODE HERE ####          

            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.currentStopId = entity.vehicle.stop_id
                v.timestamp = entity.vehicle.timestamp
                v.currentStopStatus = entity.vehicle.current_status
                v.tripId = entity.vehicle.trip.trip_id
                vList.append(v)
            ##### INSERT VEHICLE CODE HERE #####
            if entity.alert:
                a = alert.alert()
        if hasattr(entity.alert, 'trip'):
                    a.tripId = entity.alert.trip.trip_id
                    a.routeId = entity.alert.trip.route_id
                    a.startDate = entity.alert.trip.start_date
                    a.alertMessage = entity.alert.header_text.translation           
            #### INSERT ALERT CODE HERE #####
        
        tripUpdates.append(uList)
        tripUpdates.append(vList)
        tripUpdates.append(aList)
        tripUpdates.append(timestamp)
        return tripUpdates
Example #14
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " + str(e)

        timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp, self.TIMEZONE)
        self.timest = nytime
        for entity in feed.entity:
            # Trip update represents a change in timetable
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()

                ##### INSERT TRIPUPDATE CODE HERE ####
                update.tripId = entity.trip_update.trip.trip_id
                update.routeId = entity.trip_update.trip.route_id
                update.startDate = entity.trip_update.trip.start_date
                #update.direction = entity.trip_update.trip.trip_id[10]
                #update.direction = entity.trip_update.stop_time_update.stop_id[3]
                update.starttime = entity.trip_update.trip.start_time
                i = 1
                for stopid in entity.trip_update.stop_time_update:
                    update.futureStops[stopid.stop_id] = [
                        stopid.arrival.time, stopid.departure.time
                    ]
                    if i == 1:
                        update.direction = stopid.stop_id[3]
                    i = i + 1
#		    print update.futureStops[stopid.stop_id]
                self.tripUpdates.append(update)
            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                ##### INSERT VEHICLE CODE HERE #####
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.currentStopId = entity.vehicle.stop_id
                v.timestamp = entity.vehicle.timestamp
                v.currentStopStatus = self.VCS[entity.vehicle.current_status +
                                               1]
                v.tripId = entity.vehicle.trip.trip_id
                self.vehicleUpdates.append(v)
            if entity.alert and entity.alert.informed_entity:
                a = alert.alert()
                #### INSERT ALERT CODE HERE #####
                for alerttrip in entity.alert.informed_entity:
                    alert.tripId = alerttrip.trip.trip_id
                    alert.routeId = alerttrip.trip.route_id
                    #         alert.startDate = alertrrip.trip.start_date
                    a.tripId = alerttrip.trip.trip_id
                    a.routeId = alerttrip.trip.route_id
        #     a.startDate = entity.trip_update.trip.start_date

        for i in self.tripUpdates:
            for j in self.vehicleUpdates:
                if i.tripId == j.tripId:
                    i.vehicleData = [
                        j.currentStopId, j.currentStopStatus, j.timestamp
                    ]
#		    print i.tripId,i.routeId,j.tripId,j.currentStopNumber

        return self.tripUpdates, self.timest
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib.request.urlopen(
                    self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except:
            print('connection error')
            # return "ERROR"
        '''
        except (urllib.error.URLError, google.protobuf.message.DecodeError) as e:\
            print ("Error while connecting to mta server " +str(e))'''
        timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp, self.TIMEZONE)

        self.tripUpdates = []
        for entity in feed.entity:
            # Trip update represents a change in timetable
            duplicate = 0
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()
                update.tripId = str(entity.trip_update.trip.trip_id)
                update.routeId = str(entity.trip_update.trip.route_id)
                update.startDate = str(entity.trip_update.trip.start_date)
                update.direction = 'N' if 'N' in update.tripId else 'S'
                if entity.trip_update.stop_time_update:
                    for stop_update in entity.trip_update.stop_time_update:
                        arrival_time = stop_update.arrival.time if stop_update.HasField(
                            'arrival') else ' '
                        departure_time = stop_update.departure.time if stop_update.HasField(
                            'departure') else ' '
                        update.futureStops[str(stop_update.stop_id)] = [{
                            "arrivalTime":
                            arrival_time
                        }, {
                            "departureTime":
                            departure_time
                        }]
                for tripUpdate in self.tripUpdates:
                    if tripUpdate.tripId == update.tripId:
                        duplicate = 1
                        tripUpdate.futureStops = update.futureStops
                if duplicate == 0:
                    self.tripUpdates.append(update)

            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                v.currentStopSequence = str(
                    entity.vehicle.current_stop_sequence)
                v.currentStopId = str(entity.vehicle.stop_id)
                v.currentStopStatus = str(entity.vehicle.current_status)
                v.timestamp = entity.vehicle.timestamp
                for tripUpdate in self.tripUpdates:
                    if tripUpdate.tripId == str(entity.vehicle.trip.trip_id):
                        tripUpdate.vehicleData = v
                        duplicate = 1
                if duplicate == 0:
                    update = tripupdate.tripupdate()
                    update.tripId = str(entity.vehicle.trip.trip_id)
                    update.routeId = str(entity.vehicle.trip.route_id)
                    update.startDate = str(entity.vehicle.trip.start_date)
                    update.direction = 'N' if 'N' in update.tripId else 'S'
                    update.vehicleData = v
                    self.tripUpdates.append(update)
            if entity.alert:
                a = alert.alert()
        return [self.tripUpdates, timestamp]
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " + str(e)

        timestamp = feed.header.timestamp
        nytime = datetime.fromtimestamp(timestamp, self.TIMEZONE)

        for entity in feed.entity:
            # Trip update represents a change in timetable
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()
                update.tripId = entity.trip_update.trip.trip_id
                update.routeId = entity.trip_update.trip.route_id
                update.startDate = entity.trip_update.trip.start_date
                # We can get direction from tripId
                update.direction = update.tripId[10]

                # Route id could be 1,2,3,4,5,6 or S.
                # However for S they use  GS
                if update.routeId == 'GS':
                    update.routeId = 'S'

                # Create an ordered dictionary
                for stopUpdate in entity.trip_update.stop_time_update:
                    arrivalTime = {"arrivalTime": stopUpdate.arrival.time}
                    departureTime = {
                        "departureTime": stopUpdate.departure.time
                    }
                    update.futureStops[stopUpdate.stop_id] = [
                        arrivalTime, departureTime
                    ]

                self.tripUpdates.append(update)

            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                vehicleData = entity.vehicle
                tripId = vehicleData.trip.trip_id
                v.currentStopNumber = vehicleData.current_stop_sequence
                v.currentStopId = vehicleData.stop_id
                v.timestamp = vehicleData.timestamp
                if not vehicleData.current_status:
                    v.currentStopStatus = self.VCS[1]
                else:
                    v.currentStopStatus = self.VCS[vehicleData.current_status]

# Find the tripUpdate object with the exact tripId
                tripUpdateObject = next(
                    (trip
                     for trip in self.tripUpdates if trip.tripId == tripId),
                    None)
                tripUpdateObject.vehicleData = v

            if entity.alert:
                a = alert.alert()
                for item in entity.alert.informed_entity:
                    trip = item.trip.trip_id
                    a.tripId.append(trip)
                    a.routeId[trip] = item.trip.route_id
                a.alertMessage = entity.alert.header_text
                self.alerts.append(a)

        return self.tripUpdates
Example #17
0
    def getTripUpdates(self):
        feed = gtfs_realtime_pb2.FeedMessage()
        try:
            with contextlib.closing(urllib2.urlopen(self.feedurl)) as response:
                d = feed.ParseFromString(response.read())
        except (urllib2.URLError, google.protobuf.message.DecodeError) as e:
            print "Error while connecting to mta server " +str(e)

        timestamp = feed.header.timestamp
	#print type(timestamp)
        nytime = datetime.fromtimestamp(timestamp,self.TIMEZONE)

        trips={}

        for entity in feed.entity:
            # print entity
            # Trip update represents a change in timetable
            if entity.trip_update and entity.trip_update.trip.trip_id:
                update = tripupdate.tripupdate()
                ##### INSERT TRIPUPDATE CODE HERE ####
                update.tripId = str(entity.trip_update.trip.trip_id)
		#print str(update.tripId)[-4]
                update.routeId = entity.trip_update.trip.route_id
                update.startDate = entity.trip_update.trip.start_date
		st = update.tripId
                if st.split('.')[1] == '':
			update.direction = st.split('.')[2][0]
                else:
         		update.direction = st.split('.')[1][0]
		#print update.direction
		#print entity.trip_update.trip.direction_id
                if entity.trip_update.vehicle:
                      update.vehicleData = entity.trip_update.vehicle
                tmp = collections.OrderedDict()
                len_stop = len(entity.trip_update.stop_time_update)
                for i in range(len_stop):
            	    #print entity.trip_update.stop_time_update[i].stop_id
		    #print entity.trip_update.stop_time_update[i].arrival.time
 		    #print entity.trip_update.stop_time_update[i].departure.time
                    tmp[str(entity.trip_update.stop_time_update[i].stop_id)] = [
                        entity.trip_update.stop_time_update[i].arrival.time,
                        entity.trip_update.stop_time_update[i].departure.time
                    ]
		#print tmp.items()
		#tmp = collections.OrderedDict()
                update.futureStops = str(tmp.items())
		#print update.futureStops

                if update.tripId not in trips.keys():
                    trips[update.tripId] = ini()
                trips[update.tripId]['routeId'] = update.routeId
                trips[update.tripId]['startDate'] = update.startDate
                trips[update.tripId]['direction'] = update.direction
                trips[update.tripId]['futureStopData'] = update.futureStops
		trips[update.tripId]['timeStamp'] =  timestamp
                
		#print 'tripId: ',update.tripId, 
	        #print 'routeId: ',update.routeId, 
                #print 'startDate: ',update.startDate

            if entity.vehicle and entity.vehicle.trip.trip_id:
                v = vehicle.vehicle()
                ##### INSERT VEHICLE CODE HERE #####
                v.currentStopNumber = entity.vehicle.current_stop_sequence
                v.currentStopId = entity.vehicle.stop_id
                v.timestamp = entity.vehicle.timestamp
                v.currentStopStatus = self.VCS[int(entity.vehicle.current_status)+1]
                #print v.currentStopNumber, v.currentStopId, 
		#print v.currentStopStatus
                if entity.vehicle.trip.trip_id not in trips.keys():
                    trips[entity.vehicle.trip.trip_id] = ini()
                trips[entity.vehicle.trip.trip_id]['currentStopId'] = v.currentStopNumber
                trips[entity.vehicle.trip.trip_id]['currentStopStatus'] = v.currentStopStatus
                trips[entity.vehicle.trip.trip_id]['vehicleTimeStamp'] = v.timestamp
                trips[entity.vehicle.trip.trip_id]['timeStamp']= timestamp
		
            if entity.alert:
                a = alert.alert()
                #### INSERT ALERT CODE HERE #####
                a.alertMessage = []
                mes_len = len(entity.alert.header_text.translation)
                for i in range(mes_len):
                    a.alertMessage.append(entity.alert.header_text.translation[i].text)
                a.tripId = []
		trip_len = len(entity.alert.informed_entity)
                for i in range(trip_len):
                     a.tripId.append(entity.alert.informed_entity[i].trip.trip_id)
                     a.routeId[entity.alert.informed_entity[i].trip.trip_id] = entity.alert.informed_entity[i].trip.route_id
                     if entity.alert.informed_entity[i].trip.start_date:
		                  a.startDate[entity.alert.informed_entity[i].trip.trip_id] = entity.alert.informed_entity[i].trip.start_date
                self.alerts.append(a)
		
        #print trips
        self.tripUpdates.append(trips) 
	return self.tripUpdates