70 %
Chris Biscardi

Hacky yearly word count: 2020

At the end of 2020 I wanted to count the words I'd written in approximately 171 posts (this is relevant because there's a magic number in the following script) I wrote through out the year. The following is that script ::law-and-order-bum-bum-sound::

hacky-wordcount.js
JS
const cheerio = require("cheerio");
const bent = require("bent");
var unified = require("unified");
var parse = require("rehype-parse");
var rehype2retext = require("rehype-retext");
var english = require("retext-english");
const stringify = require("rehype-stringify");
const visit = require("unist-util-visit");
let get = bent("https://www.christopherbiscardi.com/", "GET", 200);
main();
let count = 0;
function wordCount(opts) {
return (ast) => {
visit(ast, "WordNode", (node) => {
count++;
});
};
}
async function main() {
const t = await get("/garden");
const html = await t.text();
const $ = cheerio.load(html);
const $thing = $("li > a.block");
const hrefs = $thing.map((i, v) => $(v).attr("href")).slice(0, 171);
let results = await Promise.all(
Array.from(hrefs).map(async (v) => {
console.log(v);
const h = await get(v);
const html = await h.text();
const $ = cheerio.load(html);
const divs = $("#toast-page-section > div > div");
const the_html = $(divs[3]).html();
return new Promise((resolve, reject) => {
unified()
.use(parse)
.use(rehype2retext, unified().use(english).use(wordCount))
.use(stringify)
.process(the_html, (err, file) => {
console.log(err);
resolve();
});
});
})
);
console.log(count);
}