Skip to content

Instantly share code, notes, and snippets.

@01fade
Last active October 1, 2021 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 01fade/28abac187258374d8df35345bd8fe58a to your computer and use it in GitHub Desktop.
Save 01fade/28abac187258374d8df35345bd8fe58a to your computer and use it in GitHub Desktop.
Use node to get bulk attributions (title, author, license) from Commons API
const fetch = require('node-fetch');
var fs = require('fs');
// just get the last part of Commons url from browser
// there might be a limit, if so build a loop
var list = [
"OOjs_UI_icon_language-ltr.svg",
"Flight_of_the_Bumblebee_on_Pipe_Organ_Pedals.webm",
"Happy_20th_birthday,_Wikipedia!_%E2%80%93_International_(subtitled)_version.webm"
];
var result = [];
function writeJson(jsonContent, outpath) {
fs.writeFile(__dirname + outpath + ".json", jsonContent, 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File.");
return console.log(err);
}
console.log(outpath + ": JSON file has been saved.");
});
}
function req(index) {
var filename = list[index];
(async () => {
try {
const res = await fetch('https://commons.wikimedia.org/w/api.php?action=query&prop=imageinfo&format=json&formatversion=2&iiprop=extmetadata&iilimit=10&titles=File:' + filename)
const json = await res.json()
console.log("---");
console.log(json.query.pages[0].title);
console.log(json.query.pages[0].imageinfo[0].extmetadata.Artist.value);
console.log(json.query.pages[0].imageinfo[0].extmetadata.LicenseShortName.value);
console.log("(https://commons.wikimedia.org/wiki/File:" + filename + ")");
console.log("---");
if (index + 1 < list.length) {
await new Promise(resolve => setTimeout(resolve, 1000));
req(index + 1);
}
} catch (error) {
console.log("[err]", filename, error.res);
if (index + 1 < list.length) {
await new Promise(resolve => setTimeout(resolve, 2000));
req(index);
}
}
})();
}
req(0);
// result might contain html, use something like https://www.textfixer.com/html/html-to-text.php to get just text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment