-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
const Generic = require('./generic') | ||
const Weather = require('./Weather') | ||
|
||
module.exports = { | ||
generic: new Generic() | ||
generic: new Generic(), | ||
weather: new Weather() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const MunicipioService = require('../services/municipio') | ||
const AemetService = require('../services/aemet') | ||
|
||
class Weather { | ||
async getAemet (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}`); | ||
} | ||
} | ||
} | ||
|
||
module.exports = Weather |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const mongoose = require('mongoose') | ||
const Schema = mongoose.Schema | ||
|
||
/* Municipio Schema */ | ||
const MunicipioSchema = new Schema({ | ||
codigoProvincia: { | ||
type: String, | ||
required: true | ||
}, | ||
codigoMunicipio: { | ||
type: String, | ||
required: true | ||
}, | ||
nombre: { | ||
type: String, | ||
required: true | ||
}, | ||
}) | ||
|
||
module.exports = mongoose.model('municipio', MunicipioSchema, 'municipios') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const config = require('../config') | ||
|
||
const endpointPrefix = 'https://opendata.aemet.es/opendata/api/' | ||
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 | ||
} | ||
|
||
|
||
async function getAemetData(url) { | ||
const response = await fetch(url) | ||
return await response.json() | ||
} | ||
|
||
module.exports = { | ||
getTimePrediction | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const dbManager = require('../helpers/dbmanager') | ||
|
||
async function getMunicipioCode(name) { | ||
let result = null | ||
|
||
name = name.trim() | ||
const articles = ['El', 'La', 'Las', 'Los', 'L', 'Es', 'L\'','Les', 'Els', 'O', 'A', 'As', 'l\''] | ||
articles.forEach(article => { | ||
article = article + ' ' | ||
const regEx = new RegExp('^' + article, "ig"); | ||
name = name.replace(regEx, '') | ||
}) | ||
|
||
name = name.replaceAll('a', '[àáa]') | ||
name = name.replaceAll('e', '[èée]') | ||
name = name.replaceAll('i', '[ïií]') | ||
name = name.replaceAll('o', '[òóo]') | ||
name = name.replaceAll('u', '[uúü]') | ||
|
||
result = await dbManager.getMunicipio(name) | ||
if (result !== null) return result | ||
result = await dbManager.getMuncipioStartsWith(name) | ||
if (result !== null) return result | ||
result = await dbManager.getMuncipioEndsWith(name) | ||
if (result !== null) return result | ||
result = await dbManager.getMuncipioContains(name) | ||
|
||
return result | ||
} | ||
|
||
module.exports = { | ||
getMunicipioCode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters