nasa-patch-search/index.ts

42 lines
1.0 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)
.map((page) => {
return (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();
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 02:27:00 +00:00
console.log(allPatches);