nasa-patch-search/index.ts

53 lines
1.4 KiB
TypeScript
Raw Normal View History

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:42:46 +00:00
const patchRequests = allPatches.map((testPatchUrl) =>
(async () => {
const req = await fetch(testPatchUrl);
const data = parse(await req.text());
return data.getElementsByTagName("h1")[0].nextElementSibling?.innerText;
})(),
);
2024-04-03 06:50:09 +00:00
const dirtyPatchDescriptions = await Promise.all(patchRequests);
const patchDescriptions = dirtyPatchDescriptions.filter((e) => e) as string[];
2024-04-03 06:42:46 +00:00
console.log(patchDescriptions);