forked from words/an-array-of-english-words
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mediawiki-query.js
41 lines (38 loc) · 1.1 KB
/
mediawiki-query.js
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
const request = require('request')
const merge = require('merge')
const Q = require('q')
const DEFAULT_QUERY_PARAMS = {
action: 'query',
format: 'json',
formatversion: 2
}
function WikiQuery (uri) {
if (!(this instanceof WikiQuery)) return new WikiQuery(uri)
this.uri = uri
}
WikiQuery.prototype.query = function (params) {
const fetchPages = continueObject => Q.Promise((resolve, reject, notify) => {
request({
method: 'POST',
uri: this.uri,
form: merge(true, DEFAULT_QUERY_PARAMS, params, continueObject)
},
(err, res, body) => {
if (err) {
reject(err)
}
if (res.statusCode !== 200) {
reject(body)
}
const json = JSON.parse(body)
if ((!json.query || !json.query.pages) && !json.batchcomplete) {
reject(body)
} else {
notify((json.query || {}).pages || [])
resolve(json.continue && fetchPages(json.continue))
}
})
})
return fetchPages({})
}
module.exports = WikiQuery