jubilee/server/weather.js

74 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-03-01 23:45:04 +00:00
const Client = require('request-json');
2018-02-23 10:36:49 +00:00
const logger = require('log4js').getLogger('Weather');
const weather = require('openweather-apis');
2018-03-07 00:02:22 +00:00
const { reduceWeather } = require('./reducers/weather');
2018-02-23 10:36:49 +00:00
logger.level = 'debug';
2018-02-23 11:01:20 +00:00
const openWeatherApiKey = process.env.openweatherAPI || '936a0ed9eb23b95cf08fc9f693c24264';
2018-02-23 10:36:49 +00:00
2018-03-01 23:45:04 +00:00
const darkskyApiKey = process.env.darkskyApiKey || '9ad2a41d420f3cf4960571bb886f710c';
const DSclient = Client.createClient(`https://api.darksky.net/forecast/${ darkskyApiKey }/`);
2018-02-23 10:36:49 +00:00
weather.setAPPID(openWeatherApiKey);
weather.setLang('en');
// weather.setCity('Glasgow City');
function doGetOpenWeather(ll) {
const [lat, long ] = ll.split(',');
return new Promise((resolve, reject) => {
weather.setCoordinate(lat, long);
weather.getWeatherForecast( function(err, wData) {
if (err)
return reject(err);
else
return resolve(wData);
});
});
}
function doGetOpenWeatherForecast(ll) {
const [lat, long ] = ll.split(',');
return new Promise((resolve, reject) => {
weather.setCoordinate(lat, long);
weather.getWeatherForecastForDays(5, function(err, wData) {
if (err)
return reject(err);
else
return resolve(wData);
});
});
}
2018-03-01 23:45:04 +00:00
function doGetDarkSkyWeather(ll) {
const query = `${ll}?units=uk2&exclude=daily,flags,minutely,hourly`;
2018-03-07 00:02:22 +00:00
2018-03-01 23:45:04 +00:00
return new Promise((resolve, reject) => {
DSclient.get(query, function(err, res, body) {
if (err || !body || !body.currently)
return reject(err);
return resolve(body);
});
});
}
2018-02-23 10:36:49 +00:00
2018-03-07 00:02:22 +00:00
function doGetFullForcast(ll) {
const query = `${ll}?units=uk2&exclude=flags,minutely`;
return new Promise((resolve, reject) => {
DSclient.get(query, function(err, res, body) {
if (err || !body || !body.currently)
return reject(err);
const output = reduceWeather(body);
return resolve(output);
});
});
}
module.exports = { doGetOpenWeather, doGetOpenWeatherForecast, doGetDarkSkyWeather, doGetFullForcast };