User:Fuzheado/Google Sheets Wikidata scripts

Paste the below into your custom scripts editor in Google Sheets for more functions

// Wikipedia Functions to: // - Getting the revid/oldid for a given article // - Getting the ORES/Revscore fora given revid // - Getting the Wikidata QID for a Wikipedia article, or return blank // // Based on https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets

// Make requests traceable in case of any issues var HEADERS = {headers: { 'X-User-Agent': 'Wikipedia Wiki List Tool' }};

// Set default language to 'en' for simpler method chaining var DEFAULT_LANGUAGE = 'en';

/** ** Given a Wikipedia article, get the latest revid, as this is used for ORES **/

function WIKIREVID(article) { 'use strict'; if (!article) { return ''; } var results = []; try { var language; var title; if (article.indexOf(':') !== -1) { language = article.split(/:(.+)?/)[0]; title = article.split(/:(.+)?/)[1]; } else { language = DEFAULT_LANGUAGE; title = article; }   if (!title) { return ''; }   var url = 'https://en.wikipedia.org/api/rest_v1/page/title/' + encodeURIComponent(title); var json = JSON.parse(UrlFetchApp.fetch(url, HEADERS).getContentText); // results[0] = Object.keys(json.items[0])[2]; results[0] = json.items[0].rev; Utilities.sleep(200); // SLEEP } catch (e) { // no-op } return results.length > 0 ? results : ''; }

/** ** Given a revid, calculate the ORES score using the wp10, article quality rating **/

function WIKIORES(revid, targetwiki) { 'use strict'; if (!revid) { return ''; } if (!targetwiki) { return ''; } var results = []; try { var url = 'https://ores.wikimedia.org/v3/scores/' + targetwiki + '/' + revid + '/' + 'wp10'; var json = JSON.parse(UrlFetchApp.fetch(url, HEADERS).getContentText); results[0] = json.enwiki.scores[revid].wp10.score.prediction; Utilities.sleep(200); // SLEEP } catch (e) { // no-op } return results.length > 0 ? results : ''; }

/** * FIXED bug in WIKIDATAQID * * Returns the Wikidata qid of the corresponding Wikidata item for an article, or nothing. Useful to prevent useless cascading API calls * * @param {string} article The article in the format "language:Query" ("de:Berlin") to get the Wikidata qid for. * @return {string} The Wikidata qid. * @customfunction */ function WIKIDATAQIDORBLANK(article) { 'use strict'; if (!article) { return ''; } var results = []; try { var language; var title; if (article.indexOf(':') !== -1) { language = article.split(/:(.+)?/)[0]; title = article.split(/:(.+)?/)[1]; } else { language = DEFAULT_LANGUAGE; title = article; }   if (!title) { return ''; }   var url = 'https://www.wikidata.org/w/api.php' + '?action=wbgetentities' + '&sites=' + language + 'wiki' + '&format=json' + '&props=' + // Empty on purpose '&titles=' + encodeURIComponent(title); var json = JSON.parse(UrlFetchApp.fetch(url, HEADERS).getContentText); results[0] = Object.keys(json.entities)[0]; Utilities.sleep(200); // SLEEP } catch (e) { // no-op } var minusone = "-1"; // return results.length > 0 ? results : ''; return results.indexOf(minusone) !== -1 ? '' : results ;

}