Re-enable PenaTemperatures and improve reliability

This commit is contained in:
lanxu 2022-05-07 15:29:37 +03:00
parent 3a38d8e589
commit 373c883027

View File

@ -4,7 +4,7 @@ var BasePlugin = require('./base.js')
const logger = require('../logger') const logger = require('../logger')
class Weather extends BasePlugin { class Weather extends BasePlugin {
constructor (config) { constructor(config) {
super(config) super(config)
this.name = 'Weather' this.name = 'Weather'
@ -31,19 +31,19 @@ class Weather extends BasePlugin {
} }
} }
help () { help() {
// | Command | Description // | Command | Description
return '.saa <kaupunki> Antaa sään maapallon tärkeimmistä sijainneista' return '.saa <kaupunki> Antaa sään maapallon tärkeimmistä sijainneista'
} }
test (input) { test(input) {
if (input.match(/^\.saa(.*)/i)) { if (input.match(/^\.saa(.*)/i)) {
return true return true
} }
return false return false
} }
getTemperature (city, code) { getTemperature(city, code) {
var language = 'fi' var language = 'fi'
var urlArgs = { var urlArgs = {
@ -57,7 +57,7 @@ class Weather extends BasePlugin {
return promise return promise
} }
getTemperatures (cities, code) { getTemperatures(cities, code) {
logger.info('getting temperatures ', cities, code) logger.info('getting temperatures ', cities, code)
if (typeof code === 'undefined') { if (typeof code === 'undefined') {
code = 'fi' code = 'fi'
@ -76,11 +76,13 @@ class Weather extends BasePlugin {
'APPID': this.apikey 'APPID': this.apikey
} }
var url = 'http://api.openweathermap.org/data/2.5/group' var url = 'http://api.openweathermap.org/data/2.5/group'
var promise = requestPromise(url, urlArgs) var promise = requestPromise(url, urlArgs).catch(e => {
logger.error('Failed to retrieve openweathermap data')
})
return promise return promise
} }
getPenaTemperatures (start, stop, limit) { getPenaTemperatures(start, stop, limit) {
logger.info('getting pena', start, stop, limit) logger.info('getting pena', start, stop, limit)
var urlArgs = {} var urlArgs = {}
@ -96,41 +98,47 @@ class Weather extends BasePlugin {
urlArgs.stop = stop urlArgs.stop = stop
} }
var url = 'http://masa.dy.fi/api/temperature.php' var url = 'https://masa.dy.fi/api/temperature.php'
var promise = requestPromise(url, urlArgs) var promise = requestPromise(url, urlArgs).catch(e => {
logger.error('Failed to retrieve pena temperatures data')
})
return promise return promise
} }
getTemperatureString (owmCityIds) { getTemperatureString(owmCityIds) {
let that = this let that = this
var promise = new Promise(function (resolve, reject) { var promise = new Promise(function (resolve, reject) {
Promise.all([ Promise.all([
that.getTemperatures(owmCityIds), that.getTemperatures(owmCityIds),
// that.getPenaTemperatures() that.getPenaTemperatures()
]).then(function (results) { ]).then(function (results) {
var resultString = '' var resultString = ''
// OWM // OWM
results[0].list.forEach(function (data) { let weatherData = []
if (results[0] !== undefined) {
results[0].list.forEach((data) => {
var city = data.name var city = data.name
var temp = (data.main.temp - 273.15).toFixed(1) var temp = (data.main.temp - 273.15).toFixed(1)
var code = data.weather[0].icon var code = data.weather[0].icon
resultString += '' + city + ' (' + temp + ' °C, ' + that.iconCodeToText[code] + '), ' weatherData.push(city + ' (' + temp + ' °C, ' + that.iconCodeToText[code] + ')')
}) })
}
/*if (results[1].length > 0) { if (results[1] !== undefined) {
if (results[1].length > 0) {
var temp = Number(results[1][0].temp).toFixed(1) var temp = Number(results[1][0].temp).toFixed(1)
resultString += 'Aura (' + temp + ' °C)' weatherData.push('Aura (' + temp + ' °C)')
}*/ }
}
resultString = weatherData.join(', ')
resolve(resultString) resolve(resultString)
}, (str) => reject(str)) }, (str) => reject(str))
}) })
return promise return promise
} }
result (input) { result(input) {
// Different cases // Different cases
let that = this let that = this