Files
CrazeAnalytix/test-csv.js
T

67 lines
2.1 KiB
JavaScript

import fs from 'fs';
import https from 'https';
const url = "https://www.dropbox.com/scl/fi/b9zxn4z5i7sxwfakk5g5y/Amazon-Sell-Out-2023-2025.csv?rlkey=uoto6v0mm99py8nszy8ldtez8&dl=1";
https.get(url, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
https.get(res.headers.location, (res2) => {
processData(res2);
});
} else {
processData(res);
}
}).on('error', (e) => {
console.error(e);
});
function processData(stream) {
let data = '';
stream.on('data', chunk => {
data += chunk.toString('utf8');
});
stream.on('end', () => {
analyze(data);
});
}
function analyze(data) {
const lines = data.split('\n');
console.log("Total Lines in CSV:", lines.length);
let maxYear = 0;
let maxWeek = 0;
let minYear = 2030;
let minWeek = 52;
let legendsRows = 0;
let legendsMaxYear = 0;
let legendsMaxWeek = 0;
for (let i = 1; i < lines.length; i++) {
const row = lines[i].split(',');
if (row.length < 5) continue;
const year = parseInt(row[3]); // Based on app processor logic it's mostly col 3 or 1
const week = parseInt(row[4]); // Based on app processor
// Use regex fallback if parsing fails
const matchedYear = parseInt(row.find(r => r.startsWith('202')) || '0');
const matchedWeek = parseInt(row.find(r => r.match(/^[0-9]{1,2}$/)) || '0');
const y = year || matchedYear || 0;
const w = week || matchedWeek || 0;
if (y > maxYear) { maxYear = y; maxWeek = w; }
else if (y === maxYear && w > maxWeek) { maxWeek = w; }
if (y < minYear && y > 2000) { minYear = y; minWeek = w; }
if (lines[i].toLowerCase().includes('legends')) {
legendsRows++;
if (y > legendsMaxYear) { legendsMaxYear = y; legendsMaxWeek = w; }
else if (y === legendsMaxYear && w > legendsMaxWeek) { legendsMaxWeek = w; }
}
}
console.log(`Global -> Min Date: Year ${minYear}, Week ${minWeek} | Max Date: Year ${maxYear}, Week ${maxWeek}`);
console.log(`LEGENDS stats -> Total rows: ${legendsRows}, Max Date: Year ${legendsMaxYear}, Week ${legendsMaxWeek}`);
}