From 4f8a40a8edeaca26e20d4ba21f046eca1f128588 Mon Sep 17 00:00:00 2001 From: Lluis Date: Wed, 31 Jan 2024 23:49:42 +0100 Subject: [PATCH] add !amanecer command to get the sunrise time --- README.md | 5 +++++ handlers/weather.js | 18 +++++++++++++++--- lib/inputParser.js | 6 +++++- lib/messenger.js | 6 +++++- services/aemet.js | 30 +++++++++++++++++++++++------- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e86abba..c60e0d1 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,11 @@ PORT //port of the running app Information from weather comes from AEMET OpenData to get Spain weather predictions. ### commands +```javascript +!amanecer girona +//result: Girona amanece a las 08:02 +``` + ```javascript !atardecer girona //result: Girona atardece a las 17:59 diff --git a/handlers/weather.js b/handlers/weather.js index 9fa0067..1dc3821 100644 --- a/handlers/weather.js +++ b/handlers/weather.js @@ -2,13 +2,25 @@ const MunicipioService = require('../services/municipio') const AemetService = require('../services/aemet') class Weather { - async getAemet (target, text, bot) { + async getSunset (target, text, bot) { const municipio = await MunicipioService.getMunicipioCode(text) + if (municipio !== null) { + const code = `${municipio.codigoProvincia}${municipio.codigoMunicipio}` + const sunsetTime = await AemetService.getSunsetPrediction(code) + if (sunsetTime) { + bot.say(target, `${municipio.nombre} atardece a las ${sunsetTime}`) + } + } + } + async getSunrise (target, text, bot) { + const municipio = await MunicipioService.getMunicipioCode(text) if (municipio !== null) { const code = `${municipio.codigoProvincia}${municipio.codigoMunicipio}` - const ocaso = await AemetService.getTimePrediction(code) - bot.say(target, `${municipio.nombre} atardece a las ${ocaso}`); + const sunriseTime = await AemetService.getSunrisePrediction(code) + if (sunriseTime) { + bot.say(target, `${municipio.nombre} amanece a las ${sunriseTime}`) + } } } } diff --git a/lib/inputParser.js b/lib/inputParser.js index c633695..4077124 100644 --- a/lib/inputParser.js +++ b/lib/inputParser.js @@ -4,7 +4,11 @@ class InputParser { } isAskingForSunset (text) { - return text.startsWith('!sunset') || text.startsWith('!ocaso') || text.startsWith('!atardecer') + return text.startsWith('!atardecer') + } + + isAskingForSunrise (text) { + return text.startsWith('!amanecer') } isAskingForNextMDTrain (text) { diff --git a/lib/messenger.js b/lib/messenger.js index 9d90029..01b30ef 100644 --- a/lib/messenger.js +++ b/lib/messenger.js @@ -56,7 +56,11 @@ class Messenger { return handlers.generic.rollDice(target, this.bot) if (textSplit.length > 1 && inputParser.isAskingForSunset(textSplit[0])) { - return handlers.weather.getAemet(target, textSplit.slice(1).join(' '), this.bot) + return handlers.weather.getSunset(target, textSplit.slice(1).join(' '), this.bot) + } + + if (textSplit.length > 1 && inputParser.isAskingForSunrise(textSplit[0])) { + return handlers.weather.getSunrise(target, textSplit.slice(1).join(' '), this.bot) } if (textSplit.length === 3 && inputParser.isAskingForNextMDTrain(textSplit[0])) { diff --git a/services/aemet.js b/services/aemet.js index 0e3d267..fd623b7 100644 --- a/services/aemet.js +++ b/services/aemet.js @@ -1,9 +1,27 @@ const config = require('../config') const endpointPrefix = 'https://opendata.aemet.es/opendata/api/' -async function getTimePrediction(name) { - let result = null +async function getSunsetPrediction(name) { + let sunsetTime = null + const result = await _getTimePrediction(name) + if (result) { + sunsetTime = result[0].prediccion.dia[0].ocaso + } + return sunsetTime +} + +async function getSunrisePrediction(name) { + let sunsetTime = null + const result = await _getTimePrediction(name) + if (result) { + sunsetTime = result[0].prediccion.dia[0].orto + } + return sunsetTime +} + +async function _getTimePrediction(name) { + let result = null const endpoint = endpointPrefix + 'prediccion/especifica/municipio/horaria/' + name const options = { headers: { @@ -11,12 +29,9 @@ async function getTimePrediction(name) { 'api_key': config.aemet.apiKey } } - const response = await fetch(endpoint, options) const data = await response.json() - result = await getAemetData(data.datos) - - return result[0].prediccion.dia[0].ocaso + return await getAemetData(data.datos) } async function getAemetData(url) { const response = await fetch(url) @@ -24,5 +39,6 @@ async function getAemetData(url) { } module.exports = { - getTimePrediction + getSunsetPrediction, + getSunrisePrediction }