diff --git a/lib/plugins/trodrigues/index.js b/lib/plugins/trodrigues/index.js index e7927ef..b437f9f 100644 --- a/lib/plugins/trodrigues/index.js +++ b/lib/plugins/trodrigues/index.js @@ -1,18 +1,56 @@ 'use strict'; +var Nipple = require('nipple'); +var cheerio = require('cheerio'); + // Declare internals var internals = {}; // Defaults internals.defaults = {}; +function addInfoParameter(name, ths, response) { + ths.each(function (index, val) { + var data = val.children[0].data; + if(data && data.toLowerCase() == name){ + response[name] = ths.eq(index).parent().children().eq(1).text(); + } + }); +} + exports.register = function(plugin, options, next) { plugin.route({ method: 'GET', path: '/trodrigues', handler: function(request, reply) { - reply('why not zoidberg?'); + reply('Call /trodrigues/{Futurama character name}'); + } + }); + + plugin.route({ + method: 'GET', + path: '/trodrigues/{character}', + handler: function(request, reply) { + if(!request.params.character){ + return reply(new Error('No character specified')) + } + Nipple.get('https://en.wikipedia.org/wiki/'+request.params.character, function (err, res, html) { + if(!err) { + var response = {}; + var $ = cheerio.load(html); + var infobox = $('.infobox'); + var ths = infobox.find('th'); + response.name = ths.eq(0).text(); + + addInfoParameter('species', ths, response); + addInfoParameter('origin', ths, response); + addInfoParameter('relatives', ths, response); + addInfoParameter('occupation', ths, response); + + reply(response); + } + }); } }); diff --git a/package.json b/package.json index db8807c..92d1c80 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,8 @@ "dependencies": { "hapi": "^6.0.2", "lab": "^3.2.1", - "mustache": "^0.8.2" + "mustache": "^0.8.2", + "cheerio": "^0.17.0", + "nipple": "^2.5.3" } } diff --git a/server.js b/server.js index 60e7c7d..ba0f226 100644 --- a/server.js +++ b/server.js @@ -1,5 +1,6 @@ var Hapi = require('hapi'); var Participants = require('./participants.json'); +var good = require('good'); var server = Hapi.createServer('0.0.0.0', 8080); @@ -15,6 +16,21 @@ Participants.forEach(function (participant) { }); }); +server.pack.register({ + plugin: good, + options: { + subscribers: { + 'console': ['log', 'error', 'debug'] + } + } +}, function (err) { + if (err) { + console.log(err); + } else { + console.log('good has loaded'); + } +}); + server.start(function (err) { if (err) { @@ -23,4 +39,4 @@ server.start(function (err) { } console.log('Server started'); -}); \ No newline at end of file +});