nws-wrapper
Version:
National Weather Service (US) API Wrapper for JS
94 lines (81 loc) • 2.68 kB
JavaScript
function getPointData(lat, lon) {
const url = `https://api.weather.gov/points/${lat},${lon}`;
return fetch(url)
.then(response => response.json())
.then(data => {
return data.properties;
})
.catch(error => console.error(error));
}
function getGrid(lat, lon) {
return getPointData(lat, lon)
.then(properties => {
return `${properties.gridX},${properties.gridY}`
});
}
function checkWeatherWarnings(latitude, longitude) {;
const apiUrl = `https://api.weather.gov/alerts/active?point=${latitude},${longitude}`;
return fetch(apiUrl)
.then(response => response.json())
.then(data => {
if (data.features.length > 0) {
const alerts = data.features.map(alert => {
const ev = alert.properties.event;
const hl = alert.properties.headline;
const des = alert.properties.description;
const descr = des;
return { descr, hl };
});
return alerts;
} else {
return "No active weather warnings found for this location.";
}
})
.catch(error => console.error(error));
}
function checkKeyword(headline) {
let keywords = ["Watch", "Advisory", "Warning", "Statement", "Emergency", "Tornado", "Hurricane", "Tropical Storm", "Blizzard"];
let presentKeywords = [];
keywords.forEach(keyword => {
if (headline.indexOf(keyword) !== -1) {
presentKeywords.push(keyword);
}
});
if (presentKeywords.length > 0) {
console.log("The string contains " + presentKeywords.join(", ") + ".");
return presentKeywords.join(", ");
} else {
console.log("The string does not contain Watch, Advisory, Warning, or Statement.");
return "NONE";
}
}
function getRadarSingle(office) {
return `https://radar.weather.gov/ridge/standard/${office}_0.gif`
}
function getRadarLoop(office) {
return `https://radar.weather.gov/ridge/standard/${office}_loop.gif`
}
function getNWSOffice(lat, lon) {
const url = `https://api.weather.gov/points/${lat},${lon}`;
return fetch(url)
.then(response => response.json())
.then(data => data.properties.cwa)
.catch(error => console.error(error));
}
function getRadarStation(lat, lon) {
const url = `https://api.weather.gov/points/${lat},${lon}`;
return fetch(url)
.then(response => response.json())
.then(data => data.properties.radarStation)
.catch(error => console.error(error));
}
module.exports = {
getPointData,
getGrid,
checkWeatherWarnings,
checkKeyword,
getRadarSingle,
getRadarLoop,
getNWSOffice,
getRadarStation,
};