Initial files
This commit is contained in:
parent
f0f89e5f92
commit
3b6397602f
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "start-fermate-api",
|
||||
"version": "2.1.0",
|
||||
"main": "script.js",
|
||||
"scripts": {
|
||||
"start": "node server.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "IlDani - Curry141",
|
||||
"license": "MIT",
|
||||
"description": "API for a frontend project by the ER-TPL team",
|
||||
"dependencies": {
|
||||
"axios": "^1.10.0",
|
||||
"cheerio": "^1.1.0",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^5.1.0",
|
||||
"node-fetch": "^3.3.2"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
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.use(express.json());
|
||||
|
||||
app.get('/fermata', 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 = [];
|
||||
var fermata = $('h2.fw-bold.text-primary.title').text().trim();
|
||||
if(fermata != ""){
|
||||
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.get('/bacino', async (req, res) => {
|
||||
const selectedOption = req.query.selectedOption;
|
||||
|
||||
if (!selectedOption || !['ra', 'rn', 'fc'].includes(selectedOption)) {
|
||||
return res.status(400).json({ error: 'Parametro selectedOption mancante o non valido' });
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.post('https://infobus.startromagna.it/FermateService.asmx/GetFermateByBacino', {
|
||||
bacino: selectedOption
|
||||
}, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
const raw = response.data.d;
|
||||
const data = raw.map(({ nome, palina, targetID }) => ({ nome, palina, targetID }));
|
||||
if (Array.isArray(data)) {
|
||||
res.json(data);
|
||||
} else {
|
||||
res.status(500).json({ error: 'Formato di risposta non valido dal server remoto' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Errore nella richiesta al servizio remoto:', error);
|
||||
res.status(500).json({ error: 'Errore nel contattare il servizio remoto' });
|
||||
}
|
||||
});
|
||||
app.listen(port, () => {
|
||||
console.log(`API attiva su http://localhost:${port}`);
|
||||
});
|
Loading…
Reference in New Issue