export default async function harpers() {
const { default: { load } } = await import("https://cdn.jsdelivr.net/npm/cheerio@1.0.0-rc.12/+esm");
const { default: { sentences } } = await import("https://cdn.jsdelivr.net/npm/sbd@1.0.19/+esm");
const continuations = "Who In By Of That".split(" ");
async function request(url) {
console.log(url);
const res = await fetch(url);
return load(await res.text());
}
const results = await Promise.all([
request("https://harpers.org/sections/findings/")
.then(($) => request($(".card a").attr("href")))
.then(($) => sentences($(".wysiwyg-content > p").text())),
request("https://harpers.org/harpers-index/")
.then(($) =>
$(".index-statement")
.map((_, s) =>
$("> p", s).text()
+ $("> span", s).text().trim().replace(/\s+/g, " ")
)
.get()
)
.then((lines) =>
lines.reduce((lines, line) => {
if (lines.length === 0)
lines.push(line);
else if (continuations.some((c) => line.startsWith(c)))
lines[lines.length - 1] = lines[lines.length - 1] + ", " + line;
else
lines.push(line);
return lines;
}, [])
),
]);
return new Response(results.flat().join("\n"));
}