Skip to content

Commit f3611fc

Browse files
author
Sourabh Choraria
authored
Adding Wiki Tools functions by Thomas Steiner
Source: https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
1 parent 86a7818 commit f3611fc

28 files changed

+1956
-0
lines changed

functions/GOOGLESUGGEST.gs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
2+
/**
3+
* Returns Google Suggest results for the given keyword.
4+
*
5+
* @param {string} query The query in the format "language:Query" ("de:Berlin") to get suggestions for.
6+
* @return {Array<string>} The list of suggestions.
7+
* @customfunction
8+
*/
9+
function GOOGLESUGGEST(query) {
10+
'use strict';
11+
if (!query) {
12+
return '';
13+
}
14+
var results = [];
15+
try {
16+
var language;
17+
var title;
18+
if (query.indexOf(':') !== -1) {
19+
language = query.split(/:(.+)?/)[0];
20+
title = query.split(/:(.+)?/)[1];
21+
} else {
22+
language = 'en';
23+
title = query;
24+
}
25+
if (!title) {
26+
return '';
27+
}
28+
var url = 'https://suggestqueries.google.com/complete/search' +
29+
'?output=toolbar' +
30+
'&hl=' + language +
31+
'&q=' + encodeURIComponent(title);
32+
var xml = UrlFetchApp.fetch(url, {
33+
headers: {
34+
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
35+
}
36+
}).getContentText();
37+
var document = XmlService.parse(xml);
38+
var entries = document.getRootElement().getChildren('CompleteSuggestion');
39+
for (var i = 0; i < entries.length; i++) {
40+
var text = entries[i].getChild('suggestion').getAttribute('data')
41+
.getValue();
42+
results[i] = text;
43+
}
44+
} catch (e) {
45+
console.log(JSON.stringify(e));
46+
}
47+
return results.length > 0 ? results : '';
48+
}

functions/WIKIARTICLESAROUND.gs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
2+
/**
3+
* Returns Wikipedia articles around a Wikipedia article or around a point.
4+
*
5+
* @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.
6+
* @param {number} radius The search radius in meters.
7+
* @param {boolean=} opt_includeDistance Whether to include the distance in the output, defaults to false (optional).
8+
* @param {string=} opt_namespaces Only include pages in these namespaces (optional).
9+
* @return {Array<string>} The list of articles around the given article or point.
10+
* @customfunction
11+
*/
12+
function WIKIARTICLESAROUND(articleOrPoint, radius, opt_includeDistance,
13+
opt_namespaces) {
14+
'use strict';
15+
if (!articleOrPoint) {
16+
return '';
17+
}
18+
var results = [];
19+
try {
20+
var language;
21+
var rest;
22+
var title;
23+
var latitude;
24+
var longitude;
25+
if (articleOrPoint.indexOf(':') !== -1) {
26+
language = articleOrPoint.split(/:(.+)?/)[0];
27+
rest = articleOrPoint.split(/:(.+)?/)[1];
28+
title = false;
29+
latitude = false;
30+
longitude = false;
31+
if (/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/.test(rest)) {
32+
latitude = rest.split(',')[0];
33+
longitude = rest.split(',')[1];
34+
} else {
35+
title = rest;
36+
}
37+
} else {
38+
language = 'en';
39+
rest = articleOrPoint;
40+
title = false;
41+
latitude = false;
42+
longitude = false;
43+
if (/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/.test(rest)) {
44+
latitude = rest.split(',')[0];
45+
longitude = rest.split(',')[1];
46+
} else {
47+
title = rest;
48+
}
49+
}
50+
if ((!title) && !(latitude && longitude)) {
51+
return;
52+
}
53+
var url = 'https://' + language + '.wikipedia.org/w/api.php';
54+
if (title) {
55+
url += '?action=query' +
56+
'&list=geosearch' +
57+
'&format=xml' +
58+
'&gslimit=max' +
59+
'&gsradius=' + radius +
60+
'&gspage=' + encodeURIComponent(title.replace(/\s/g, '_'));
61+
} else {
62+
url += '?action=query' +
63+
'&list=geosearch' +
64+
'&format=xml&gslimit=max' +
65+
'&gsradius=' + radius +
66+
'&gscoord=' + latitude + '%7C' + longitude;
67+
}
68+
url += '&gsnamespace=' + (opt_namespaces ?
69+
encodeURIComponent(opt_namespaces) : '0');
70+
var xml = UrlFetchApp.fetch(url, {
71+
headers: {
72+
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
73+
}
74+
}).getContentText();
75+
var document = XmlService.parse(xml);
76+
var entries = document.getRootElement().getChild('query')
77+
.getChild('geosearch').getChildren('gs');
78+
for (var i = 0; i < entries.length; i++) {
79+
var title = entries[i].getAttribute('title').getValue();
80+
var lat = entries[i].getAttribute('lat').getValue();
81+
var lon = entries[i].getAttribute('lon').getValue();
82+
if (opt_includeDistance) {
83+
var dist = entries[i].getAttribute('dist').getValue();
84+
results[i] = [title, lat, lon, dist];
85+
} else {
86+
results[i] = [title, lat, lon];
87+
}
88+
}
89+
} catch (e) {
90+
console.log(JSON.stringify(e));
91+
}
92+
return results.length > 0 ? results : '';
93+
}

