3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-18 00:22:35 +01:00
dbot/modules/wikipedia/wikipedia.js

132 lines
4.1 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 = {
2016-03-23 16:36:15 +01:00
'getSentence': function(term, random, cb) {
2016-03-03 18:31:42 +01:00
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) {
2016-03-22 22:31:59 +01:00
body = body.query.pages
for(var prop in body) {
break;
}
2016-03-22 23:30:06 +01:00
2016-03-22 22:31:59 +01:00
body = body[prop].revisions[0]['*'];
2016-03-22 23:54:17 +01:00
var oBody = body;
2016-03-03 18:31:42 +01:00
2016-03-22 23:30:06 +01:00
var redirect = body.match(/#redirect \[\[(.+)\]\]/i);
if(redirect) {
2016-03-23 16:36:15 +01:00
return this.api.getSentence(redirect[1], random, cb);
2016-03-22 23:30:06 +01:00
}
2016-03-22 23:47:21 +01:00
var refer = body.match(/may refer to:/i);
if(refer) {
var links = body.match(/\[\[(.+)\]\]/g);
2016-03-23 16:36:15 +01:00
return this.api.getSentence(links[_.random(0,links.length-1)], random, cb);
2016-03-22 23:47:21 +01:00
}
2016-03-03 18:31:42 +01:00
body = body.replace(/=(.+)=/g,'');
body = body.replace(/\t/g,'');
body = body.replace(/\{(.+)\}/g,'');
body = body.replace(/(\[|\])/g,'');
body = body.replace(/(\(|\))/g,'');
2016-03-22 22:31:59 +01:00
body = body.replace(/\*\s?/g,'');
2016-03-23 16:36:15 +01:00
body = body.replace(/<.+?>.+<.+?>/g,'');
2016-03-22 22:31:59 +01:00
body = body.replace(/<.+?>/g,'');
2016-03-23 16:36:15 +01:00
body = body.replace(/(\w+)\|([^ ^, ^\.]+)/g,'$2');
body = body.replace(/'+/g, '\'');
2016-03-03 18:31:42 +01:00
body = body.split('\n');
2016-03-23 16:36:15 +01:00
if(random == false) {
var newBody = [];
_.each(body, function(line) {
console.log(line.split('. '));
newBody = _.union(newBody, line.split('. '));
});
body = newBody;
}
2016-03-22 22:31:59 +01:00
body = _.filter(body, function(line) {
var spaces = line.match(/\s/g);
2016-03-31 23:59:31 +02:00
return line != '' && !line.match(/{|}/) && !line.match(/^\s+$/) && !line.match(/^!/) && !line.match(/^#/) && !line.match(/^File:/) && !line.match(/^Image:/) && !line.match(/^Category:/) && !line.match(/http:\/\//) && !line.match(/^\s+?\|/) && !line.match(/:$/) && spaces && spaces.length > 5 && spaces.length < 100;
2016-03-22 22:31:59 +01:00
});
2016-03-23 16:36:15 +01:00
if(random == true) {
var sentence = body[_.random(0, body.length -1)];
} else {
var sentence = body[0];
}
2016-03-03 18:31:42 +01:00
2016-03-22 23:54:17 +01:00
if(_.isUndefined(sentence)) {
var links = oBody.match(/\[\[(.+)\]\]/g);
2016-03-23 16:36:15 +01:00
return this.api.getSentence(links[_.random(0,links.length-1)], random, cb);
}
var w = sentence.split(' ');
if(w.length > 50) {
sentence = w.slice(0, 60).join(' ') + '...';
2016-03-22 23:54:17 +01:00
}
2016-03-03 18:31:42 +01:00
cb(sentence);
2016-03-22 23:30:06 +01:00
}.bind(this));
2016-03-03 18:31:42 +01:00
}
2016-03-22 23:30:06 +01:00
}.bind(this));
2016-03-03 18:31:42 +01:00
}
};
this.commands = {
'~lol': function(event) {
2016-03-23 16:36:15 +01:00
this.api.getSentence(event.input[1], true, function(sentence) {
event.reply(sentence);
});
},
'~summary': function(event) {
this.api.getSentence(event.input[1], false, function(sentence) {
2016-03-03 18:31:42 +01:00
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-23 16:36:15 +01:00
this.commands['~summary'].regex = [/^summary ([\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);
};