Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
parsedWeatherData.js
const fs = require('fs');
const axios = require('axios');
const xml2js = require('xml2js-es6-promise');
let weatherData;
const extraDataMap = (status, data) => {
return {
status: status,
time: data.$.time,
coordinates: [
+data.location[0].longitude[0].$.value,
+data.location[0].latitude[0].$.value,
],
radius: +data.intensity_structure[0].structure[0].wind_distribution[0].dir[0].radius[0].$.value,
};
};
const getAndParse = async function() {
const apiResult = await axios('http://www.somehost/weather_data.xml');
weatherData = await xml2js(apiResult.data);
const typhinfo = weatherData.cwbtyphfcst.typhinfo;
const typhoonData = typhinfo[0].typhoon[0].typhdata[0];
let pastData = typhoonData.past[0].point;
let currData = typhoonData.curr[0].point;
let fcstData = typhoonData.fcst[0].point;
pastData = pastData.map(extraDataMap.bind(this, 'past'));
currData = currData.map(extraDataMap.bind(this, 'curr'));
fcstData = fcstData.map(extraDataMap.bind(this, 'fcst'));
const parsedWeatherData = {
pastData,
currData,
fcstData,
};
console.log('parsedWeatherData', parsedWeatherData);
fs.writeFile('./parsedWeatherData.json', JSON.stringify(parsedWeatherData, 0, 4), (err) => err && console.log('err', err));
};
getAndParse();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment