-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode_helper.js
61 lines (54 loc) · 2.04 KB
/
node_helper.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
/* Magic Mirror
* Module: MMM-Recipe
*
* By Cowboysdude
*
*/
const NodeHelper = require('node_helper');
const request = require('request');
module.exports = NodeHelper.create({
start: function() {
console.log('starting Node_helper for ' + this.name + " " + this.data);
},
getRecipe: function(url) {
request({
url: "http://www.themealdb.com/api/json/v1/1/random.php",
method: 'GET'
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
var result = JSON.parse(body),
meal = result.meals[0];
function youtube_parser(url) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return (match && match[7].length == 11) ? match[7] : false;
}
var recipe = {
recipeName: meal.strMeal,
instruction: meal.strInstructions,
videoId: youtube_parser(meal.strYoutube),
video: meal.strYoutube,
thumb: meal.strMealThumb,
nation: meal.strArea,
category: meal.strCategory,
ingredients: []
};
for (var i = 1; i <= 54; i++) {
if (!meal['strIngredient' + i]) {
break;
}
recipe.ingredients.push({
ingredient: meal['strMeasure' + i] + " " + meal['strIngredient' + i]
});
}
this.sendSocketNotification('RECIPE_RESULT', recipe);
}
});
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_RECIPE') {
this.getRecipe(payload);
}
}
});