mapcomplete/Logic/Web/Wikimedia.ts

56 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import { Utils } from "../../Utils"
export default class Wikimedia {
/**
* Recursively walks a wikimedia commons category in order to search for entries, which can be File: or Category: entries
* Returns (a promise of) a list of URLS
* @param categoryName The name of the wikimedia category
* @param maxLoad: the maximum amount of images to return
* @param continueParameter: if the page indicates that more pages should be loaded, this uses a token to continue. Provided by wikimedia
*/
2022-09-08 21:40:48 +02:00
public static async GetCategoryContents(
categoryName: string,
maxLoad = 10,
continueParameter: string = undefined
): Promise<string[]> {
if (categoryName === undefined || categoryName === null || categoryName === "") {
2022-09-08 21:40:48 +02:00
return []
}
if (!categoryName.startsWith("Category:")) {
2022-09-08 21:40:48 +02:00
categoryName = "Category:" + categoryName
}
2022-09-08 21:40:48 +02:00
let url =
"https://commons.wikimedia.org/w/api.php?" +
"action=query&list=categorymembers&format=json&" +
"&origin=*" +
2022-09-08 21:40:48 +02:00
"&cmtitle=" +
encodeURIComponent(categoryName)
if (continueParameter !== undefined) {
2022-09-08 21:40:48 +02:00
url = `${url}&cmcontinue=${continueParameter}`
}
const response = await Utils.downloadJson(url)
2022-09-08 21:40:48 +02:00
const members = response.query?.categorymembers ?? []
const imageOverview: string[] = members.map((member) => member.title)
if (response.continue === undefined) {
// We are done crawling through the category - no continuation in sight
2022-09-08 21:40:48 +02:00
return imageOverview
}
if (maxLoad - imageOverview.length <= 0) {
console.debug(`Recursive wikimedia category load stopped for ${categoryName}`)
2022-09-08 21:40:48 +02:00
return imageOverview
}
// We do have a continue token - let's load the next page
2022-09-08 21:40:48 +02:00
const recursive = await Wikimedia.GetCategoryContents(
categoryName,
maxLoad - imageOverview.length,
response.continue.cmcontinue
)
imageOverview.push(...recursive)
return imageOverview
}
2022-09-08 21:40:48 +02:00
}