From 86c570a8c8c7b7ea15d345efa5e7a10d1e9526c2 Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 3 Mar 2016 17:31:42 +0000 Subject: [PATCH] ~wikipedia --- modules/wikipedia/wikipedia.js | 79 ++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 modules/wikipedia/wikipedia.js diff --git a/modules/wikipedia/wikipedia.js b/modules/wikipedia/wikipedia.js new file mode 100644 index 0000000..9f66b86 --- /dev/null +++ b/modules/wikipedia/wikipedia.js @@ -0,0 +1,79 @@ +/** + * 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); + }); + }, + + '~wikipedia': function(event) { + request.get('http://wikipedia.org/w/api.php', { + 'qs': { + 'action': 'opensearch', + 'search': event.input[1], + '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]; + this.commands['~wikipedia'].regex = [/^wikipedia (.+)/, 2]; + +}; + +exports.fetch = function(dbot) { + return new wikipedia(dbot); +};