kulmapaikka-ircbot/plugins/weather.js

186 lines
5.0 KiB
JavaScript
Raw Normal View History

2018-04-29 17:37:07 +03:00
var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
class Weather extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Weather';
this.apikey = config.keys.owm.apiKey;
this.iconCodeToText = {
'01d': 'selkeää',
'02d': 'puolipilvistä',
'03d': 'pilvistä',
'04d': 'pilvipeite rakoilee',
'09d': 'sadekuuroja',
'10d': 'vesisadetta',
'11d': 'ukkostaa',
'13d': 'lumisadetta',
'50d': 'sumua',
'01n': 'selkeää',
'02n': 'puolipilvistä',
'03n': 'pilvistä',
'04n': 'pilvipeite rakoilee',
'09n': 'sadekuuroja',
'10n': 'vesisadetta',
'11n': 'ukkostaa',
'13n': 'lumisadetta',
'50n': 'sumua'
};
}
test(input) {
var res = null;
if (res = input.match(/^\.saa(.*)/i)) {
return true;
}
return false;
}
getTemperature(city, code) {
var language = 'fi';
var urlArgs = {
'q': city,
'lang': language,
'APPID': this.apikey
};
var url = 'http://api.openweathermap.org/data/2.5/weather';
var promise = requestPromise(url, urlArgs);
return promise;
}
getTemperatures(cities, code) {
logger.info('getting temperatures ', cities, code);
if(typeof code === 'undefined') {
code = 'fi';
}
var language = 'fi';
var idList = '';
for(var city in cities) {
idList += cities[city] + ',';
}
idList = idList.substring(0,idList.length - 1);
var urlArgs = {
'id': idList,
'lang': language,
'APPID': this.apikey
};
var url = 'http://api.openweathermap.org/data/2.5/group';
var promise = requestPromise(url, urlArgs);
return promise;
}
getPenaTemperatures(start, stop, limit) {
logger.info('getting pena', start, stop, limit);
var d = new Date();
var urlArgs = {};
if(typeof limit === 'undefined') {
limit = 20;
}
urlArgs.limit = limit;
if(typeof start !== 'undefined' && typeof stop !== 'undefined') {
urlArgs.limit = limit;
urlArgs.start = start;
urlArgs.stop = stop;
}
var url = 'http://masa.dy.fi/api/temperature.php';
var promise = requestPromise(url, urlArgs);
return promise;
}
getTemperatureString(owmCityIds) {
let that = this;
var promise = new Promise(function(resolve, reject) {
Promise.all([
that.getTemperatures(owmCityIds),
that.getPenaTemperatures()
]).then(function(results) {
var resultString = '';
// OWM
results[0].list.forEach(function(data) {
var city = data.name;
var temp = (data.main.temp-273.15).toFixed(1);
var code = data.weather[0].icon;
resultString += '' + city + ' ('+temp+' °C, '+that.iconCodeToText[code]+'), ';
});
if(results[1].length > 0) {
var temp = Number(results[1][0].temp).toFixed(1);
resultString += 'Aura (' + temp + ' °C)';
}
resolve(resultString);
}, (str) => reject(str));
});
return promise;
}
result(input) {
// Different cases
let that = this;
var resultPromise = new Promise(function(resultResolve, resultReject) {
if (input.match(/^\.sää$/i) || input.match(/^\.saa$/i)) {
that.getTemperatureString(that.config.owmCityIds).then(function(str) {
resultResolve('Sää: ' + str);
}, function(str) {
resultReject(str)
});
}
var res = null;
// Weather
if (res = input.match(/^\.s(ää|aa) ([^0-9]+)$/i)) {
//console.log('kaupunki', res[1]);
that.getTemperature(res[2]).then(function(result) {
//console.log(result);
if(result.cod === '404') {
//console.log('Eioo: ' + res[1]);
client.say(to, 'Eioo: ' + res[2]);
} else {
var city = result.name;
var country = result.sys.country;
var temp = (result.main.temp-273.15).toFixed(1);
var code = result.weather[0].icon;
var sunriseDate = new Date(result.sys.sunrise*1000);
var sunsetDate = new Date(result.sys.sunset*1000);
var sunrise = sunriseDate.getHours() + '.' + sunriseDate.getMinutes();
var sunset = sunsetDate.getHours() + '.' + sunsetDate.getMinutes();
var humidity = result.main.humidity;
var pressure = result.main.pressure;
var windspeed = result.wind.speed;
let str = 'Sää: '+city+', '+country+' ('+temp+' °C, '+ that.iconCodeToText[code] +', ilmankosteus: '+humidity+' %, ilmanpaine: '+pressure+' hPa, tuulen nopeus: '+windspeed+' m/s)';
resultResolve('Sää: ' + str);
}
}, function(error) {
logger.error('ERROR ',error);
resultResolve('Oho! Tapahtui virhe. Yritä myöhemmin uudelleen...');
});
}
});
return resultPromise;
}
}
module.exports = Weather;