BOM stats

node v6.17.1
version: 14.0.1
endpointsharetweet
const osmosis = require("osmosis"); const R = require("ramda"); const endpoint = require("@runkit/runkit/json-endpoint/1.0.0"); endpoint(exports, async function(req) { // let city = req.query.city || 'sydney'; let city = R.defaultTo('sydney', req.query.city); let stations = req.query.stations=='true'; stations = R.isEmpty(req.body)?stations:req.body; let result = await fetch(city, stations); return result; //return JSON.stringify(stations==true); //return stations; }); function fetch(city, getStations) { let state = { sydney: 'nsw', melbourne: 'vic', brisbane: 'qld', perth: 'wa', adelaide: 'sa', hobart: 'tas', canberra: 'act', darwin: 'nt' }; return new Promise(resolve => { osmosis.get(`http://www.bom.gov.au/${state[city.toLowerCase()]}/observations/${city.toLowerCase()}.shtml`) .set({ stn: [`table#t${city.toUpperCase()}>tbody>tr>th.rowleftcolumn@id`], name: [`table#t${city.toUpperCase()}>tbody>tr>th.rowleftcolumn>a`] }) .set({ keys: [`table#t${city.toUpperCase()}>tbody>tr>th~td@headers`], values: [`table#t${city.toUpperCase()}>tbody>tr>th~td`] }) .set({ header: [`table#t${city.toUpperCase()}>thead>tr>th@id`], short: [`table#t${city.toUpperCase()}>thead>tr>th`] }) .data(d => { let trimStation = R.compose(R.join('-'), R.slice(2,Infinity), R.split('-')); let trimMetric = R.compose(R.join('-'), R.slice(1,Infinity), R.split('-')); let stations = R.zipObj(R.map(trimStation, d.stn), d.name); let stats = R.toPairs(R.zipObj(d.keys, d.values)); let headers = R.zipObj(R.map(trimMetric, d.header), d.short); let focus = R.keys(stations); let result = {}; const proc = e => { let keys = R.split(' ', e[0]); let station = trimStation(R.last(keys)); let metric = trimMetric(R.last(R.init(keys))); if(R.contains(station, focus)) { // result[station] = R.merge(result[station], R.objOf(metric, e[1])); result[metric] = R.merge(result[metric], R.objOf(station, e[1])); } } if(getStations && R.is(Boolean, getStations)) { result = stations; //result = headers; } else if(R.is(Array, getStations)){ focus = getStations; R.forEach(proc, stats); } else if(getStations && R.is(Object, getStations)){ focus = R.keys(getStations); R.forEach(proc, stats); } else { R.forEach(proc, stats); } resolve(result); }); }); }; //console.log(await fetch('sydney', ["canterbury"]));
Loading…

no comments

    sign in to comment