-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathWIKIOUTBOUNDLINKS.gs
53 lines (53 loc) · 1.76 KB
/
WIKIOUTBOUNDLINKS.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
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
/**
* Returns Wikipedia outbound links for a Wikipedia article.
*
* @param {string} article The Wikipedia article in the format "language:Article_Title" ("de:Berlin") to get outbound links for.
* @param {string=} opt_namespaces Only include pages in these namespaces (optional).
* @return {Array<string>} The list of outbound links.
* @customfunction
*/
function WIKIOUTBOUNDLINKS(article, opt_namespaces) {
'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=links' +
'&plnamespace=' + (opt_namespaces ?
encodeURIComponent(opt_namespaces) : '0') +
'&format=xml' +
'&pllimit=max' +
'&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 entries = document.getRootElement().getChild('query').getChild('pages')
.getChild('page').getChild('links').getChildren('pl');
for (var i = 0; i < entries.length; i++) {
var text = entries[i].getAttribute('title').getValue();
results[i] = text;
}
} catch (e) {
console.log(JSON.stringify(e));
}
return results.length > 0 ? results : '';
}