-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
74 lines (65 loc) · 2 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var express = require('express');
var jsdom = require('jsdom');
var recipes = [];
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
var port = process.env.PORT || 8080
app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});
app.get('/', function(req, res) {
res.render('index');
});
app.post('/', (req, res) => {
let ingredient = req.body.text;
if(! /^[a-z]+$/.test(ingredient)) {
res.send('I can\'t cook with this, pick something else!');
return;
}
request_recipes(ingredient, function(err, recipes) {
var random_recipe = recipes[Math.floor(Math.random() * recipes.length)];
let data = {
response_type: 'in_channel',
text: 'Check out this ' + ingredient + ' vegan recipe!',
attachments:[
{
title: random_recipe.title,
title_link: random_recipe.url,
image_url: random_recipe.picture
}
]};
res.json(data);
});
});
function request_recipes(ingredient, callback) {
var url = 'https://www.bbcgoodfood.com/search/recipes?query=Vegan+' + ingredient;
recipes = [];
jsdom.env({
url: url,
scripts: ['http://code.jquery.com/jquery.js'],
done: function (errors, window) {
get_recipes(errors, window);
callback(null, recipes);
}
});
}
function get_recipes(errors, window) {
var $ = window.$;
console.log("Test recipe title");
$(".teaser-item__image").children("a").each(function() {
var recipe = {
"title": $(this).children("img").attr("alt"),
"url": "https://www.bbcgoodfood.com" + $(this).attr("href"),
"picture": "https:" + $(this).children("img").attr("src")
};
console.log(" -", recipe["title"]);
console.log("url:", recipe["url"]);
console.log("recipe_picture:", recipe["picture"]);
recipes.push(recipe);
});
console.log("Recipes:", recipes);
}