Skip to content

Instantly share code, notes, and snippets.

@TheMapSmith
Last active March 17, 2024 15:51
Show Gist options
  • Select an option

  • Save TheMapSmith/33277dea01b8ae8140af85c4406ba2a1 to your computer and use it in GitHub Desktop.

Select an option

Save TheMapSmith/33277dea01b8ae8140af85c4406ba2a1 to your computer and use it in GitHub Desktop.

Revisions

  1. TheMapSmith revised this gist Mar 18, 2018. 1 changed file with 23 additions and 18 deletions.
    41 changes: 23 additions & 18 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,6 @@ function getFlights() {
    if (!fs.existsSync(airportFile)) {
    // build the request URL
    var uri = uriSource[0] + code + uriSource[1] + type + uriSource[2] + timestamp + uriSource[3];

    request({
    "method": "GET",
    "uri": uri,
    @@ -136,23 +135,29 @@ function fetchPlayback(flightId, i) {
    // TODO: Add delay tool - try npm bottleneck https://stackoverflow.com/questions/43715068/how-to-use-the-bottleneck-npm-module

    request({
    "method": "GET",
    "uri": uri,
    "headers": {
    "User-Agent": "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6",
    "origin": "https://www.flightradar24.com",
    "referer": "https://www.flightradar24.com/data/flights/ORD"
    }
    }).then(function write(response) {
    fs.writeFile(filename, response, 'utf8')
    // make an array object out of the current flight info and push into a
    // master array that will be processed all at once
    flightObj.file = filename
    flightObj.id = flightId
    allPlaybacks.push(flightObj)
    // drop a FlightId from the array to whittle down the call stack
    FlightIds.pop() // drop the final one
    })
    "method": "GET",
    "uri": uri,
    "headers": {
    "User-Agent": "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6",
    "origin": "https://www.flightradar24.com",
    "referer": "https://www.flightradar24.com/data/flights/ORD"
    }
    })
    .then(function write(response) {
    if (!error && response.statusCode == 200) {
    fs.writeFile(filename, response, 'utf8')
    // make an array object out of the current flight info and push into a
    // master array that will be processed all at once
    flightObj.file = filename
    flightObj.id = flightId
    allPlaybacks.push(flightObj)
    // drop a FlightId from the array to whittle down the call stack
    FlightIds.pop() // drop the final one
    }
    })
    .catch(function(err) {
    console.log("Couldn't fetch " + filename + ",code: " + err.statusCode)
    })

    } else {
    // make an array object out of the current flight info and push into a
  2. TheMapSmith revised this gist Mar 18, 2018. 1 changed file with 32 additions and 29 deletions.
    61 changes: 32 additions & 29 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -8,23 +8,20 @@ global.allPlaybacks = [];
    global.geojson = {};
    global.geojson['type'] = 'FeatureCollection';
    global.geojson['features'] = [];

    var code = "ORD"
    var types = ["arrivals", "departures"]
    var FlightIds = [] // array for all IDs found
    var folder = code + "/"
    global.code = "DFW"
    global.folder = code + "/"
    global.types = ["arrivals", "departures"]
    global.FlightIds = [] // array for all IDs found

    // first call
    makeFolder(code, folder);
    makeFolder(getFlights);

    function makeFolder(code, folder) {
    function makeFolder(callback) {
    fs.stat(code, function(err, stats) {
    if (err) {
    // Directory doesn't exist or something.
    console.log('Folder doesn\'t exist, so I made the folder ' + code);
    return fs.mkdir(folder);
    getFlights(code, types);
    // TODO: Actually move on with the code when making new folder
    return fs.mkdir(folder, callback);
    }
    if (!stats.isDirectory()) {
    // This isn't a directory!
    @@ -36,7 +33,7 @@ function makeFolder(code, folder) {
    });
    }

    function getFlights(code, types) {
    function getFlights() {
    // API URL string slugs
    var uriSource = [
    'https://api.flightradar24.com/common/v1/airport.json?code=',
    @@ -56,19 +53,25 @@ function getFlights(code, types) {
    var uri = uriSource[0] + code + uriSource[1] + type + uriSource[2] + timestamp + uriSource[3];

    request({
    "method": "GET",
    "uri": uri,
    "headers": {
    "User-Agent": "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6",
    "origin": "https://www.flightradar24.com",
    "referer": "https://www.flightradar24.com/data/flights/ORD"
    }
    }).then(function write(response) {
    var filename = code + "_" + type + ".json";
    fs.writeFile(folder + filename, response, 'utf8')
    console.log("Successfully fetched " + filename);
    // TODO: Actually move on with the program from here
    })
    "method": "GET",
    "uri": uri,
    "headers": {
    "User-Agent": "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6",
    "origin": "https://www.flightradar24.com",
    "referer": "https://www.flightradar24.com/data/flights/ORD"
    }
    })
    .then(function write(response, error) {
    if (!error && response.statusCode == 200) {
    var filename = code + "_" + type + ".json";
    fs.writeFile(folder + filename, response, 'utf8', getIds)
    console.log("Successfully fetched " + filename);
    // TODO: Actually move on with the program from here
    }
    })
    .catch(function(err) {
    console.log("Couldn't fetch " + airportFile + ",code: " + err.statusCode)
    })
    } else if (type === "departures") {
    // only call next function when done with the types array
    getIds(airportFile, types, FlightIds, allPlaybacks);
    @@ -79,7 +82,7 @@ function getFlights(code, types) {
    }

    // read the Airport file to extract all flightIds
    function getIds(airportFile, types, FlightIds) {
    function getIds(airportFile) {
    types.forEach(function(type) {
    var airportFile = folder + code + "_" + type + ".json"
    fs.readFile(airportFile, 'utf8', function(err, data) {
    @@ -105,14 +108,14 @@ function getIds(airportFile, types, FlightIds) {
    })
    };

    function processFlightIds(i, FlightIds) {
    function processFlightIds(i) {
    console.log("breakpoint");
    for (var i = FlightIds.length - 1; i >= 0; i--) {
    // drop FlightIds[i] so that i have a call stack based on each flight id
    fetchPlayback(FlightIds[i], i);
    }
    if (FlightIds.length === 0) {
    processTracks(allPlaybacks, writeGeoJSON)
    processTracks(writeGeoJSON)
    }
    };

    @@ -164,7 +167,7 @@ function fetchPlayback(flightId, i) {


    // take a Flight Playback json and turn the coordinates into a GeoJSON line
    function processTracks(allPlaybacks, callback) {
    function processTracks(callback) {
    // read all the files
    console.log("huh")
    for (var i = 0; i < allPlaybacks.length; i++) {
    @@ -203,7 +206,7 @@ function processTracks(allPlaybacks, callback) {
    }
    };

    function writeGeoJSON(geojson) {
    function writeGeoJSON() {
    var filename = code + '.geoJSON'
    fs.writeFile(filename, JSON.stringify(geojson), 'utf8');
    }
  3. TheMapSmith revised this gist Mar 18, 2018. 1 changed file with 46 additions and 37 deletions.
    83 changes: 46 additions & 37 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    var fs = require('fs');
    var request = require('request-promise');
    var delayed = require('delayed');
    var timestamp // TODO: Add the current timestamp in ms
    var moment = require('moment')

    // Globals
    global.timestamp = moment().unix()
    global.allPlaybacks = [];
    global.geojson = {};
    global.geojson['type'] = 'FeatureCollection';
    global.geojson['features'] = [];

    var code = "btv"
    var code = "ORD"
    var types = ["arrivals", "departures"]
    var FlightIds = [] // array for all IDs found
    var folder = code + "/"
    @@ -22,8 +22,9 @@ function makeFolder(code, folder) {
    if (err) {
    // Directory doesn't exist or something.
    console.log('Folder doesn\'t exist, so I made the folder ' + code);
    return fs.mkdir(folder, callback);
    return fs.mkdir(folder);
    getFlights(code, types);
    // TODO: Actually move on with the code when making new folder
    }
    if (!stats.isDirectory()) {
    // This isn't a directory!
    @@ -40,7 +41,8 @@ function getFlights(code, types) {
    var uriSource = [
    'https://api.flightradar24.com/common/v1/airport.json?code=',
    '&plugin[]=&plugin-setting[schedule][mode]=',
    '&plugin-setting[schedule][timestamp]=1520910587&page=-1&limit=100&token='
    '&plugin-setting[schedule][timestamp]=',
    '&page=-1&limit=100&token='
    ]

    // check if we have a file
    @@ -51,19 +53,21 @@ function getFlights(code, types) {
    // if we don't have an airport file yet
    if (!fs.existsSync(airportFile)) {
    // build the request URL
    var uri = uriSource[0] + code + uriSource[1] + type + uriSource[2];
    var uri = uriSource[0] + code + uriSource[1] + type + uriSource[2] + timestamp + uriSource[3];

    request({
    "method": "GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
    "referer": "https://www.flightradar24.com/data/flights/"
    "User-Agent": "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6",
    "origin": "https://www.flightradar24.com",
    "referer": "https://www.flightradar24.com/data/flights/ORD"
    }
    }).then(function write(response) {
    var filename = code + "_" + type + ".json";
    fs.writeFile(folder + filename, response, 'utf8')
    console.log("Successfully fetched " + filename);
    // TODO: Actually move on with the program from here
    })
    } else if (type === "departures") {
    // only call next function when done with the types array
    @@ -76,9 +80,6 @@ function getFlights(code, types) {

    // read the Airport file to extract all flightIds
    function getIds(airportFile, types, FlightIds) {
    // TODO: need to iterate through both arrivals and departures files to push
    // TODO: each ID into the flightid array call stack

    types.forEach(function(type) {
    var airportFile = folder + code + "_" + type + ".json"
    fs.readFile(airportFile, 'utf8', function(err, data) {
    @@ -111,7 +112,7 @@ function processFlightIds(i, FlightIds) {
    fetchPlayback(FlightIds[i], i);
    }
    if (FlightIds.length === 0) {
    processTracks(allPlaybacks)
    processTracks(allPlaybacks, writeGeoJSON)
    }
    };

    @@ -121,20 +122,23 @@ function fetchPlayback(flightId, i) {
    console.log("getting " + flightId);
    var uriSource = [
    'https://api.flightradar24.com/common/v1/flight-playback.json?flightId=',
    '&timestamp=1520806800&token='
    '&timestamp=',
    '&token='
    ]
    var filename = folder + "flight-playback-" + flightId + ".json";

    if (!fs.existsSync(filename)) {
    var uri = uriSource[0] + flightId + uriSource[1]
    var uri = uriSource[0] + flightId + uriSource[1] + timestamp + uriSource[2]
    // TODO: Catch request errors
    // TODO: Add delay tool - try npm bottleneck https://stackoverflow.com/questions/43715068/how-to-use-the-bottleneck-npm-module

    request({
    "method": "GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    "User-Agent": "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6",
    "origin": "https://www.flightradar24.com",
    "referer": "https://www.flightradar24.com/data/flights/ORD"
    }
    }).then(function write(response) {
    fs.writeFile(filename, response, 'utf8')
    @@ -160,10 +164,13 @@ function fetchPlayback(flightId, i) {


    // take a Flight Playback json and turn the coordinates into a GeoJSON line
    function processTracks(allPlaybacks) {
    function processTracks(allPlaybacks, callback) {
    // read all the files
    console.log("huh")
    for (var i = 0; i < allPlaybacks.length; i++) {
    if (i === allPlaybacks.length) {
    callback(geojson)
    }
    var onePlayback = allPlaybacks[i];
    fs.readFile(onePlayback.file, 'utf8', function(err, data) {
    if (err) throw err;
    @@ -173,28 +180,30 @@ function processTracks(allPlaybacks) {

    var coordinates = []

    track.forEach(function(e) {
    var pair = []
    pair.push(e.longitude)
    pair.push(e.latitude)
    coordinates.push(pair)
    })
    if (track) {
    track.forEach(function(e) {
    var pair = []
    pair.push(e.longitude)
    pair.push(e.latitude)
    coordinates.push(pair)
    })

    var feature = {
    "type": "Feature",
    "geometry": {
    "type": "LineString",
    "coordinates": coordinates
    },
    "properties": {}
    }
    geojson['features'].push(feature);

    var feature = {
    "type": "Feature",
    "geometry": {
    "type": "LineString",
    "coordinates": coordinates
    },
    "properties": {}
    }
    geojson['features'].push(feature);
    })

    if (i === allPlaybacks.length-1) {
    var filename = code + '.geoJSON'
    console.log(filename);
    fs.writeFile(filename, JSON.stringify(geojson), 'utf8');
    }
    }
    }; ///end function processTrack
    };

    function writeGeoJSON(geojson) {
    var filename = code + '.geoJSON'
    fs.writeFile(filename, JSON.stringify(geojson), 'utf8');
    }
  4. TheMapSmith revised this gist Mar 17, 2018. 1 changed file with 41 additions and 44 deletions.
    85 changes: 41 additions & 44 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,41 @@
    var fs = require('fs');
    var request = require('request-promise');
    var delayed = require('delayed');
    var timestamp // TODO: Add the current timestamp in ms

    // Globals
    var allPlaybacks = [];
    global.allPlaybacks = [];
    global.geojson = {};
    global.geojson['type'] = 'FeatureCollection';
    global.geojson['features'] = [];

    var code = "btv"
    var types = ["arrivals", "departures"]
    var FlightIds = [] // array for all IDs found
    var folder = code + "/"

    // first call
    makeFolder(code, folder, allPlaybacks);
    makeFolder(code, folder);

    function makeFolder(code, folder, allPlaybacks) {
    function makeFolder(code, folder) {
    fs.stat(code, function(err, stats) {
    if (err) {
    // Directory doesn't exist or something.
    console.log('Folder doesn\'t exist, so I made the folder ' + code);
    return fs.mkdir(folder, callback);
    getFlights(code, types);
    }
    if (!stats.isDirectory()) {
    // This isn't a directory!
    callback(new Error('temp is not a directory!'));
    } else {
    console.log('Folder ' + code + ' exists');
    getFlights(code, types, allPlaybacks);
    getFlights(code, types);
    }
    });
    }

    function getFlights(code, types, allPlaybacks) {
    function getFlights(code, types) {
    // API URL string slugs
    var uriSource = [
    'https://api.flightradar24.com/common/v1/airport.json?code=',
    @@ -40,7 +46,7 @@ function getFlights(code, types, allPlaybacks) {
    // check if we have a file
    types.forEach(function(type) {

    var airportFile = folder + "/" + code + "_" + type + ".json"
    var airportFile = folder + code + "_" + type + ".json"

    // if we don't have an airport file yet
    if (!fs.existsSync(airportFile)) {
    @@ -62,16 +68,19 @@ function getFlights(code, types, allPlaybacks) {
    } else if (type === "departures") {
    // only call next function when done with the types array
    getIds(airportFile, types, FlightIds, allPlaybacks);
    } else if (type === "arrivals") {
    console.log("arrivals")
    }
    })
    }

    // read the Airport file to extract all flightIds
    function getIds(airportFile, types, FlightIds, allPlaybacks) {
    function getIds(airportFile, types, FlightIds) {
    // TODO: need to iterate through both arrivals and departures files to push
    // TODO: each ID into the flightid array call stack

    types.forEach(function(type) {
    var airportFile = folder + "/" + code + "_" + type + ".json"
    var airportFile = folder + code + "_" + type + ".json"
    fs.readFile(airportFile, 'utf8', function(err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);
    @@ -83,36 +92,43 @@ function getIds(airportFile, types, FlightIds, allPlaybacks) {
    var id = e.flight.identification.id
    if (id) {
    FlightIds.push(id)
    console.log(id);
    }
    })
    console.log(type + " ids: " + FlightIds.length);
    for (var i = FlightIds.length; i >= 0; i--) {
    // drop FlightIds[i] so that i have a call stack based on each flight id
    fetchPlayback(FlightIds[i], i, type, allPlaybacks);
    if (type === "departures") {
    var i
    processFlightIds(i, FlightIds);
    }
    // if (typeof callback === "function") {
    // // callback(<<THE ARRAY OF IDS>>);
    // }
    })
    })
    };

    // fetch the playback files for each flightId
    function fetchPlayback(flightId, i, type, allPlaybacks) {
    function processFlightIds(i, FlightIds) {
    console.log("breakpoint");
    for (var i = FlightIds.length - 1; i >= 0; i--) {
    // drop FlightIds[i] so that i have a call stack based on each flight id
    fetchPlayback(FlightIds[i], i);
    }
    if (FlightIds.length === 0) {
    processTracks(allPlaybacks)
    }
    };

    // fetch the playback files for each flightId
    function fetchPlayback(flightId, i) {
    var flightObj = {}

    console.log("getting " + type + " " + flightId);

    console.log("getting " + flightId);
    var uriSource = [
    'https://api.flightradar24.com/common/v1/flight-playback.json?flightId=',
    '&timestamp=1520806800&token='
    ]

    var filename = folder + "flight-playback-" + flightId + ".json";

    if (!fs.existsSync(filename)) {
    var uri = uriSource[0] + flightId + uriSource[1]
    // TODO: Catch request errors
    // TODO: Add delay tool - try npm bottleneck https://stackoverflow.com/questions/43715068/how-to-use-the-bottleneck-npm-module

    request({
    "method": "GET",
    @@ -128,14 +144,7 @@ function fetchPlayback(flightId, i, type, allPlaybacks) {
    flightObj.id = flightId
    allPlaybacks.push(flightObj)
    // drop a FlightId from the array to whittle down the call stack
    FlightIds.splice(i, 1) // drop the final one

    if (FlightIds.length === 0) {
    allPlaybacks.forEach(function(e) {
    processTracks(allPlaybacks)
    });
    }

    FlightIds.pop() // drop the final one
    })

    } else {
    @@ -145,26 +154,16 @@ function fetchPlayback(flightId, i, type, allPlaybacks) {
    flightObj.id = flightId
    allPlaybacks.push(flightObj)
    // drop a FlightId from the array to whittle down the call stack
    FlightIds.splice(i, 1) // drop the final one

    if (FlightIds.length === 0) {
    allPlaybacks.forEach(function(e) {
    processTracks(allPlaybacks)
    });
    }

    FlightIds.pop() // drop the final one
    }
    }


    // take a Flight Playback json and turn the coordinates into a GeoJSON line
    function processTracks(allPlaybacks) {
    var geojson = {};
    geojson['type'] = 'FeatureCollection';
    geojson['features'] = [];

    // read all the files
    for (var i = allPlaybacks.length; i <= 0; i--) {
    console.log("huh")
    for (var i = 0; i < allPlaybacks.length; i++) {
    var onePlayback = allPlaybacks[i];
    fs.readFile(onePlayback.file, 'utf8', function(err, data) {
    if (err) throw err;
    @@ -192,12 +191,10 @@ function processTracks(allPlaybacks) {
    geojson['features'].push(feature);
    })

    if (i === 0) {
    if (i === allPlaybacks.length-1) {
    var filename = code + '.geoJSON'
    console.log(filename);
    fs.writeFile(filename, JSON.stringify(geojson), 'utf8');
    }
    }


    }; ///end function processTrack
  5. TheMapSmith revised this gist Mar 16, 2018. 1 changed file with 29 additions and 22 deletions.
    51 changes: 29 additions & 22 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -59,35 +59,41 @@ function getFlights(code, types, allPlaybacks) {
    fs.writeFile(folder + filename, response, 'utf8')
    console.log("Successfully fetched " + filename);
    })
    } else {
    getIds(airportFile, type, FlightIds, allPlaybacks);
    } else if (type === "departures") {
    // only call next function when done with the types array
    getIds(airportFile, types, FlightIds, allPlaybacks);
    }
    })
    }

    // read the Airport file to extract all flightIds
    function getIds(airportFile, type, FlightIds, allPlaybacks) {
    fs.readFile(airportFile, 'utf8', function(err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);

    var schedule = obj.result.response.airport.pluginData.schedule
    var data = schedule[type].data;

    data.forEach(function(e) {
    var id = e.flight.identification.id
    if (id) {
    FlightIds.push(id)
    function getIds(airportFile, types, FlightIds, allPlaybacks) {
    // TODO: need to iterate through both arrivals and departures files to push
    // TODO: each ID into the flightid array call stack
    types.forEach(function(type) {
    var airportFile = folder + "/" + code + "_" + type + ".json"
    fs.readFile(airportFile, 'utf8', function(err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);

    var schedule = obj.result.response.airport.pluginData.schedule
    var data = schedule[type].data;

    data.forEach(function(e) {
    var id = e.flight.identification.id
    if (id) {
    FlightIds.push(id)
    }
    })
    console.log(type + " ids: " + FlightIds.length);
    for (var i = FlightIds.length; i >= 0; i--) {
    // drop FlightIds[i] so that i have a call stack based on each flight id
    fetchPlayback(FlightIds[i], i, type, allPlaybacks);
    }
    // if (typeof callback === "function") {
    // // callback(<<THE ARRAY OF IDS>>);
    // }
    })
    console.log(type + " ids: " + FlightIds.length);
    for (var i = FlightIds.length; i >= 0; i--) {
    // drop FlightIds[i] so that i have a call stack based on each flight id
    fetchPlayback(FlightIds[i], i, type, allPlaybacks);
    }
    // if (typeof callback === "function") {
    // // callback(<<THE ARRAY OF IDS>>);
    // }
    })
    };

    @@ -159,6 +165,7 @@ function processTracks(allPlaybacks) {

    // read all the files
    for (var i = allPlaybacks.length; i <= 0; i--) {
    var onePlayback = allPlaybacks[i];
    fs.readFile(onePlayback.file, 'utf8', function(err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);
  6. TheMapSmith revised this gist Mar 16, 2018. 1 changed file with 104 additions and 84 deletions.
    188 changes: 104 additions & 84 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -2,31 +2,34 @@ var fs = require('fs');
    var request = require('request-promise');
    var delayed = require('delayed');

    // Globals
    var allPlaybacks = [];
    var code = "btv"
    var types = ["arrivals", "departures"]
    var FlightIds = [] // array for all IDs found
    var folder = code + "/"

    makeFolder(code, folder);
    // first call
    makeFolder(code, folder, allPlaybacks);

    function makeFolder(code, folder) {
    fs.stat(code, function (err, stats){
    if (err) {
    // Directory doesn't exist or something.
    console.log('Folder doesn\'t exist, so I made the folder ' + code);
    return fs.mkdir(folder, callback);
    }
    if (!stats.isDirectory()) {
    // This isn't a directory!
    callback(new Error('temp is not a directory!'));
    } else {
    console.log('Folder '+ code + ' exists');
    getFlights(code,types);
    }
    });
    function makeFolder(code, folder, allPlaybacks) {
    fs.stat(code, function(err, stats) {
    if (err) {
    // Directory doesn't exist or something.
    console.log('Folder doesn\'t exist, so I made the folder ' + code);
    return fs.mkdir(folder, callback);
    }
    if (!stats.isDirectory()) {
    // This isn't a directory!
    callback(new Error('temp is not a directory!'));
    } else {
    console.log('Folder ' + code + ' exists');
    getFlights(code, types, allPlaybacks);
    }
    });
    }

    function getFlights(code,types){
    function getFlights(code, types, allPlaybacks) {
    // API URL string slugs
    var uriSource = [
    'https://api.flightradar24.com/common/v1/airport.json?code=',
    @@ -35,7 +38,7 @@ function getFlights(code,types){
    ]

    // check if we have a file
    types.forEach(function(type){
    types.forEach(function(type) {

    var airportFile = folder + "/" + code + "_" + type + ".json"

    @@ -45,59 +48,56 @@ function getFlights(code,types){
    var uri = uriSource[0] + code + uriSource[1] + type + uriSource[2];

    request({
    "method":"GET",
    "method": "GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
    "referer": "https://www.flightradar24.com/data/flights/"
    }
    }).then(function write(response){
    }).then(function write(response) {
    var filename = code + "_" + type + ".json";
    fs.writeFile(folder + filename, response,'utf8')
    fs.writeFile(folder + filename, response, 'utf8')
    console.log("Successfully fetched " + filename);
    })
    }
    else {
    getIds(airportFile,type,FlightIds);
    } else {
    getIds(airportFile, type, FlightIds, allPlaybacks);
    }
    })
    }

    // read the Airport file to extract all flightIds
    function getIds(airportFile,type,FlightIds){
    fs.readFile(airportFile, 'utf8', function (err, data) {
    function getIds(airportFile, type, FlightIds, allPlaybacks) {
    fs.readFile(airportFile, 'utf8', function(err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);

    var schedule = obj.result.response.airport.pluginData.schedule
    var data = schedule[type].data;

    data.forEach(function(e){
    data.forEach(function(e) {
    var id = e.flight.identification.id
    if (id) {
    FlightIds.push(id)
    }
    })
    console.log(type + " ids: " + FlightIds.length);
    for (var i = 0; i < 4; i++) {
    var randomDelay = Math.floor(Math.random() * 1000) + 1000
    console.log("Waiting " + randomDelay + " to get id " + FlightIds[i]);

    delayed.delay(fetchPlayback(FlightIds[i], type), randomDelay);
    for (var i = FlightIds.length; i >= 0; i--) {
    // drop FlightIds[i] so that i have a call stack based on each flight id
    fetchPlayback(FlightIds[i], i, type, allPlaybacks);
    }
    console.log("done with lop");
    console.log("hot swap")
    // if (typeof callback === "function") {
    // // callback(<<THE ARRAY OF IDS>>);
    // }
    })
    };

    // fetch the playback files for each flightId
    function fetchPlayback(flightId, type){
    var allPlaybacks = []
    var flightObj = {}
    function fetchPlayback(flightId, i, type, allPlaybacks) {

    var flightObj = {}

    console.log("getting " + type + " " + flightId);

    var uriSource = [
    'https://api.flightradar24.com/common/v1/flight-playback.json?flightId=',
    '&timestamp=1520806800&token='
    @@ -108,69 +108,89 @@ function fetchPlayback(flightId, type){
    if (!fs.existsSync(filename)) {
    var uri = uriSource[0] + flightId + uriSource[1]

    request({
    "method":"GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    }
    }).then(function write(response){
    fs.writeFile(filename, response,'utf8')
    flightObj.file = filename
    flightObj.id = flightId
    allPlaybacks.push(flightObj)
    })
    request({
    "method": "GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    }
    }).then(function write(response) {
    fs.writeFile(filename, response, 'utf8')
    // make an array object out of the current flight info and push into a
    // master array that will be processed all at once
    flightObj.file = filename
    flightObj.id = flightId
    allPlaybacks.push(flightObj)
    // drop a FlightId from the array to whittle down the call stack
    FlightIds.splice(i, 1) // drop the final one

    if (FlightIds.length === 0) {
    allPlaybacks.forEach(function(e) {
    processTracks(allPlaybacks)
    });
    }

    }
    else {
    })

    } else {
    // make an array object out of the current flight info and push into a
    // master array that will be processed all at once
    flightObj.file = filename
    flightObj.id = flightId
    allPlaybacks.push(flightObj)
    // drop a FlightId from the array to whittle down the call stack
    FlightIds.splice(i, 1) // drop the final one

    console.log(filename + " exists");

    allPlaybacks.forEach(function(e){
    processTrack(e.file, e.id)
    });
    if (FlightIds.length === 0) {
    allPlaybacks.forEach(function(e) {
    processTracks(allPlaybacks)
    });
    }


    }
    }


    // take a Flight Playback json and turn the coordinates into a GeoJSON line
    function processTrack(playbackFile,flightID) {
    console.log("thing")
    fs.readFile(playbackFile, 'utf8', function (err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);
    // var flightID = obj.result.request.flightId
    var track = obj.result.response.data.flight.track;

    function processTracks(allPlaybacks) {
    var geojson = {};
    geojson['type'] = 'FeatureCollection';
    geojson['features'] = [];

    var coordinates = []
    // read all the files
    for (var i = allPlaybacks.length; i <= 0; i--) {
    fs.readFile(onePlayback.file, 'utf8', function(err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);
    // var flightID = obj.result.request.flightId
    var track = obj.result.response.data.flight.track;

    var coordinates = []

    track.forEach(function(e) {
    var pair = []
    pair.push(e.longitude)
    pair.push(e.latitude)
    coordinates.push(pair)
    })

    track.forEach(function(e){
    var pair = []
    pair.push(e.longitude)
    pair.push(e.latitude)
    coordinates.push(pair)
    })
    var feature = {
    "type": "Feature",
    "geometry": {
    "type": "LineString",
    "coordinates": coordinates
    },
    "properties": {}
    }
    geojson['features'].push(feature);
    })

    var feature = {
    "type": "Feature",
    "geometry": {
    "type": "LineString",
    "coordinates": coordinates
    },
    "properties": {}
    if (i === 0) {
    var filename = code + '.geoJSON'
    console.log(filename);
    fs.writeFile(filename, JSON.stringify(geojson), 'utf8');
    }
    }
    geojson['features'].push(feature);
    var filename = flightID + '.geoJSON'
    console.log(filename);
    fs.writeFile(filename, JSON.stringify(geojson), 'utf8');

    })}; ///end function processTrack

    }; ///end function processTrack
  7. TheMapSmith revised this gist Mar 15, 2018. 1 changed file with 21 additions and 20 deletions.
    41 changes: 21 additions & 20 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -64,7 +64,7 @@ function getFlights(code,types){
    }

    // read the Airport file to extract all flightIds
    function getIds(airportFile,type,FlightIds, callback){
    function getIds(airportFile,type,FlightIds){
    fs.readFile(airportFile, 'utf8', function (err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);
    @@ -85,15 +85,18 @@ function getIds(airportFile,type,FlightIds, callback){

    delayed.delay(fetchPlayback(FlightIds[i], type), randomDelay);
    }

    if (typeof callback === "function") {
    // callback(<<THE ARRAY OF IDS>>);
    }
    console.log("done with lop");
    console.log("hot swap")
    // if (typeof callback === "function") {
    // // callback(<<THE ARRAY OF IDS>>);
    // }
    })
    };

    // fetch the playback files for each flightId
    function fetchPlayback(flightId, type){
    var allPlaybacks = []
    var flightObj = {}
    console.log("getting " + type + " " + flightId);
    var uriSource = [
    'https://api.flightradar24.com/common/v1/flight-playback.json?flightId=',
    @@ -113,33 +116,31 @@ function fetchPlayback(flightId, type){
    }
    }).then(function write(response){
    fs.writeFile(filename, response,'utf8')
    flightObj.file = filename
    flightObj.id = flightId
    allPlaybacks.push(flightObj)
    })

    }
    else {
    console.log(filename + " exists");
    }
    }
    flightObj.file = filename
    flightObj.id = flightId
    allPlaybacks.push(flightObj)

    var testID = '10b1ca65';
    console.log(filename + " exists");

    var playbackFile = "flight-playback-" + testID + ".json";
    allPlaybacks.forEach(function(e){
    processTrack(e.file, e.id)
    });

    var testPlaybacks = [{
    "file":'flight-playback-10abccf1.json',
    "id":'10abccf1'

    }
    }
    ,{
    "file":'flight-playback-10af4355.json',
    "id":'10af4355'
    }];

    testPlaybacks.forEach(function(e){
    // processTrack(e.file, e.id)
    });

    // take a Flight Playback json and turn the coordinates into a GeoJSON line
    function processTrack(playbackFile,flightID) {
    console.log("thing")
    fs.readFile(playbackFile, 'utf8', function (err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);
  8. TheMapSmith revised this gist Mar 14, 2018. 1 changed file with 83 additions and 54 deletions.
    137 changes: 83 additions & 54 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -1,101 +1,130 @@
    var fs = require('fs');
    var request = require('request-promise');
    var delayed = require('delayed');

    var code = "btv"
    var types = ["arrivals", "departures"]
    var FlightIds = [] // array for all IDs found
    var folder = code + "/"

    function main (){
    // fetch airport info
    types.forEach(function(type){
    var airportFile = code + "_" + type + ".json"
    if (fs.existsSync(airportFile)) {
    getIds(airportFile,type)
    }
    else {
    getFlights(code, type);
    }
    })

    // fetch flight playback
    if (fs.existsSync(playbackFile)) {
    processTrack(playbackFile)
    makeFolder(code, folder);

    function makeFolder(code, folder) {
    fs.stat(code, function (err, stats){
    if (err) {
    // Directory doesn't exist or something.
    console.log('Folder doesn\'t exist, so I made the folder ' + code);
    return fs.mkdir(folder, callback);
    }
    else {
    fetchPlayback(testID)
    if (!stats.isDirectory()) {
    // This isn't a directory!
    callback(new Error('temp is not a directory!'));
    } else {
    console.log('Folder '+ code + ' exists');
    getFlights(code,types);
    }
    });
    }


    function getFlights(code,type){
    function getFlights(code,types){
    // API URL string slugs
    var uriSource = [
    'https://api.flightradar24.com/common/v1/airport.json?code=',
    '&plugin[]=&plugin-setting[schedule][mode]=',
    '&plugin-setting[schedule][timestamp]=1520910587&page=-1&limit=100&token='
    ]
    // build the request URL
    var uri = uriSource[0] + code + uriSource[1] + type + uriSource[2];

    request({
    "method":"GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"

    // check if we have a file
    types.forEach(function(type){

    var airportFile = folder + "/" + code + "_" + type + ".json"

    // if we don't have an airport file yet
    if (!fs.existsSync(airportFile)) {
    // build the request URL
    var uri = uriSource[0] + code + uriSource[1] + type + uriSource[2];

    request({
    "method":"GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
    "referer": "https://www.flightradar24.com/data/flights/"
    }
    }).then(function write(response){
    var filename = code + "_" + type + ".json";
    fs.writeFile(folder + filename, response,'utf8')
    console.log("Successfully fetched " + filename);
    })
    }
    else {
    getIds(airportFile,type,FlightIds);
    }
    }).then(function write(response){
    var filename = code + "_" + type + ".json";
    fs.writeFile(filename, response,'utf8')
    })
    }

    // getIds(airportFile);

    // read the Airport file to extract all flightIds
    function getIds(airportFile,type){
    function getIds(airportFile,type,FlightIds, callback){
    fs.readFile(airportFile, 'utf8', function (err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);

    var schedule = obj.result.response.airport.pluginData.schedule
    var data = schedule[type].data;

    var ids = [];

    data.forEach(function(e){
    var id = e.flight.identification.id
    if (id) {
    ids.push(id)
    FlightIds.push(id)
    }
    })
    console.log(type + " ids: " + ids.length);
    })};

    var testID = '10b1ca65';

    var playbackFile = "flight-playback-" + testID + ".json";
    console.log(type + " ids: " + FlightIds.length);
    for (var i = 0; i < 4; i++) {
    var randomDelay = Math.floor(Math.random() * 1000) + 1000
    console.log("Waiting " + randomDelay + " to get id " + FlightIds[i]);

    delayed.delay(fetchPlayback(FlightIds[i], type), randomDelay);
    }

    function fetchPlayback(flightId){
    if (typeof callback === "function") {
    // callback(<<THE ARRAY OF IDS>>);
    }
    })
    };

    // fetch the playback files for each flightId
    function fetchPlayback(flightId, type){
    console.log("getting " + type + " " + flightId);
    var uriSource = [
    'https://api.flightradar24.com/common/v1/flight-playback.json?flightId=',
    '&timestamp=1520806800&token='
    ]

    var uri = uriSource[0] + flightId + uriSource[1]
    var filename = folder + "flight-playback-" + flightId + ".json";

    request({
    "method":"GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    }
    }).then(function write(response){
    var filename = "flight-playback-" + flightId + ".json";
    fs.writeFile(filename, response,'utf8')
    })
    if (!fs.existsSync(filename)) {
    var uri = uriSource[0] + flightId + uriSource[1]

    request({
    "method":"GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    }
    }).then(function write(response){
    fs.writeFile(filename, response,'utf8')
    })

    }
    else {
    console.log(filename + " exists");
    }
    }

    var testID = '10b1ca65';

    var playbackFile = "flight-playback-" + testID + ".json";

    var testPlaybacks = [{
    "file":'flight-playback-10abccf1.json',
    "id":'10abccf1'
  9. TheMapSmith revised this gist Mar 13, 2018. 1 changed file with 39 additions and 10 deletions.
    49 changes: 39 additions & 10 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -4,16 +4,27 @@ var request = require('request-promise');
    var code = "btv"
    var types = ["arrivals", "departures"]

    types.forEach(function(type){
    var file = code + "_" + type + ".json"

    if (fs.existsSync(file)) {
    getIds(file,type)
    function main (){
    // fetch airport info
    types.forEach(function(type){
    var airportFile = code + "_" + type + ".json"
    if (fs.existsSync(airportFile)) {
    getIds(airportFile,type)
    }
    else {
    getFlights(code, type);
    }
    })

    // fetch flight playback
    if (fs.existsSync(playbackFile)) {
    processTrack(playbackFile)
    }
    else {
    getFlights(code, type);
    fetchPlayback(testID)
    }
    })
    }


    function getFlights(code,type){
    // API URL string slugs
    @@ -59,12 +70,30 @@ function getIds(airportFile,type){
    console.log(type + " ids: " + ids.length);
    })};

    var testID = '10abccf1';
    var testID = '10b1ca65';

    var playbackFile = "flight-playback-" + testID + ".json";


    fetchPlayback(testID)
    function fetchPlayback(flightId){

    var uriSource = [
    'https://api.flightradar24.com/common/v1/flight-playback.json?flightId=',
    '&timestamp=1520806800&token='
    ]

    function fetchPlayback(FlightId){
    var uri = uriSource[0] + flightId + uriSource[1]

    request({
    "method":"GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    }
    }).then(function write(response){
    var filename = "flight-playback-" + flightId + ".json";
    fs.writeFile(filename, response,'utf8')
    })
    }

    var testPlaybacks = [{
  10. TheMapSmith created this gist Mar 13, 2018.
    117 changes: 117 additions & 0 deletions flights.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,117 @@
    var fs = require('fs');
    var request = require('request-promise');

    var code = "btv"
    var types = ["arrivals", "departures"]

    types.forEach(function(type){
    var file = code + "_" + type + ".json"

    if (fs.existsSync(file)) {
    getIds(file,type)
    }
    else {
    getFlights(code, type);
    }
    })

    function getFlights(code,type){
    // API URL string slugs
    var uriSource = [
    'https://api.flightradar24.com/common/v1/airport.json?code=',
    '&plugin[]=&plugin-setting[schedule][mode]=',
    '&plugin-setting[schedule][timestamp]=1520910587&page=-1&limit=100&token='
    ]
    // build the request URL
    var uri = uriSource[0] + code + uriSource[1] + type + uriSource[2];

    request({
    "method":"GET",
    "uri": uri,
    "headers": {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
    }
    }).then(function write(response){
    var filename = code + "_" + type + ".json";
    fs.writeFile(filename, response,'utf8')
    })
    }

    // getIds(airportFile);

    // read the Airport file to extract all flightIds
    function getIds(airportFile,type){
    fs.readFile(airportFile, 'utf8', function (err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);

    var schedule = obj.result.response.airport.pluginData.schedule
    var data = schedule[type].data;

    var ids = [];

    data.forEach(function(e){
    var id = e.flight.identification.id
    if (id) {
    ids.push(id)
    }
    })
    console.log(type + " ids: " + ids.length);
    })};

    var testID = '10abccf1';

    fetchPlayback(testID)

    function fetchPlayback(FlightId){

    }

    var testPlaybacks = [{
    "file":'flight-playback-10abccf1.json',
    "id":'10abccf1'
    }
    ,{
    "file":'flight-playback-10af4355.json',
    "id":'10af4355'
    }];

    testPlaybacks.forEach(function(e){
    // processTrack(e.file, e.id)
    });

    // take a Flight Playback json and turn the coordinates into a GeoJSON line
    function processTrack(playbackFile,flightID) {
    fs.readFile(playbackFile, 'utf8', function (err, data) {
    if (err) throw err;
    var obj = JSON.parse(data);
    // var flightID = obj.result.request.flightId
    var track = obj.result.response.data.flight.track;

    var geojson = {};
    geojson['type'] = 'FeatureCollection';
    geojson['features'] = [];

    var coordinates = []

    track.forEach(function(e){
    var pair = []
    pair.push(e.longitude)
    pair.push(e.latitude)
    coordinates.push(pair)
    })

    var feature = {
    "type": "Feature",
    "geometry": {
    "type": "LineString",
    "coordinates": coordinates
    },
    "properties": {}
    }
    geojson['features'].push(feature);
    var filename = flightID + '.geoJSON'
    console.log(filename);
    fs.writeFile(filename, JSON.stringify(geojson), 'utf8');

    })}; ///end function processTrack
    7 changes: 7 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # INCOMPLETE - FOR EDUCATIONAL PURPOSES ONLY

    I wrote a long twitter thread about developing this Node app, and I thought I'd share the incomplete results

    Thread https://twitter.com/TheMapSmith/status/973269523867623425

    You definitely shouldn't expect this to work for you!