Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const niceParameter = function (parameter) {
switch (parameter) {
case 'OZONE':
return 'o3';
case 'PM2.5':
return 'pm25';
default:
return parameter.toLowerCase();
}
};
// Get the measurements
// Filename should be the current time in UTC like '2016030616' and then
// get the previous hours measurements
const dateString = moment.utc().subtract(1, 'hours').format('YYYYMMDDHH');
const file = `HourlyData/${dateString}.dat`;
const lineToObj = function (line) {
return {
aqsid: line[2],
day: line[0],
hour: line[1],
location: line[3].trim(),
timezoneOffset: Number(line[4]),
parameter: niceParameter(line[5]),
unit: niceUnit(line[6]),
value: Number(line[7]),
attribution: [{name: 'US EPA AirNow', url: 'http://www.airnow.gov/'}, {name: line[8].trim()}],
averagingPeriod: {unit: 'hours', value: 1}
};
};
getObjects(source.url, file, lineToObj, (err, measurements) => {
const getDate = (day, time, offset) => {
// Grab date from page, add time string and convert to date
const dateString = `${day} ${time}`;
// A bit odd looking here based on what we're getting in the files
const utc = moment.utc(dateString, 'MM/DD/YYYY HH:mm');
const local = moment.utc(dateString, 'MM/DD/YYYY HH:mm').utcOffset(offset);
return {utc: utc.toDate(), local: local.format()};
};
const getDate = function (date, timezone) {
// For offset, we're making use of the fact that moment.js picks up first string
// like +02 in the provided url
const mo = moment.utc(date, 'YYYY-MM-DD HH:mm:ss').utcOffset(timezone, true);
return {utc: mo.toDate(), local: mo.format('YYYY-MM-DDTHH:mm:ssZ')};
};
exports.fetchData = function (source, cb) {
// Because we're getting the data async, make a top-level timeout
// to keep things from going on forever. Default to 7 minutes
const timeoutId = setTimeout(() => {
return cb({message: 'Failure to receive data from EEA system.'});
}, (process.env.EEA_GLOBAL_TIMEOUT || 360) * 1000);
// Unsure of how date is exactly handled by the EEA system, the adapter
// applies a generous buffer to the toDate and fromDate
let fromDate = moment.utc().subtract(12, 'hour').format('YYYY-MM-DD+HH[%3A]mm');
let toDate = moment.utc().add(1, 'hour').format('YYYY-MM-DD+HH[%3A]mm');
// Only ask for the pollutants we want
let pollutants = acceptableParameters.map((p) => {
// https://github.com/openaq/openaq-fetch/issues/202
if (p === 'pm25') { p = 'PM2.5'; }
return p.toUpperCase();
});
pollutants = pollutants.join();
let finalUrl = `http://fme.discomap.eea.europa.eu/fmedatastreaming/AirQuality/AirQualityUTDExport.fmw?FromDate=${fromDate}&ToDate=${toDate}&Countrycode=${source.country}&Pollutant=${pollutants}&Format=XML&UserToken=${process.env.EEA_TOKEN}&RunAsync=True`;
request(finalUrl, function (err, res, body) {
if (err || res.statusCode !== 200) {
return cb({message: 'Failure to receive job ID from EEA.'});
}
// Since we're asking for the data asynchronously, keep checking for
const getDate = (day, time, offset) => {
// Grab date from page, add time string and convert to date
const dateString = `${day} ${time}`;
// A bit odd looking here based on what we're getting in the files
const utc = moment.utc(dateString, 'MM/DD/YYYY HH:mm');
const local = moment.utc(dateString, 'MM/DD/YYYY HH:mm').utcOffset(offset);
return {utc: utc.toDate(), local: local.format()};
};
exports.fetchData = function (source, cb) {
// Because we're getting the data async, make a top-level timeout
// to keep things from going on forever. Default to 7 minutes
const timeoutId = setTimeout(() => {
return cb({message: 'Failure to receive data from EEA system.'});
}, (process.env.EEA_GLOBAL_TIMEOUT || 360) * 1000);
// Unsure of how date is exactly handled by the EEA system, the adapter
// applies a generous buffer to the toDate and fromDate
let fromDate = moment.utc().subtract(12, 'hour').format('YYYY-MM-DD+HH[%3A]mm');
let toDate = moment.utc().add(1, 'hour').format('YYYY-MM-DD+HH[%3A]mm');
// Only ask for the pollutants we want
let pollutants = acceptableParameters.map((p) => {
// https://github.com/openaq/openaq-fetch/issues/202
if (p === 'pm25') { p = 'PM2.5'; }
return p.toUpperCase();
});
pollutants = pollutants.join();
let finalUrl = `http://fme.discomap.eea.europa.eu/fmedatastreaming/AirQuality/AirQualityUTDExport.fmw?FromDate=${fromDate}&ToDate=${toDate}&Countrycode=${source.country}&Pollutant=${pollutants}&Format=XML&UserToken=${process.env.EEA_TOKEN}&RunAsync=True`;
request(finalUrl, function (err, res, body) {
if (err || res.statusCode !== 200) {
return cb({message: 'Failure to receive job ID from EEA.'});
}