Restore code
This commit is contained in:
commit
6403020ffd
|
@ -0,0 +1,15 @@
|
|||
# RFI Train Monitor API
|
||||
API for the RFI Train Monitor.
|
||||
## How to use
|
||||
1. Download [Node.JS](https://nodejs.org/en/download) and install it
|
||||
2. Download the repository and extract it
|
||||
3. Open a command line on the folder you extracted the repository
|
||||
4. Run ``npm install`` to install required modules, then ``node server.js`` to start the server instance
|
||||
|
||||
## API params
|
||||
The API is called with 2 params:
|
||||
- Arrivals: A boolean set to True for arrivals, set to False for departures
|
||||
- PlaceId: The ID for the specific train station
|
||||
|
||||
## Rights and credits
|
||||
RFI, TrenItalia are trademarks used here only for reference purpouses.
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"axios": "^1.8.4",
|
||||
"cheerio": "^1.0.0",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^5.1.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
const express = require('express');
|
||||
const axios = require('axios');
|
||||
const cheerio = require('cheerio');
|
||||
const cors = require('cors');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
const port = 3002;
|
||||
|
||||
// Permette richieste da qualsiasi origine
|
||||
app.use(cors());
|
||||
|
||||
// Route API che estrae i dati da monitor
|
||||
app.get('/', async (req, res) => {
|
||||
try {
|
||||
const { Arrivals, PlaceId, exclude } = req.query;
|
||||
|
||||
const response = await axios.get(`https://iechub.rfi.it/ArriviPartenze/ArrivalsDepartures/Monitor?Arrivals=${Arrivals}&PlaceId=${PlaceId}`);
|
||||
const $ = cheerio.load(response.data);
|
||||
|
||||
// Parse excluded column indexes from query
|
||||
const excludeIndexes = exclude
|
||||
? exclude.split(',').map(i => parseInt(i)).filter(i => !isNaN(i))
|
||||
: [];
|
||||
|
||||
const rows = [];
|
||||
|
||||
$('#monitor tbody tr').each((i, row) => {
|
||||
const allColumns = $(row).find('td').map((j, col) => $(col).text().trim()).get();
|
||||
if (allColumns.length > 0) {
|
||||
// Filter out excluded columns
|
||||
const filteredColumns = allColumns.filter((_, idx) => !excludeIndexes.includes(idx));
|
||||
rows.push(filteredColumns);
|
||||
}
|
||||
});
|
||||
|
||||
res.json(rows);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send('Errore nel recupero dei dati');
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server avviato su http://localhost:${port}`);
|
||||
});
|
Loading…
Reference in New Issue