Skip to content

Commit

Permalink
add train command for renfe
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Jan 31, 2024
1 parent ee65b72 commit 48ee809
Show file tree
Hide file tree
Showing 12 changed files with 1,002 additions and 51 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 @@ -25,6 +25,7 @@ jobs:
envkey_TWITCH_USER_ID: ${{ secrets.TWITCH_USER_ID }}
envKey_MONGODB_URI: ${{ secrets.MONGODB_URI }}
envKey_AEMET_API_KEY: ${{ secrets.AEMET_API_KEY }}
envKey_SQL_CONNECTION: ${{ secrets.SQL_CONNECTION }}
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 @@ -8,5 +8,8 @@ module.exports = {
},
aemet: {
apiKey: process.env.AEMET_API_KEY
},
sql : {
connectionString: process.env.SQL_CONNECTION
}
}
4 changes: 3 additions & 1 deletion handlers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const Generic = require('./generic')
const Weather = require('./Weather')
const Train = require('./Train')

module.exports = {
generic: new Generic(),
weather: new Weather()
weather: new Weather(),
train: new Train()
}
13 changes: 13 additions & 0 deletions handlers/train.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const RenfeService = require('../services/renfe')

class Train {
async getTrainTime (target, originText, destinationText, bot) {
const result = await RenfeService.getMDTrain(originText, destinationText)

if (result) {
bot.say(target, `El próximo tren MD sale a las ${result.time} de la ${result.origin_stop} con destino ${result.destination_stop}`);
}
}
}

module.exports = Train
2 changes: 2 additions & 0 deletions handlers/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Weather {
bot.say(target, `${municipio.nombre} atardece a las ${ocaso}`);
}
}


}

module.exports = Weather
53 changes: 53 additions & 0 deletions helpers/sqlManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const sql = require('mssql')
const config = require('../config')
const Token = require("../models/token");

async function getTrainMDTime (origin, destination) {
try {
this.pool = await sql.connect(config.sql.connectionString)
const request = this.pool.request()
const result = await request.query(`SELECT TOP 1 CONVERT(VARCHAR(8),A.departure_time, 108) time , A.stop_name origin_stop, B.stop_name destination_stop FROM
(SELECT ST.trip_id , S.stop_name, ST.stop_id, ST.stop_sequence, ST.departure_time
from stop_times ST
inner join stops S ON S.stop_id = ST.stop_id
WHERE
S.stop_name LIKE '%${origin}%' and
ST.pickup_type = 0
) AS A
INNER JOIN
(SELECT ST.trip_id, ST.stop_id, ST.stop_sequence, S.stop_name
from stop_times ST
inner join stops S ON S.stop_id = ST.stop_id
WHERE
S.stop_name LIKE '%${destination}%' and
ST.drop_off_type = 0
) AS B
ON A.trip_id = B.trip_id
inner join trips T ON T.trip_id = A.trip_id
inner join routes R ON R.route_id = T.route_id
inner join calendar C ON C.service_id = T.service_id
WHERE A.stop_sequence < B.stop_sequence AND
C.start_date <= GETDATE() and
C.end_date >= GETDATE() AND
T.service_id NOT IN (SELECT CD.service_id FROM calendar_dates CD
where CAST (CD.date AS DATE) = CAST(GETDATE() AS DATE)) AND
route_short_name = 'MD'
AND A.departure_time > CONVERT (time, GETDATE())
ORDER BY ABS( DATEDIFF(minute, A.departure_time, CONVERT (time, GETDATE())) )`)
//const result = await sql.query
console.log(result.recordset[0])
return result.recordset[0]
} catch (err) {
console.log(err)
}
}

module.exports = {
getTrainMDTime
}
4 changes: 4 additions & 0 deletions lib/inputParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class InputParser {
isAskingForSunset (text) {
return text.startsWith('!sunset') || text.startsWith('!ocaso') || text.startsWith('!atardecer')
}

isAskingForTrainTime (text) {
return text.toLowerCase().startsWith('!tren')
}
}

module.exports = InputParser
4 changes: 4 additions & 0 deletions lib/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class Messenger {
if (textSplit.length > 1 && inputParser.isAskingForSunset(textSplit[0])) {
return handlers.weather.getAemet(target, textSplit.slice(1).join(' '), this.bot)
}

if (textSplit.length === 3 && inputParser.isAskingForTrainTime(textSplit[0])) {
return handlers.train.getTrainTime(target, textSplit[1], textSplit[2], this.bot)
}
}

handleConnect (addr, port) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dotenv": "^16.4.1",
"express": "^4.18.2",
"mongoose": "^8.1.1",
"node-fetch": "^3.3.2",
"mssql": "^10.0.2",
"tmi.js": "^1.8.5"
},
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion services/aemet.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ async function getTimePrediction(name) {
return result[0].prediccion.dia[0].ocaso
}


async function getAemetData(url) {
const response = await fetch(url)
return await response.json()
Expand Down
9 changes: 9 additions & 0 deletions services/renfe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const sqlManager = require('../helpers/sqlManager')

async function getMDTrain (origin, destination) {
return sqlManager.getTrainMDTime(origin, destination)
}

module.exports = {
getMDTrain
}
Loading

0 comments on commit 48ee809

Please sign in to comment.