dbot/modules/wikipedia/wikipedia.js

80 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-03-03 18:31:42 +01:00
/**
* Module name: Wikipedia
*/
var _ = require('underscore')._,
request = require('request');
var wikipedia = function(dbot) {
this.api = {
'randomSentence': function(term, cb) {
request.get('https://en.wikipedia.org/w/api.php', {
'qs': {
'action': 'opensearch',
'search': term
},
'json': true
}, function(error, response, body) {
if(body && body[1].length != 0) {
request.get('https://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&&titles='+body[1][0], {'json': true}, function(error, response, body) {
body = body.query.pages
for(var prop in body) {
break;
}
body = body[prop].revisions[0]['*'];
body = body.replace(/=(.+)=/g,'');
body = body.replace(/\t/g,'');
body = body.replace(/\{(.+)\}/g,'');
body = body.replace(/(\[|\])/g,'');
body = body.replace(/(\(|\))/g,'');
console.log(body);
body = body.split('\n');
var sentence = body[_.random(0, body.length -1)];
cb(sentence);
});
}
});
}
};
this.commands = {
'~lol': function(event) {
this.api.randomSentence(event.input[1], function(sentence) {
event.reply(sentence);
});
},
2016-03-03 18:36:21 +01:00
'~w': function(event) {
2016-03-03 18:31:42 +01:00
request.get('http://wikipedia.org/w/api.php', {
'qs': {
'action': 'opensearch',
2016-03-03 18:41:17 +01:00
'search': event.input[1].replace(/\s/,'_'),
2016-03-03 18:31:42 +01:00
'limit': 1,
'namespace': 0,
'format': 'json'
},
'json': true
}, function(err, res, body) {
if(!err && body[1].length !== 0) {
event.reply(event.input[1] + ': https://wikipedia.org/wiki/'+body[1][0].replace(/\s/g, '_'));
} else {
event.reply(event.input[1] + ' not found.');
}
});
}
};
this.commands['~lol'].regex = [/^lol ([\d\w\s-]*)/, 2];
2016-03-03 18:39:08 +01:00
this.commands['~w'].regex = [/^w (.+)/, 2];
2016-03-03 18:31:42 +01:00
};
exports.fetch = function(dbot) {
return new wikipedia(dbot);
};