-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
207 lines (165 loc) · 5.75 KB
/
server.js
File metadata and controls
207 lines (165 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const cors = require('cors');
const app = express();
const port = 3005;
const version = "2.3.1";
app.use(cors());
app.use(express.json());
app.get('/fermata', async (req, res) => {
try {
const { param, param2, palina, det } = 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 = [];
let 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();
let stato = element.find('.bus-status').text().trim();
let linea = headerSpan.contents().filter((i, el) => el.type === 'text').text().trim();
linea = linea.replace("Linea ", "");
let destinazione = element.find('.bus-destination').text().trim();
let mezzo = element.find('.det a').attr('data-vehicle') || '';
//Aggiustamenti
if (mezzo.length == 4) {
mezzo = 3 + mezzo;
}
if (stato == "Non disp") {
stato = "N.D.";
}
if (mezzo == "") {
mezzo = "N.D.";
}
if (destinazione == "Fornace.Zarattini") {
destinazione = "Fornace Zarattini"
}
if (linea == "4" && destinazione == "Classe") {
destinazione = "Classe Piazza";
linea = "4R";
}
//Varianti linee
if (linea == "1" && destinazione == "Borgo Nuovo") {
linea = "1B";
}
if (linea == "4" && destinazione == "Mirabilandia") {
linea = "4B";
}
if (linea == "4" && destinazione == "Classe Cantoniera") {
linea = "4C";
}
if (linea == "4" && destinazione == "ClasseCantoniera") {
linea = "4C";
}
if (linea == "4" && destinazione == "Lido di Dante") {
linea = "4D";
}
if (linea == "4" && destinazione == "Fosso Ghiaia") {
linea = "4F";
}
if (linea == "4" && destinazione == "Fosso Ghiaia-Standiana") {
linea = "4F";
}
if (linea == "4" && destinazione == "Savio di Cervia") {
linea = "4F";
}
if (linea == "4" && destinazione == "Classe via Liburna") {
linea = "4R";
}
if (linea == "4" && destinazione == "Classe Romea Vecchia") {
linea = "4R";
}
if (linea == "4" && destinazione == "R.Vecchia") {
linea = "4R";
}
if (linea == "4" && destinazione == "Romea Vecchia via del Centurione") {
linea = "4C";
}
if (linea == "161" && destinazione == "_") {
destinazione = "Alfonsine (corsa bis)";
}
//Linee limitate o soppresse a metà
if (linea == "8" && destinazione == "Deposito") {
linea = "8/";
}
if (linea == "3" && destinazione == "via Sant'Alberto") {
linea = "3/";
}
if (linea == "1" && destinazione == "Antica Milizia") {
linea = "1/";
}
if (linea == "1" && destinazione == "Via Antica Milizia") {
linea = "1/";
}
const linee = ["1", "1B", "3", "4", "4B", "4C", "4D", "4F", "4R", "5", "8", "18", "70", "80"];
if (linee.includes(linea) && destinazione == "Stazione FS") {
linea = linea + "/";
}
if (linee.includes(linea) && destinazione == "STAZIONE FS") {
linea = linea + "/";
}
if (linee.includes(linea) && destinazione == "Ravenna FS") {
linea = linea + "/";
}
if (linee.includes(linea) && destinazione == "Ravenna Radio Taxi") {
linea = linea + "/";
}
//Pulisco la spazzatura che mandano le AVM da circa natale 2025
const azioni = ["RIENTRO DEPOSITO", "PRESA SERVIZIO", "FUORI LINEA"];
if(det == "true" || !azioni.includes(destinazione)){
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.get('/versione', async (req, res) => {
res.send(version);
});
app.listen(port, () => {
console.log(`API attiva su http://localhost:${port}`);
});