Initial commit
This commit is contained in:
commit
c53c7085bc
60 changed files with 23885 additions and 0 deletions
65
odchod.js
Normal file
65
odchod.js
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
|
||||
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
|
||||
});
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue