2024-04-03 02:24:31 +00:00
|
|
|
import { parse } from "node-html-parser";
|
|
|
|
|
|
|
|
const BASE_URL =
|
|
|
|
"https://www.nasa.gov/gallery/human-spaceflight-mission-patches/";
|
|
|
|
|
2024-04-03 02:27:00 +00:00
|
|
|
async function getPatchHrefs() {
|
2024-04-03 02:24:31 +00:00
|
|
|
function getGalleryPageHref(page: number) {
|
|
|
|
if (page > 1) {
|
|
|
|
return `${BASE_URL}page/${page}/`;
|
|
|
|
} else {
|
|
|
|
return BASE_URL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 03:21:55 +00:00
|
|
|
const hrefRequests = [...Array(7).keys()]
|
|
|
|
.map((val) => ++val)
|
2024-04-03 03:32:01 +00:00
|
|
|
.map((page) =>
|
|
|
|
(async () => {
|
2024-04-03 03:21:55 +00:00
|
|
|
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[];
|
2024-04-03 03:32:01 +00:00
|
|
|
})(),
|
|
|
|
);
|
2024-04-03 03:21:55 +00:00
|
|
|
|
|
|
|
const hrefLists = await Promise.all(hrefRequests);
|
|
|
|
|
|
|
|
return hrefLists.flat();
|
2024-04-03 02:27:00 +00:00
|
|
|
}
|
2024-04-03 02:24:31 +00:00
|
|
|
|
2024-04-03 02:27:00 +00:00
|
|
|
const allPatches = await getPatchHrefs();
|
2024-04-03 02:24:31 +00:00
|
|
|
|
2024-04-03 06:59:41 +00:00
|
|
|
const patchRequests = allPatches.map((url) =>
|
2024-04-03 06:42:46 +00:00
|
|
|
(async () => {
|
2024-04-03 06:59:41 +00:00
|
|
|
const req = await fetch(url);
|
2024-04-03 06:42:46 +00:00
|
|
|
const data = parse(await req.text());
|
2024-04-03 06:59:41 +00:00
|
|
|
return {
|
|
|
|
url,
|
|
|
|
description:
|
|
|
|
data.getElementsByTagName("h1")[0].nextElementSibling?.innerText,
|
|
|
|
};
|
2024-04-03 06:42:46 +00:00
|
|
|
})(),
|
|
|
|
);
|
|
|
|
|
2024-04-03 06:50:09 +00:00
|
|
|
const dirtyPatchDescriptions = await Promise.all(patchRequests);
|
2024-04-03 06:59:41 +00:00
|
|
|
const patchDescriptions = dirtyPatchDescriptions.filter(
|
|
|
|
({ description }) => description,
|
|
|
|
) as { url: string; description: string }[];
|
2024-04-03 06:42:46 +00:00
|
|
|
|
|
|
|
console.log(patchDescriptions);
|