functions/WIKICATEGORIES.gs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
2+
/**
3+
* Returns Wikipedia categories for a Wikipedia article.
4+
*
5+
* @param {string} article The Wikipedia article in the format "language:Article_Title" ("en:Berlin") to get categories for.
6+
* @return {Array<string>} The list of categories.
7+
* @customfunction
8+
*/
9+
function WIKICATEGORIES(article) {
10+
'use strict';
11+
if (!article) {
12+
return '';
13+
}
14+
var results = [];
15+
try {
16+
var language;
17+
var title;
18+
if (article.indexOf(':') !== -1) {
19+
language = article.split(/:(.+)?/)[0];
20+
title = article.split(/:(.+)?/)[1];
21+
} else {
22+
language = 'en';
23+
title = article;
24+
}
25+
if (!title) {
26+
return '';
27+
}
28+
var url = 'https://' + language + '.wikipedia.org/w/api.php' +
29+
'?action=query' +
30+
'&prop=categories' +
31+
'&format=xml' +
32+
'&cllimit=max' +
33+
'&titles=' + encodeURIComponent(title.replace(/\s/g, '_'));
34+
var xml = UrlFetchApp.fetch(url, {
35+
headers: {
36+
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
37+
}
38+
}).getContentText();
39+
var document = XmlService.parse(xml);
40+
var entries = document.getRootElement().getChild('query').getChild('pages')
41+
.getChild('page').getChild('categories').getChildren('cl');
42+
for (var i = 0; i < entries.length; i++) {
43+
var text = entries[i].getAttribute('title').getValue();
44+
results[i] = text;
45+
}
46+
} catch (e) {
47+
console.log(JSON.stringify(e));
48+
}
49+
return results.length > 0 ? results : '';
50+
}

functions/WIKICATEGORYMEMBERS.gs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
2+
/**
3+
* Returns Wikipedia category members for a Wikipedia category.
4+
*
5+
* @param {string} category The Wikipedia category in the format "language:Category_Title" ("en:Category:Visitor_attractions_in_Berlin") to get members for.
6+
* @param {string=} opt_namespaces Only include pages in these namespaces (optional).
7+
* @return {Array<string>} The list of category members.
8+
* @customfunction
9+
*/
10+
function WIKICATEGORYMEMBERS(category, opt_namespaces) {
11+
'use strict';
12+
if (!category) {
13+
return '';
14+
}
15+
var results = [];
16+
try {
17+
var language;
18+
var title;
19+
if ((category.match(/:/g) || []).length > 1) {
20+
language = category.split(/:(.+)?/)[0];
21+
title = category.split(/:(.+)?/)[1];
22+
} else {
23+
language = 'en';
24+
title = category;
25+
}
26+
if (!title) {
27+
return '';
28+
}
29+
var url = 'https://' + language + '.wikipedia.org/w/api.php' +
30+
'?action=query' +
31+
'&list=categorymembers' +
32+
'&cmlimit=max' +
33+
'&cmprop=title' +
34+
'&cmtype=subcat%7Cpage' +
35+
'&format=xml' +
36+
'&cmnamespace=' + (opt_namespaces ?
37+
encodeURIComponent(opt_namespaces) : '0') +
38+
'&cmtitle=' + encodeURIComponent(title.replace(/\s/g, '_'));
39+
var xml = UrlFetchApp.fetch(url, {
40+
headers: {
41+
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
42+
}
43+
}).getContentText();
44+
var document = XmlService.parse(xml);
45+
var entries = document.getRootElement().getChild('query')
46+
.getChild('categorymembers').getChildren('cm');
47+
for (var i = 0; i < entries.length; i++) {
48+
var text = entries[i].getAttribute('title').getValue();
49+
results[i] = text;
50+
}
51+
} catch (e) {
52+
console.log(JSON.stringify(e));
53+
}
54+
return results.length > 0 ? results : '';
55+
}

