def poll_mbta_schedule_deviations(): fetch_timestamp = int(time.time()) # fetch feed from remote server fp = urlopen( FEED_URL ) gtfsrt_message = fp.read() # parse it using protocol buffers pp = grt.FeedMessage() pp.ParseFromString( gtfsrt_message ) try: info = Info.objects.all()[0] if pp.header.timestamp <= info.timestamp: print "nothing to do - already have the latest data\n" return except IndexError: info = Info(timestamp = pp.header.timestamp) ret = [] ret.append( "new timestamp %s > %s old timestamp"%(pp.header.timestamp, info.timestamp) ) info.timestamp = pp.header.timestamp info.save() # render it into an html response ret.append( "%s"%pp.header ) for i, entity in enumerate( pp.entity ): for stu in entity.trip_update.stop_time_update: sturec = StopTimeUpdate() sturec.trip_id = entity.trip_update.trip.trip_id sturec.start_date = entity.trip_update.trip.start_date sturec.stop_sequence = stu.stop_sequence if stu.HasField( 'stop_id' ): sturec.stop_id = stu.stop_id if stu.HasField( 'arrival' ): if stu.arrival.HasField( 'delay' ): sturec.arrival_delay = stu.arrival.delay if stu.arrival.HasField( 'time' ): sturec.arrival_time = stu.arrival.time if stu.arrival.HasField( 'uncertainty' ): sturec.arrival_uncertainty = stu.arrival.uncertainty if stu.HasField( 'departure' ): if stu.departure.HasField( 'delay' ): sturec.departure_delay = stu.departure.delay if stu.departure.HasField( 'time' ): sturec.departure_time = stu.departure.time if stu.departure.HasField( 'uncertainty' ): sturec.departure_uncertainty = stu.departure.uncertainty sturec.schedule_relationship = stu.schedule_relationship sturec.data_timestamp = pp.header.timestamp sturec.fetch_timestamp = fetch_timestamp sturec.save() ret.append( " - create stu: %s - %s"%(entity.trip_update.trip.trip_id, stu) ) #print "\n".join(ret) print print "%s - %s\n"%(datetime.datetime.fromtimestamp( fetch_timestamp), i)
def poll_mbta_vehicle_positions(): fetch_timestamp = int(time.time()) # fetch feed from remote server fp = urlopen(FEED_URL) gtfsrt_message = fp.read() # parse it using protocol buffers pp = grt.FeedMessage() pp.ParseFromString(gtfsrt_message) try: info = Info.objects.all()[0] if info.buspos_timestamp is not None and pp.header.timestamp <= info.buspos_timestamp: self.stdout.write("nothing to do - already have the latest data\n") return except IndexError: info = Info(buspos_timestamp=pp.header.timestamp) ret = [] print "new timestamp %s > %s old timestamp" % (pp.header.timestamp, info.timestamp) info.buspos_timestamp = pp.header.timestamp info.save() print pp.header for i, entity in enumerate(pp.entity): print ".", vprec = VehicleUpdate() if entity.vehicle.HasField('trip'): vprec.trip_id = entity.vehicle.trip.trip_id vprec.start_date = entity.vehicle.trip.start_date vprec.schedule_relationship = entity.vehicle.trip.schedule_relationship if entity.vehicle.HasField('position'): vprec.latitude = entity.vehicle.position.latitude vprec.longitude = entity.vehicle.position.longitude vprec.current_stop_sequence = entity.vehicle.current_stop_sequence vprec.data_timestamp = entity.vehicle.timestamp vprec.fetch_timestamp = fetch_timestamp vprec.save() print print "%s - %s\n" % (datetime.datetime.fromtimestamp(fetch_timestamp), i)
def poll_mbta_vehicle_positions(): fetch_timestamp = int(time.time()) # fetch feed from remote server fp = urlopen( FEED_URL ) gtfsrt_message = fp.read() # parse it using protocol buffers pp = grt.FeedMessage() pp.ParseFromString( gtfsrt_message ) try: info = Info.objects.all()[0] if info.buspos_timestamp is not None and pp.header.timestamp <= info.buspos_timestamp: self.stdout.write( "nothing to do - already have the latest data\n" ) return except IndexError: info = Info(buspos_timestamp = pp.header.timestamp) ret = [] print "new timestamp %s > %s old timestamp"%(pp.header.timestamp, info.timestamp) info.buspos_timestamp = pp.header.timestamp info.save() print pp.header for i, entity in enumerate( pp.entity ): print ".", vprec = VehicleUpdate() if entity.vehicle.HasField('trip'): vprec.trip_id = entity.vehicle.trip.trip_id vprec.start_date = entity.vehicle.trip.start_date vprec.schedule_relationship = entity.vehicle.trip.schedule_relationship if entity.vehicle.HasField('position'): vprec.latitude = entity.vehicle.position.latitude vprec.longitude = entity.vehicle.position.longitude vprec.current_stop_sequence = entity.vehicle.current_stop_sequence vprec.data_timestamp = entity.vehicle.timestamp vprec.fetch_timestamp = fetch_timestamp vprec.save() print print "%s - %s\n"%(datetime.datetime.fromtimestamp( fetch_timestamp), i)