Skip to content

Instantly share code, notes, and snippets.

@lndgalante
Last active May 7, 2024 09:56
Show Gist options
  • Save lndgalante/13b075a4263fa5fa814153b4b9f17848 to your computer and use it in GitHub Desktop.
Save lndgalante/13b075a4263fa5fa814153b4b9f17848 to your computer and use it in GitHub Desktop.
1031 parser
import { stripHtml } from "string-strip-html";
import { mkConfig, generateCsv, asString } from "export-to-csv";
import { writeFile } from "node:fs";
import { Buffer } from "node:buffer";
// merge all pages (get from...)
const allPages = [
];
// parse data from all pages
const data = allPages.map((page) => {
const { top } = page;
const parsedData = top.reduce((accumulator, item, index) => {
const {result} = stripHtml(item.html);
if (index === 0) {
return {
...accumulator,
name: result,
};
}
if (index === 1) {
return {
...accumulator,
address: result,
};
}
if (index === 2) {
return {
...accumulator,
city: result,
};
}
if (index === 3) {
return {
...accumulator,
email: result,
};
}
if (index === 4) {
return {
...accumulator,
phone: result,
};
}
if (index === 5) {
return {
...accumulator,
website: result,
};
}
return {
...accumulator,
};
}, {});
return parsedData;
});
// convert to CSV
const csvConfig = mkConfig({ useKeysAsHeaders: true });
const csv = generateCsv(csvConfig)(data);
const filename = `${csvConfig.filename}.csv`;
const csvBuffer = new Uint8Array(Buffer.from(asString(csv)));
writeFile(filename, csvBuffer, (err) => {
if (err) throw err;
console.log("file saved: ", filename);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment