sokolov-sbs-flowserver/odchod.js
2025-08-07 21:49:01 +02:00

65 lines
1.7 KiB
JavaScript

const fetch = require('node-fetch'); // Import node-fetch
async function getOdchodyData(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const csvData = await response.text();
return parseOdchodyCSV(csvData);
} catch (error) {
console.error("Error fetching or processing data:", error);
return []; // Or handle the error as appropriate for your application
}
}
function parseOdchodyCSV(csvData) {
const lines = csvData.split('\n');
const odchod = [];
let dataStart = -1;
// Find the line where the actual data starts
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith("Linka;Spoj;Čas odjezdu;Nástupiště;Cílová obec spoje;Kód datumové masky")) {
dataStart = i + 1;
break;
}
}
if (dataStart === -1) {
console.warn("Data header not found in CSV.");
return []; // Or handle this error condition
}
for (let i = dataStart; i < lines.length; i++) {
const line = lines[i].trim();
if (line) { // Skip empty lines
const values = line.split(';');
if (values.length >= 6 && values[0] && values[2] && values[4]) {
odchod.push([
parseInt(values[0], 10), // Linka (parse to integer)
values[2], // Čas odjezdu
values[4] // Cílová obec spoje
]);
}
}
}
return odchod;
}
// --- Example Usage ---
const csvUrl = "https://bezpecne.sokolov.cz/zast/jedn_drogerie.csv";
getOdchodyData(csvUrl)
.then(result => {
console.log("Parsed Odchody Data:", result);
})
.catch(error => {
// Error already handled in getOdchodyData, but you can add more specific handling here if needed
});