Skip to content

Instantly share code, notes, and snippets.

@ariedro
Created April 5, 2024 16:48
Show Gist options
  • Save ariedro/e6ed1b543e7dfc872bb0bc8ab60961a8 to your computer and use it in GitHub Desktop.
Save ariedro/e6ed1b543e7dfc872bb0bc8ab60961a8 to your computer and use it in GitHub Desktop.
const https = require("https");
const RETIRO_ID = 332;
const SUAREZ_ID = 190;
const appendSingleZero = (n) => `${n < 10 ? "0" : ""}${n}`;
https.get(
{ hostname: "ariedro.dev", path: "/api-trenes/estaciones/79/horarios" },
(res) => {
const data = [];
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => {
const horarios = JSON.parse(Buffer.concat(data).toString());
const resultadosParaRetiro = horarios.results.filter(
(result) => result.servicio.cabeceraFinal.id === RETIRO_ID
);
if (!resultadosParaRetiro.length) {
console.log("No hay servicio :(");
process.exit(1);
}
for (const result of resultadosParaRetiro) {
console.log("================================");
const estacionesSinSuarez = result.servicio.estaciones.filter(
(estacion) => estacion.id !== SUAREZ_ID
);
for (const estacion of estacionesSinSuarez) {
const { llegada, nombre, segundos } = estacion;
const cuantoFalta =
segundos <= 0
? "------"
: `${appendSingleZero(
parseInt(segundos / 60)
)}m${appendSingleZero(segundos % 60)}s`;
const llegadaDate = new Date(llegada);
const llegadaHora = llegadaDate.getHours();
const llegadaMin = llegadaDate.getMinutes();
const llegadaFormatted = `${appendSingleZero(
llegadaHora
)}:${appendSingleZero(llegadaMin)}`;
console.log(`${cuantoFalta}\t${llegadaFormatted}\t${nombre}`);
}
}
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment