-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathWIKIDATALABELS.gs
48 lines (48 loc) · 1.66 KB
/
WIKIDATALABELS.gs
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
/**
* Returns the labels for a Wikidata item.
*
* @param {string} qid The Wikidata item's qid to get the labels for.
* @param {Array<string>=} opt_targetLanguages The list of languages to limit the results to, or "all" (optional).
* @return {Array<string>} The labels.
* @customfunction
*/
function WIKIDATALABELS(qid, opt_targetLanguages) {
'use strict';
if (!qid) {
return '';
}
var results = [];
try {
opt_targetLanguages = opt_targetLanguages || [];
opt_targetLanguages = Array.isArray(opt_targetLanguages) ?
opt_targetLanguages : [opt_targetLanguages];
if (opt_targetLanguages.length === 0) {
opt_targetLanguages = ['en'];
}
if (opt_targetLanguages.length === 1 && opt_targetLanguages[0] === 'all') {
opt_targetLanguages = [];
}
var url = 'https://www.wikidata.org/w/api.php' +
'?format=json' +
'&action=wbgetentities' +
'&props=labels' +
'&ids=' + qid +
(opt_targetLanguages.length ?
'&languages=' + opt_targetLanguages.join('%7C') : '');
var json = JSON.parse(UrlFetchApp.fetch(url, {
headers: {
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
}
}).getContentText());
var labels = json.entities[qid].labels;
var availableLanguages = Object.keys(labels).sort();
availableLanguages.forEach(function (language) {
var label = labels[language].value;
results.push([language, label]);
});
} catch (e) {
console.log(JSON.stringify(e));
}
return results.length > 0 ? results : '';
}