3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-18 08:32:42 +01:00
dbot/modules/food/food.js

61 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-04-23 16:53:33 +02:00
/**
* Module name: Food
* Description: recipe search
*/
var _ = require('underscore')._,
request = require('request');
var food = function(dbot) {
this.commands = {
2015-04-23 16:56:13 +02:00
'~recipe': function(event) {
2015-04-23 16:53:33 +02:00
request.get('http://food2fork.com/api/search', {
'qs': {
'key': this.config.api_key,
2015-04-23 17:00:56 +02:00
'q': event.input[1]
2015-04-23 16:53:33 +02:00
},
'json': true
}, function(error, response, body) {
if(_.isObject(body) && _.has(body, 'recipes') && body.recipes.length > 0) {
var num = _.random(0, body.recipes.length - 1),
recipe = body.recipes[num];
event.reply(dbot.t('recipe', {
'title': recipe.title,
'link': recipe.source_url
}));
} else {
event.reply(dbot.t('no_recipe'));
}
}.bind(this));
}
};
2015-04-23 16:56:47 +02:00
this.commands['~recipe'].regex = [/^recipe (.+)$/, 2];
2015-04-23 17:11:45 +02:00
this.listener = function(event) {
2015-04-23 17:18:28 +02:00
var match = event.message.match(new RegExp(dbot.config.name + ': what should i (have|eat|make)\\??( for (dinner|lunch|breakfast))?\\??', 'i'));
2015-04-23 17:11:45 +02:00
if(match) {
2015-04-23 17:37:42 +02:00
var page = _.random(0, 200);
2015-04-23 17:11:45 +02:00
request.get('http://food2fork.com/api/search', {
'qs': {
'key': this.config.api_key,
2015-04-23 17:37:42 +02:00
'page': page
2015-04-23 17:11:45 +02:00
},
'json': true
}, function(error, response, body) {
if(_.isObject(body) && _.has(body, 'recipes') && body.recipes.length > 0) {
var num = _.random(0, body.recipes.length - 1),
recipe = body.recipes[num];
2015-04-23 17:17:38 +02:00
event.reply(event.user + ': You should make ' + recipe.title + '. See: ' + recipe.source_url);
2015-04-23 17:11:45 +02:00
}
}.bind(this));
}
2015-04-23 17:17:04 +02:00
}.bind(this);
2015-04-23 17:13:02 +02:00
this.on = 'PRIVMSG';
2015-04-23 16:53:33 +02:00
};
exports.fetch = function(dbot) {
return new food(dbot);
};