68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
/**
|
|
* Quick test of Wikipedia image API for disease search terms.
|
|
* Run: cd apps/web && npx tsx scripts/test-wiki-images.ts
|
|
*/
|
|
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 }> } };
|
|
}
|
|
|
|
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 } }> };
|
|
};
|
|
}
|
|
|
|
async function testOne(term: string) {
|
|
const s = await search(term);
|
|
const page = s?.query?.search?.[0];
|
|
if (page) {
|
|
const img = await getImg(page.title);
|
|
const pages = img?.query?.pages;
|
|
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"}`);
|
|
} else {
|
|
console.log(`${term.padEnd(40)} → NO PAGE`);
|
|
}
|
|
await new Promise((r) => setTimeout(r, 400));
|
|
}
|
|
|
|
async function main() {
|
|
const tests = [
|
|
"Phytophthora infestans Late Blight",
|
|
"Early Blight",
|
|
"Septoria Leaf Spot",
|
|
"Powdery Mildew",
|
|
"Fusarium oxysporum",
|
|
"Citrus Canker",
|
|
"Root Rot Pythium",
|
|
"Downy Mildew Peronospora",
|
|
"Bacterial Leaf Spot Xanthomonas",
|
|
"Apple Scab Venturia inaequalis",
|
|
"Fire Blight Erwinia amylovora",
|
|
"Blossom End Rot",
|
|
"Tomato Mosaic Virus",
|
|
"Rust Puccinia",
|
|
"Black Spot Diplocarpon rosae",
|
|
"Sooty Mold Capnodium",
|
|
"Clubroot Plasmodiophora brassicae",
|
|
"Anthracnose Colletotrichum",
|
|
];
|
|
console.log("Searching Wikipedia for disease images...\n");
|
|
for (const t of tests) {
|
|
await testOne(t);
|
|
}
|
|
}
|
|
|
|
main().catch(console.error);
|