rickyc81

Untitled

May 1st, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const URL = "htgtps://api.ivao.aero/getdata/whazzup/whazzup.txt";
  2. const URL_UNKNOW = "http://www.google.it";
  3. const URL_TEST = "http://159.69.178.177/whazzup.txt";
  4. const URL_TEST_EMPTY = "http://159.69.178.177/whazzup_empty.txt";
  5. const URL_TEST_MALFORMED = "http://159.69.178.177/whazzup_malformed.txt";
  6.  
  7. app.use(helmet());
  8. app.use(cors());
  9.  
  10. //  Ivao Section: !GENERAL, !CLIENTS, !SERVERS, !AIRPORTS
  11.  
  12. // Main page route
  13. app.get('/', (req, res) => {
  14.   res.status(200).send({
  15.     status: 'success',
  16.     code: 200,
  17.     response: {
  18.       message: 'Welcome to ivaoToJson API',
  19.     },
  20.   })
  21. });
  22.  
  23. app.get('/pilots', function(req, res) {
  24.   let dataToSend = [];
  25.  
  26.   fetch(URL)
  27.     .then(response => {
  28.       //console.log(response.body._outBuffer.length)
  29.       if (response.body._outBuffer && response.ok) {
  30.         return response.text();
  31.       } else {
  32.         return data = null;
  33.  
  34.       }
  35.     })
  36.     .then(
  37.       data => {
  38.         //console.log(data)
  39.         if (data !== null) {
  40.           let splitted = data.split('\n');
  41.           let startIndex = splitted.indexOf("!CLIENTS") + 1;
  42.           let endIndex = splitted.indexOf("!AIRPORTS") + 2;
  43.           console.log(startIndex, endIndex)
  44.           if (!startIndex || startIndex === "0") {
  45.             res.send({
  46.               status: 'success',
  47.               code: 200,
  48.               response: {
  49.                 message: "Something wrong, malformed file retrived",
  50.               },
  51.             })
  52.             return
  53.           } else {
  54.             let clients = splitted.slice(startIndex, endIndex);
  55.             clients.forEach(line => {
  56.               let fields = line.split(":");
  57.               let temp = {}
  58.               temp.callsign = fields[0];
  59.               temp.vid = fields[1];
  60.               temp.connectionTime = fields[37];
  61.               temp.softwareName = fields[38];
  62.               temp.softwareVersion = fields[39];
  63.               temp.latitiude = fields[5];
  64.               temp.longtitude = fields[6];
  65.               temp.altitude = fields[7];
  66.               temp.groundSpeed = fields[8];
  67.               temp.heading = fields[45];
  68.               temp.onGround = fields[46];
  69.               temp.squawk = fields[17];
  70.               temp.rating = fields[41];
  71.               dataToSend.push(temp);
  72.             })
  73.             res.status(200).send({
  74.               status: 'success',
  75.               code: 200,
  76.               response: {
  77.                 message: 'fetch data ok',
  78.                 data: dataToSend,
  79.               },
  80.             })
  81.           }
  82.  
  83.         } else {
  84.           res.send({
  85.             status: "success",
  86.             code: 200,
  87.             response: {
  88.               message: "Response ok, but data not found!"
  89.             }
  90.           })
  91.         }
  92.       })
  93.     .catch(error => {
  94.       console.error('Error: ', error)
  95.       res.send({
  96.         status: "fail",
  97.         code: 400,
  98.         response: {
  99.           message: `${error}, check that you typed the url correctly`
  100.         }
  101.       })
  102.     });
  103. });
Add Comment
Please, Sign In to add comment