Example of javascript function to call GetFlightID

Hi!

I’m working on a project to track landing of several VIPs for an event. I have their departure times and Tail Numbers but I’m not sure how to pass a 2D array into the API.

My code is currently written to call the function FlightInfoEx with only the tail numbers.



function consumeFlightsApi() {
  var identifiers = getFlightIdentifiers();
  var flights = ];
  var airlines = ];
  
  for (var i = 0; i < identifiers.length; i++) {
    var id = identifiers*;
    var flight_data = requestFlightData(id);
    
    if (!flight_data) {
      continue;
    }
    
    var airline_data = requestAirlineFlightData(flight_data.faFlightID);
    
    if (!airline_data) {
      continue;
    }
    
    flights.push(flight_data);
    airlines.push(airline_data);
  }
  
  if (flights.length == 0 && airlines.length == 0) {
    Logger.log("No data received");
    return;
  }
  
  fillFlightDataSheet(flights);
  fillAirlineDataSheet(airlines);
}

function getFlightIdentifiers() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("INFO");
  var range = sheet.getRange("V4:V100");
  var values = range.getValues();
  var identifiers = Array.prototype.concat.apply(], values).filter(function(item) { return item && item.length > 0; });
  return identifiers;
}

function requestFlightData(id) {
  try {
    var data = makeRequest("GET", "FlightInfoEx", { "ident": id, "howMany": 4 });
    var flights = data.FlightInfoExResult.flights;
    
    if (flights && flights.length > 0) {
      return flights[0];
    }
    
    return null;
  } catch(e) {
    Logger.log("Can't request flight data");
    Logger.log(e);
    return null;
  }
}

function requestAirlineFlightData(id) {
  try {
    var data = makeRequest("GET", "AirlineFlightInfo", { "faFlightID": id });
    var airline = data.AirlineFlightInfoResult;    
    return airline;
  } catch(e) {
    Logger.log("Can't request airline flight data");
    Logger.log(e);
    return null;
  }  
}

function makeRequest(method, url, payload) {
  var options = {
    method: method,
    headers: {
      "Authorization" : "Basic " + Utilities.base64Encode(USER + ':' + API_KEY)
    },
    payload: payload
  };
  
  var response = UrlFetchApp.fetch(BASE_URL + url, options);
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  return data;
}

function fillFlightDataSheet(data) {
  var date_properties = { 
    "filed_time": true, 
    "filed_departuretime": true, 
    "actualdeparturetime": true,
    "estimatedarrivaltime": true,
    "actualarrivaltime": true
  };
  
  fillSheet("FLIGHT_DATA", data, date_properties);
}

function fillAirlineDataSheet(data) {
  fillSheet("AIRLINE_DATA", data, {});
}

function fillSheet(sheet_name, data, date_properties) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name);
  var properties = Object.keys(data[0]); // Get the object property names
  var headers = ]; // Array with the columns names
  
  for (var i = 0; i < properties.length; i++) {
    var property = properties*;
    
    // Separate datetime values in a date and time column
    if (date_properties[property] === true) {
      headers.push(property + " - date");
      headers.push(property + " - time");
      continue;
    }
    
    headers.push(property);
  }
  
  // Set property names as headers
  sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
  
  for (var i = 0; i < data.length; i++) {
    var item = data*;
    var values = ];
    
    for (var j = 0; j < properties.length; j++) {
      var property = properties[j];
      var value = item[property];
      
      if (date_properties[property] === true) {
        value = new Date(value * 1000);
        values.push(Utilities.formatDate(value, "EST", "yyyy-MM-dd"));
        values.push(Utilities.formatDate(value, "EST", "HH:mm:ss"));
        continue;
      }
      
      values.push(value);
    }
    
    sheet.getRange(2 + i, 1, 1, values.length).setValues([values]);
  }
  
  // Auto size columns
  for (var i = 0; i < headers.length; i++) {
    sheet.autoResizeColumn(i + 1);
  }
}





I’m not sure what you mean by 2D array, since no FlightXML functions take arrays as arguments.

You generally do not need to use GetFlightID at all, since you can call FlightInfoEx with “ident@timestamp” if you have already obtained the filed departure timestamp from another FlightXML function that doesn’t return faFlightID.

If you did not get the filed departure timestamp from another FlightXML function already, then you should probably just call FlightInfoEx with just the ident and look at the results to find the flight you are interested in.

Thank you! I didn’t realize you could string together the flight number & date like that. Works perfectly.

Now all I need to do is pull departure times for future flights (about 2 weeks out). Is that best done through AirlineFlightSchedules?

Can I just modify the above code to call that or would I need to take a different approach?

If you need flights more than a day in the future, then you will need to use AirlineFlightSchedules. However, you will not be able to get tracking information about any of those flights until they reach closer to the current day.

Is this browser-side Javascript, or server-side Javascript (Node JS, etc)? This is very intensive data processing and will not result in a pleasant user experience if run within the browser.

Would those future flight still have an estimated departure timestamp that i could turn into epoch time and pass in to FlightInfoEx once they are only a day away?

Also, on the above code, I’m having trouble getting more than one result per tail number that I pass in. I have the “howMany” variable set to more than 1 and I tried parseInt() to get it as an integer. What am I missing?

Thanks for your help!

AirlineFlightSchedules returns a structure that includes “departuretime”, which is the integer epoch timestamp of flight’s scheduled departure. You should generally be able to call FlightInfoEx using “ident@departuretime” when it gets approximately 48 hours of the departure for most airlines.

flightxml.flightaware.com/soap/ … duleStruct