import RSS from "rss"; import axios from "axios"; import fs from 'fs'; const API_URL = "https://aviationweather.gov/api/data/"; const METAR_SERVICE = "metar"; const feedOutput = process.env.METAR_FEED_OUTPUT; const icao = process.env.METAR_ICAO; const rssMetarOptions = { title: icao + " METAR", description: "METAR feed for " + icao + " Airport", feed_url: process.env.METAR_FEED_URL, site_url: process.env.METAR_SITE_URL, generator: "https://git.entropic.pro/Aiden/metar-rss" } const checkInterval = 10 * 1000 * 60; //10 Minute Check Interval let lastMetar; let metarRSS = new RSS(rssMetarOptions); checkMetar(); //Inital check run const metarCheckJob = setInterval(checkMetar, checkInterval); //Run the check every interval /** * Pulls the current metar and adds it to the feed if it is new */ function checkMetar() { console.log("Checking METAR for " + icao); getMetar().then(res => { if (res.data != lastMetar) { console.log("New METAR: " + res.data); newMetar(metarRSS, res.data); writeFeed(metarRSS); lastMetar = res.data; } }); } /** * Adds a new METAR to the given RSS Feed * @param {*} rss - RSS Feed to add the entry to * @param {*} metar - metar data to add */ function newMetar(rss, metar) { let metarSplit = metar.split(" "); let date = new Date(Date.now()); let eventDate = new Date(Date.UTC(date.getFullYear(), date.getMonth(), Number(metarSplit[2].substring(0,2)), Number(metarSplit[2].substring(2,4)), Number(metarSplit[2].substring(4,6)))); rss.item({ title: metarSplit[0] + " " + metarSplit[1] + " " + metarSplit[2], description: metar, url: API_URL + METAR_SERVICE, guid: metarSplit[1] + metarSplit[2], date: eventDate }); } /** * Pulls METAR data from the API * @returns Axios generated GET Request Response */ function getMetar() { let requestURL = API_URL + METAR_SERVICE + "?ids=" + icao; return axios.get(requestURL); } /** * Writes the given RSS feed to the specified file * @param {*} rss - RSS Feed to Write */ function writeFeed(rss) { let xmlFeed = rss.xml({indent: true}) try { fs.writeFileSync(feedOutput, xmlFeed); } catch(err) { console.error(err); } }