This commit is contained in:
2026-06-06 10:15:53 -04:00
parent 71d7a9d6f0
commit 78220d3568
11 changed files with 1315 additions and 335 deletions

View File

@@ -7,13 +7,15 @@ const API = "https://en.wikipedia.org/w/api.php";
async function search(term: string) {
const url = `${API}?action=query&list=search&srsearch=${encodeURIComponent(term)}&format=json&srlimit=1&origin=*`;
const res = await fetch(url, { headers: { "User-Agent": "PlantHealthKB/1.0" } });
return await res.json() as { query?: { search?: Array<{ title: string; pageid: number }> } };
return (await res.json()) as { query?: { search?: Array<{ title: string; pageid: number }> } };
}
async function getImg(title: string) {
const url = `${API}?action=query&titles=${encodeURIComponent(title)}&prop=pageimages&format=json&pithumbsize=400&origin=*`;
const res = await fetch(url, { headers: { "User-Agent": "PlantHealthKB/1.0" } });
return await res.json() as { query?: { pages?: Record<string, { thumbnail?: { source: string } }> } };
return (await res.json()) as {
query?: { pages?: Record<string, { thumbnail?: { source: string } }> };
};
}
async function testOne(term: string) {
@@ -22,7 +24,10 @@ async function testOne(term: string) {
if (page) {
const img = await getImg(page.title);
const pages = img?.query?.pages;
if (!pages) { console.log(term, '→ NO PAGES'); return; }
if (!pages) {
console.log(term, "→ NO PAGES");
return;
}
const first = Object.values(pages)[0] as { thumbnail?: { source: string } };
const thumb = first?.thumbnail?.source;
console.log(`${term.padEnd(40)}${page.title.padEnd(50)}${thumb ?? "NO IMG"}`);