Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const URL = "htgtps://api.ivao.aero/getdata/whazzup/whazzup.txt";
- const URL_UNKNOW = "http://www.google.it";
- const URL_TEST = "http://159.69.178.177/whazzup.txt";
- const URL_TEST_EMPTY = "http://159.69.178.177/whazzup_empty.txt";
- const URL_TEST_MALFORMED = "http://159.69.178.177/whazzup_malformed.txt";
- app.use(helmet());
- app.use(cors());
- // Ivao Section: !GENERAL, !CLIENTS, !SERVERS, !AIRPORTS
- // Main page route
- app.get('/', (req, res) => {
- res.status(200).send({
- status: 'success',
- code: 200,
- response: {
- message: 'Welcome to ivaoToJson API',
- },
- })
- });
- app.get('/pilots', function(req, res) {
- let dataToSend = [];
- fetch(URL)
- .then(response => {
- //console.log(response.body._outBuffer.length)
- if (response.body._outBuffer && response.ok) {
- return response.text();
- } else {
- return data = null;
- }
- })
- .then(
- data => {
- //console.log(data)
- if (data !== null) {
- let splitted = data.split('\n');
- let startIndex = splitted.indexOf("!CLIENTS") + 1;
- let endIndex = splitted.indexOf("!AIRPORTS") + 2;
- console.log(startIndex, endIndex)
- if (!startIndex || startIndex === "0") {
- res.send({
- status: 'success',
- code: 200,
- response: {
- message: "Something wrong, malformed file retrived",
- },
- })
- return
- } else {
- let clients = splitted.slice(startIndex, endIndex);
- clients.forEach(line => {
- let fields = line.split(":");
- let temp = {}
- temp.callsign = fields[0];
- temp.vid = fields[1];
- temp.connectionTime = fields[37];
- temp.softwareName = fields[38];
- temp.softwareVersion = fields[39];
- temp.latitiude = fields[5];
- temp.longtitude = fields[6];
- temp.altitude = fields[7];
- temp.groundSpeed = fields[8];
- temp.heading = fields[45];
- temp.onGround = fields[46];
- temp.squawk = fields[17];
- temp.rating = fields[41];
- dataToSend.push(temp);
- })
- res.status(200).send({
- status: 'success',
- code: 200,
- response: {
- message: 'fetch data ok',
- data: dataToSend,
- },
- })
- }
- } else {
- res.send({
- status: "success",
- code: 200,
- response: {
- message: "Response ok, but data not found!"
- }
- })
- }
- })
- .catch(error => {
- console.error('Error: ', error)
- res.send({
- status: "fail",
- code: 400,
- response: {
- message: `${error}, check that you typed the url correctly`
- }
- })
- });
- });
Add Comment
Please, Sign In to add comment