diff --git a/package.json b/package.json new file mode 100644 index 0000000..a5a0e1f --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "start-fermate", + "version": "1.0.0", + "main": "script.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "axios": "^1.10.0", + "cheerio": "^1.1.0", + "cors": "^2.8.5", + "express": "^5.1.0", + "node-fetch": "^3.3.2" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..64880f7 --- /dev/null +++ b/server.js @@ -0,0 +1,66 @@ +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 = []; + $('.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(); + 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(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, + 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}`); +});