-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyles.css
More file actions
27 lines (27 loc) · 1.08 KB
/
Copy pathstyles.css
File metadata and controls
27 lines (27 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
export async function fetchCommonsImageMetadata(fileTitle) {
if (!fileTitle) return null;
const title = fileTitle.startsWith("File:") ? fileTitle : `File:${fileTitle}`;
const params = new URLSearchParams({
action: "query",
format: "json",
origin: "*",
prop: "imageinfo",
iiprop: "url|extmetadata",
titles: title
});
const res = await fetch(`https://commons.wikimedia.org/w/api.php?${params.toString()}`, { headers: { "User-Agent": "openARTlasAtlas/5.0" } });
if (!res.ok) throw new Error(`Commons failed: ${res.status}`);
const json = await res.json();
const page = Object.values(json.query?.pages || {})[0];
const info = page?.imageinfo?.[0];
if (!info) return null;
return {
url: info.url,
credit: stripHtml(info.extmetadata?.Artist?.value || info.extmetadata?.Credit?.value || "Wikimedia Commons"),
license: stripHtml(info.extmetadata?.LicenseShortName?.value || "Open license via Commons"),
sourceUrl: info.descriptionurl
};
}
function stripHtml(value) {
return String(value || "").replace(/<[^>]+>/g, "").replace(/\s+/g, " ").trim();
}