-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathWIKIGEOCOORDINATES.gs
51 lines (51 loc) · 1.64 KB
/
WIKIGEOCOORDINATES.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
49
50
51
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
/**
* Returns Wikipedia geocoordinates for a Wikipedia article.
*
* @param {string} article The Wikipedia article in the format "language:Article_Title" ("de:Berlin") to get geocoordinates for.
* @return {Array<number>} The latitude and longitude.
* @customfunction
*/
function WIKIGEOCOORDINATES(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 = 'en';
title = article;
}
if (!title) {
return '';
}
var url = 'https://' + language + '.wikipedia.org/w/api.php' +
'?action=query' +
'&prop=coordinates' +
'&format=xml' +
'&colimit=max' +
'&coprimary=primary' +
'&titles=' + encodeURIComponent(title.replace(/\s/g, '_'));
var xml = UrlFetchApp.fetch(url, {
headers: {
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
}
}).getContentText();
var document = XmlService.parse(xml);
var coordinates = document.getRootElement().getChild('query')
.getChild('pages').getChild('page').getChild('coordinates')
.getChild('co');
var latitude = coordinates.getAttribute('lat').getValue();
var longitude = coordinates.getAttribute('lon').getValue();
results = [[latitude, longitude]];
} catch (e) {
console.log(JSON.stringify(e));
}
return results.length > 0 ? results : '';
}