-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathWIKIDATALOOKUP.gs
33 lines (33 loc) · 1.15 KB
/
WIKIDATALOOKUP.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
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
/**
* Returns the Wikidata qid of the given identifier and property.
*
* Internally, this function invokes a haswbstatement query against the Wikidata API.
*
* @param {string} property The Wikidata property (such as "P298").
* @param {string} identifier The identifier (value) to lookup (such as "AUT").
* @return {string} The Wikidata qid.
* @customfunction
*/
function WIKIDATALOOKUP(property, identifier) {
'use strict';
var results = [];
try {
var url = 'https://www.wikidata.org/w/api.php' +
'?action=query' +
'&format=json' +
'&formatversion=2' +
'&list=search' +
'&ppprop=wikibase_item' +
'&srsearch=haswbstatement:' + property + '=' + encodeURIComponent(identifier);
var json = JSON.parse(UrlFetchApp.fetch(url, {
headers: {
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
}
}).getContentText());
results[0] = json.query.search[0].title;
} catch (e) {
console.log(JSON.stringify(e));
}
return results.length > 0 ? results : '';
}