59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { parse } from "node-html-parser";
|
|
|
|
const BASE_URL =
|
|
"https://www.nasa.gov/gallery/human-spaceflight-mission-patches/";
|
|
|
|
async function getPatchHrefs() {
|
|
function getGalleryPageHref(page: number) {
|
|
if (page > 1) {
|
|
return `${BASE_URL}page/${page}/`;
|
|
} else {
|
|
return BASE_URL;
|
|
}
|
|
}
|
|
|
|
const hrefRequests = [...Array(7).keys()]
|
|
.map((val) => ++val)
|
|
.map((page) =>
|
|
(async () => {
|
|
const req = await fetch(getGalleryPageHref(page));
|
|
const data = parse(await req.text());
|
|
|
|
const gallery = data
|
|
.getElementsByTagName("div")
|
|
.filter((el) => el.classList.contains("hds-gallery-items"))
|
|
.shift();
|
|
|
|
return gallery
|
|
?.getElementsByTagName("a")
|
|
.map((el) => el.getAttribute("href"))
|
|
.filter((el) => el) as string[];
|
|
})(),
|
|
);
|
|
|
|
const hrefLists = await Promise.all(hrefRequests);
|
|
|
|
return hrefLists.flat();
|
|
}
|
|
|
|
const allPatches = await getPatchHrefs();
|
|
|
|
const patchRequests = allPatches.map((url) =>
|
|
(async () => {
|
|
const req = await fetch(url);
|
|
const data = parse(await req.text());
|
|
return {
|
|
url,
|
|
description:
|
|
data.getElementsByTagName("h1")[0].nextElementSibling?.innerText,
|
|
};
|
|
})(),
|
|
);
|
|
|
|
const dirtyPatchDescriptions = await Promise.all(patchRequests);
|
|
const patchDescriptions = dirtyPatchDescriptions.filter(
|
|
({ description }) => description,
|
|
) as { url: string; description: string }[];
|
|
|
|
console.log(patchDescriptions);
|