Skip to content

Commit

Permalink
add !amanecer command to get the sunrise time
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Jan 31, 2024
1 parent 4560938 commit 4f8a40a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions handlers/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/inputParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
30 changes: 23 additions & 7 deletions services/aemet.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
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: {
'accept': 'application/json',
'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)
return await response.json()
}

module.exports = {
getTimePrediction
getSunsetPrediction,
getSunrisePrediction
}

0 comments on commit 4f8a40a

Please sign in to comment.