77 lines
2.4 KiB
JavaScript
77 lines
2.4 KiB
JavaScript
const express = require('express');
|
|
const axios = require('axios');
|
|
const cheerio = require('cheerio');
|
|
const cors = require('cors');
|
|
|
|
const app = express();
|
|
const port = 3005;
|
|
|
|
app.use(cors());
|
|
|
|
app.get('/', async (req, res) => {
|
|
try {
|
|
const { param, param2, palina } = req.query;
|
|
const url = `https://infobus.startromagna.it/InfoFermata?param=${param}¶m2=${param2}&palina=${palina}`;
|
|
const response = await axios.get(url);
|
|
const $ = cheerio.load(response.data);
|
|
const results = [];
|
|
const fermata = $('h2.fw-bold.text-primary.title').text().trim();
|
|
results.push({
|
|
fermata
|
|
});
|
|
$('.container.mb-50 .bus-card').each((i, el) => {
|
|
const element = $(el);
|
|
const isSoppressa = element.find('.bus-status.sopp').length > 0;
|
|
const headerSpan = element.find('.bus-header span').first();
|
|
const orario = element.find('.bus-times span').first().text().trim();
|
|
var stato = element.find('.bus-status').text().trim();
|
|
var linea = headerSpan.contents().filter((i, el) => el.type === 'text').text().trim();
|
|
const destinazione = element.find('.bus-destination').text().trim();
|
|
var mezzo = element.find('.det a').attr('data-vehicle') || '';
|
|
|
|
if(mezzo.length == 4){
|
|
mezzo = 3 + mezzo;
|
|
}
|
|
|
|
if(stato == "Non disp"){
|
|
stato = "Non disponibile";
|
|
}
|
|
|
|
if(mezzo == ""){
|
|
mezzo = "Non disponibile";
|
|
}
|
|
|
|
if(linea == "Linea 4" && destinazione == "Mirabilandia"){
|
|
linea = "Linea 4B";
|
|
}
|
|
|
|
if(linea == "Linea 4" && destinazione == "Lido di Dante"){
|
|
linea = "Linea 4D";
|
|
}
|
|
|
|
if(linea == "Linea 1" && destinazione == "Borgo Nuovo"){
|
|
linea = "Linea 1B";
|
|
}
|
|
|
|
|
|
results.push({
|
|
linea,
|
|
destinazione,
|
|
orario,
|
|
stato,
|
|
mezzo,
|
|
soppressa: isSoppressa
|
|
});
|
|
});
|
|
|
|
res.json(results);
|
|
} catch (error) {
|
|
console.error('Errore:', error.message);
|
|
res.status(500).send('Errore nel recupero dei dati');
|
|
}
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
console.log(`API attiva su http://localhost:${port}`);
|
|
});
|