Fix: fix wikimedia-commons image attribution; move code for this out of 'DownloadCommons'

This commit is contained in:
Pieter Vander Vennet 2025-07-05 20:54:17 +02:00
parent d8b86703c6
commit eab010a13e
5 changed files with 222 additions and 227 deletions

View file

@ -156,7 +156,7 @@ export class WikimediaImageProvider extends ImageProvider {
value = WikimediaImageProvider.removeCommonsPrefix(value)
if (value.startsWith("Category:")) {
const urls = await Wikimedia.GetCategoryContents(value)
const urls = await Wikimedia.getCategoryContents(value)
return urls
.filter((url) => url.startsWith("File:"))
.map((image) => this.UrlForImage(image))
@ -173,59 +173,15 @@ export class WikimediaImageProvider extends ImageProvider {
}
public async DownloadAttribution(img: { id: string }): Promise<LicenseInfo> {
const filename = "File:" + WikimediaImageProvider.extractFileName(img.id)
console.log("Downloading attribution for", filename, img.id)
if (filename === "") {
return undefined
const l = await Wikimedia.getLicenseFor("https://commons.wikimedia.org", img.id)
return <LicenseInfo>{
title: img.id,
artist: l.authors.join(", "),
licenseShortName : l.license,
informationLocation: {href:
this.visitUrl(img)
}
}
const url =
"https://en.wikipedia.org/w/" +
"api.php?action=query&prop=imageinfo&iiprop=extmetadata&" +
"titles=" +
filename +
"&format=json&origin=*"
const data = await Utils.downloadJsonCached<{
query: { pages: { title: string; imageinfo: { extmetadata }[] }[] }
}>(url, 365 * 24 * 60 * 60)
const licenseInfo = new LicenseInfo()
const pages = data.query.pages
/*jup, a literal "-1" in an object, not a list!*/
let pageInfo = pages["-1"]
if (Array.isArray(pages)) {
pageInfo = pages.at(-1)
}
if (pageInfo === undefined) {
console.warn("No attribution found for wikimedia image:", filename)
return undefined
}
const license = (pageInfo.imageinfo ?? [])[0]?.extmetadata
if (license === undefined) {
console.warn(
"The file",
filename,
"has no usable metedata or license attached... Please fix the license info file yourself!"
)
return undefined
}
let title = WikimediaImageProvider.makeCanonical(pageInfo.title)
if (title.endsWith(".jpg") || title.endsWith(".png")) {
title = title.substring(0, title.length - 4)
}
licenseInfo.title = title
licenseInfo.artist = license.Artist?.value
licenseInfo.license = license.License?.value
licenseInfo.copyrighted = license.Copyrighted?.value
licenseInfo.attributionRequired = license.AttributionRequired?.value
licenseInfo.usageTerms = license.UsageTerms?.value
licenseInfo.licenseShortName = license.LicenseShortName?.value
licenseInfo.credit = license.Credit?.value
licenseInfo.description = license.ImageDescription?.value
licenseInfo.informationLocation = new URL("https://en.wikipedia.org/wiki/" + pageInfo.title)
return licenseInfo
}
private UrlForImage(image: string, key?: string, value?: string): ProvidedImage {
@ -248,7 +204,7 @@ export class WikimediaImageProvider extends ImageProvider {
return undefined
}
visitUrl(): string | undefined {
return undefined
visitUrl(img: ProvidedImage): string | undefined {
return "https://commons.wikimedia.org/wiki/"+img.id
}
}