functions/WIKICOMMONSLINK.gs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
2+
/**
3+
* Returns the Wikimedia Commons link for a file.
4+
*
5+
* @param {string} fileName The Wikimedia Commons file name in the format "language:File_Name" ("en:Flag of Berlin.svg") to get the link for.
6+
* @return {string} The link of the Wikimedia Commons file.
7+
* @customfunction
8+
*/
9+
function WIKICOMMONSLINK(fileName) {
10+
'use strict';
11+
if (!fileName) {
12+
return '';
13+
}
14+
var results = [];
15+
try {
16+
var language;
17+
var title;
18+
if (fileName.indexOf(':') !== -1) {
19+
language = fileName.split(/:(.+)?/)[0];
20+
title = fileName.split(/:(.+)?/)[1];
21+
} else {
22+
language = 'en';
23+
title = fileName;
24+
}
25+
if (!title) {
26+
return '';
27+
}
28+
var url = 'https://' + language + '.wikipedia.org/w/api.php' +
29+
'?action=query' +
30+
'&prop=imageinfo' +
31+
'&iiprop=url' +
32+
'&format=xml' +
33+
'&titles=File:' + encodeURIComponent(title.replace(/\s/g, '_'));
34+
var xml = UrlFetchApp.fetch(url, {
35+
headers: {
36+
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
37+
}
38+
}).getContentText();
39+
var document = XmlService.parse(xml);
40+
var entry = document.getRootElement().getChild('query').getChild('pages')
41+
.getChild('page').getChild('imageinfo').getChild('ii');
42+
var fileUrl = entry.getAttribute('url').getValue();
43+
results[0] = fileUrl;
44+
} catch (e) {
45+
console.log(JSON.stringify(e));
46+
}
47+
return results.length > 0 ? results : '';
48+
}

functions/WIKIDATADESCRIPTIONS.gs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @author Thomas Steiner https://github.com/tomayac/wikipedia-tools-for-google-spreadsheets
2+
/**
3+
* Returns the descriptions for a Wikidata item.
4+
*
5+
* @param {string} qid The Wikidata item's qid to get the label for.
6+
* @param {Array<string>=} opt_targetLanguages The list of languages to limit the results to, or "all" (optional).
7+
* @return {Array<string>} The label.
8+
* @customfunction
9+
*/
10+
function WIKIDATADESCRIPTIONS(qid, opt_targetLanguages) {
11+
'use strict';
12+
if (!qid) {
13+
return '';
14+
}
15+
var results = [];
16+
try {
17+
opt_targetLanguages = opt_targetLanguages || [];
18+
opt_targetLanguages = Array.isArray(opt_targetLanguages) ?
19+
opt_targetLanguages : [opt_targetLanguages];
20+
if (opt_targetLanguages.length === 0) {
21+
opt_targetLanguages = ['en'];
22+
}
23+
if (opt_targetLanguages.length === 1 && opt_targetLanguages[0] === 'all') {
24+
opt_targetLanguages = [];
25+
}
26+
var url = 'https://www.wikidata.org/w/api.php' +
27+
'?format=json' +
28+
'&action=wbgetentities' +
29+
'&props=descriptions' +
30+
'&ids=' + qid +
31+
(opt_targetLanguages.length ?
32+
'&languages=' + opt_targetLanguages.join('%7C') : '');
33+
var json = JSON.parse(UrlFetchApp.fetch(url, {
34+
headers: {
35+
'X-User-Agent': 'Wikipedia Tools for Google Spreadsheets'
36+
}
37+
}).getContentText());
38+
var descriptions = json.entities[qid].descriptions;
39+
var availableLanguages = Object.keys(descriptions).sort();
40+
availableLanguages.forEach(function (language) {
41+
var description = descriptions[language].value;
42+
results.push([language, description]);
43+
});
44+
} catch (e) {
45+
console.log(JSON.stringify(e));
46+
}
47+
return results.length > 0 ? results : '';
48+
}

0 commit comments

Comments
 (0)