Skip to content

Commit

Permalink
get sunset time
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Jan 29, 2024
1 parent 18f8e85 commit ee65b72
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main_twitch-mz-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
envkey_TWITCH_CHANNELS: ${{ secrets.TWITCH_CHANNELS }}
envkey_TWITCH_USER_ID: ${{ secrets.TWITCH_USER_ID }}
envKey_MONGODB_URI: ${{ secrets.MONGODB_URI }}
envKey_AEMET_API_KEY: ${{ secrets.AEMET_API_KEY }}
envkey_PORT: 3000

- name: Set up Node.js version
Expand Down
3 changes: 3 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ module.exports = {
clientId: process.env.TWITCH_CLIENT_ID,
clientSecret: process.env.TWITCH_CLIENT_SECRET,
userId: process.env.TWITCH_USER_ID
},
aemet: {
apiKey: process.env.AEMET_API_KEY
}
}
4 changes: 3 additions & 1 deletion handlers/index.js
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()
}
16 changes: 16 additions & 0 deletions handlers/weather.js
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
25 changes: 23 additions & 2 deletions helpers/dbmanager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Token = require('../models/token')
const Muncipio = require('../models/municipio')

function getToken (userId) {
return Token.findOne({userId: userId})
Expand All @@ -8,7 +9,27 @@ function updateToken (userId, update) {
return Token.updateOne({userId: userId}, update)
}

async function getMuncipioStartsWith (name) {
return Muncipio.findOne({nombre: { "$regex": "^" + name , "$options": "i"}}).lean()
}

async function getMuncipioEndsWith (name) {
return Muncipio.findOne({nombre: { "$regex": name + "$", "$options": "i"}}).lean()
}

async function getMuncipioContains (name) {
return Muncipio.findOne({nombre: { "$regex": name, "$options": "i"}}).lean()
}

async function getMunicipio (name) {
return Muncipio.findOne({nombre: { "$regex": "^" + name + "$", "$options": "i"}}).lean()
}

module.exports = {
getToken,
updateToken
}
updateToken,
getMunicipio,
getMuncipioStartsWith,
getMuncipioEndsWith,
getMuncipioContains
}
6 changes: 5 additions & 1 deletion lib/inputParser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class InputParser {
isAskingForRollDice (text) {
return text === ('!dice')
return text === ('!dados')
}

isAskingForSunset (text) {
return text.startsWith('!sunset') || text.startsWith('!ocaso') || text.startsWith('!atardecer')
}
}

Expand Down
8 changes: 7 additions & 1 deletion lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,15 @@ class Messenger {
if (self) { return; } // Ignore messages from the bot

const text = msg.trim();
const textSplit = text.split(' ')

if (inputParser.isAskingForRollDice(text))

if (textSplit.length > 0 && inputParser.isAskingForRollDice(textSplit[0]))
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)
}
}

handleConnect (addr, port) {
Expand Down
20 changes: 20 additions & 0 deletions models/municipio.js
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')
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dotenv": "^16.4.1",
"express": "^4.18.2",
"mongoose": "^8.1.1",
"node-fetch": "^3.3.2",
"tmi.js": "^1.8.5"
},
"scripts": {
Expand Down
30 changes: 30 additions & 0 deletions services/aemet.js
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
}
33 changes: 33 additions & 0 deletions services/municipio.js
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
}
39 changes: 39 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ cookie@0.5.0:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.5.0.tgz#d1f5d71adec6558c58f389987c366aa47e994f8b"
integrity sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==

data-uri-to-buffer@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e"
integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==

debug@2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -279,6 +284,14 @@ express@^4.18.2:
utils-merge "1.0.1"
vary "~1.1.2"

fetch-blob@^3.1.2, fetch-blob@^3.1.4:
version "3.2.0"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9"
integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==
dependencies:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"

finalhandler@1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/finalhandler/-/finalhandler-1.2.0.tgz#7d23fe5731b207b4640e4fcd00aec1f9207a7b32"
Expand All @@ -292,6 +305,13 @@ finalhandler@1.2.0:
statuses "2.0.1"
unpipe "~1.0.0"

formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"

forwarded@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811"
Expand Down Expand Up @@ -485,13 +505,27 @@ negotiator@0.6.3:
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
integrity sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==

node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==

node-fetch@^2.6.1, node-fetch@^2.6.12:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
dependencies:
whatwg-url "^5.0.0"

node-fetch@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.2.tgz#d1e889bacdf733b4ff3b2b243eb7a12866a0b78b"
integrity sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==
dependencies:
data-uri-to-buffer "^4.0.0"
fetch-blob "^3.1.4"
formdata-polyfill "^4.0.10"

object-inspect@^1.9.0:
version "1.13.1"
resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.13.1.tgz#b96c6109324ccfef6b12216a956ca4dc2ff94bc2"
Expand Down Expand Up @@ -683,6 +717,11 @@ vary@~1.1.2:
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==

web-streams-polyfill@^3.0.3:
version "3.3.2"
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.3.2.tgz#32e26522e05128203a7de59519be3c648004343b"
integrity sha512-3pRGuxRF5gpuZc0W+EpwQRmCD7gRqcDOMt688KmdlDAgAyaB1XlN0zq2njfDNm44XVdIouE7pZ6GzbdyH47uIQ==

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
Expand Down

0 comments on commit ee65b72

Please sign in to comment.