-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathWIKIARTICLESAROUND.gs
93 lines (93 loc) · 3.38 KB
/
WIKIARTICLESAROUND.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
/**
* Returns Wikipedia articles around a Wikipedia article or around a point.
*
* @param {string} articleOrPoint The Wikipedia article in the format "language:Article_Title" ("de:Berlin") or the point in the format "language:Latitude,Longitude" ("en:37.786971,-122.399677") to get articles around for.
* @param {number} radius The search radius in meters.
* @param {boolean=} opt_includeDistance Whether to include the distance in the output, defaults to false (optional).
* @param {string=} opt_namespaces Only include pages in these namespaces (optional).
* @return {Array<string>} The list of articles around the given article or point.
* @customfunction
*/
function WIKIARTICLESAROUND(articleOrPoint, radius, opt_includeDistance,
opt_namespaces) {
'use strict';
if (!articleOrPoint) {
return '';
}
var results = [];
try {
var language;
var rest;
var title;
var latitude;
var longitude;
if (articleOrPoint.indexOf(':') !== -1) {
language = articleOrPoint.split(/:(.+)?/)[0];
rest = articleOrPoint.split(/:(.+)?/)[1];
title = false;
latitude = false;
longitude = false;
if (/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/.test(rest)) {
latitude = rest.split(',')[0];
longitude = rest.split(',')[1];
} else {
title = rest;
}
} else {
language = 'en';
rest = articleOrPoint;
title = false;
latitude = false;
longitude = false;
if (/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/.test(rest)) {
latitude = rest.split(',')[0];
longitude = rest.split(',')[1];
} else {
title = rest;
}
}
if ((!title) && !(latitude && longitude)) {
return;
}
var url = 'https://' + language + '.wikipedia.org/w/api.php';
if (title) {
url += '?action=query' +
'&list=geosearch' +
'&format=xml' +
'&gslimit=max' +
'&gsradius=' + radius +
'&gspage=' + encodeURIComponent(title.replace(/\s/g, '_'));
} else {
url += '?action=query' +
'&list=geosearch' +
'&format=xml&gslimit=max' +
'&gsradius=' + radius +
'&gscoord=' + latitude + '%7C' + longitude;
}
url += '&gsnamespace=' + (opt_namespaces ?
encodeURIComponent(opt_namespaces) : '0');
var xml = UrlFetchApp.fetch(url, {
headers: {
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
}
}).getContentText();
var document = XmlService.parse(xml);
var entries = document.getRootElement().getChild('query')
.getChild('geosearch').getChildren('gs');
for (var i = 0; i < entries.length; i++) {
var title = entries[i].getAttribute('title').getValue();
var lat = entries[i].getAttribute('lat').getValue();
var lon = entries[i].getAttribute('lon').getValue();
if (opt_includeDistance) {
var dist = entries[i].getAttribute('dist').getValue();
results[i] = [title, lat, lon, dist];
} else {
results[i] = [title, lat, lon];
}
}
} catch (e) {
console.log(JSON.stringify(e));
}
return results.length > 0 ? results : '';
}