-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwikimediaApi.js
More file actions
83 lines (72 loc) · 2.24 KB
/
wikimediaApi.js
File metadata and controls
83 lines (72 loc) · 2.24 KB
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
const https = require('https');
const log = require('./log');
const WIKI_ENDPOINT = 'https://wiki.protospace.ca/'
function extractNameAndId(title) {
log.debug(`title: ${title}`);
result = (new RegExp(/(.*)\ ID:\s*(\d+)/)).exec(title);
log.debug(`result: ${result}`);
return {
name: result[1],
id: result[2]
}
}
function isRedirect(wikitext) {
return wikitext.includes('{{id/after-redirect}}');
}
function extractRedirect(wikitext) {
// regexr.com/6acsk
// the first group match here is for the Page we redirect to
let regex = new RegExp(/REDIRECT \[\[(.*)\]\]\{\{id\/after-redirect\}\}/);
// TODO: what do in case of no match?
// get the name of the page we are redirecting to
// TODO: can we use a named group or something instead of blindly indexing?
// https://www.bennadel.com/blog/3508-playing-with-regexp-named-capture-groups-in-node-10.htm
return regex.exec(wikitext)[1]
}
function getPage(name, callback) {
const API = WIKI_ENDPOINT + 'api.php';
// TODO: string builder? what is risk of injection attack?
// DO this: https://www.valentinog.com/blog/url/
let request = API + '?action=parse&prop=wikitext&format=json&page=' + encodeURIComponent(name)
log.debug(`requesting: ${request}`);
https.get(request, res => {
res.setEncoding('utf-8');
let body = '';
res.on('data', data => {
body += data;
});
res.on('end', (err) => {
if (err) {
callback(null, err);
return
}
raw = JSON.parse(body);
if ('error' in raw) {
message = raw.error.info
log.warn(message + ": " + name);
callback(null, message);
return
}
resp = raw.parse;
// TODO: error handling if property doesn't exist
wikitext = resp.wikitext['*'];
// protospace wiki pages for tools often have redirects
if (isRedirect(wikitext)) {
redirect_page = extractRedirect(wikitext)
log.debug(`CALL REDIRECT ${redirect_page}`);
getPage(redirect_page, callback);
return
}
log.debug(`resp: ${JSON.stringify(resp)}`);
// otherwise, pass the response data back
callback(resp, err);
})
});
}
module.exports = {
getPage,
extractNameAndId,
isRedirect,
extractRedirect,
WIKI_ENDPOINT
}