nasa-patch-search/index.ts

36 lines
868 B
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 02:33:13 +00:00
[...Array(7).keys()]
.map((val) => ++val)
.map(async (page) => {
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 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);