forked from annisaristya/slimme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnutritionix.js
305 lines (276 loc) · 11.4 KB
/
nutritionix.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Express webhook
const RapidAPI = require('rapidapi-connect');
const rapid = new RapidAPI("default-application_5acdd39de4b06ec3937ba3fd","16a6f4ee-836d-43d3-85d2-370fbebc324c");
const express = require('express');
const app = express();
const PORT = process.env.PORT || 5000
// db.connect();
db.any('create table if not exists consumer(id serial primary key, user_id varchar(200),timestamp timestamp,age int,weight int, height int,gender varchar(10),weightgoal varchar(100),consume int,activity varchar(100),exercise int);')
.then(data=>console.log(data))
.catch(error=>console.log(error))
const server = express();
server.use(bodyParser.urlencoded({
extended: true
}));
function getRequiredCalorie(weight, gender, height, age, activity, weightgoal){
const properWeight = Math.round(height * height * 0.0022);
console.log('proper: '+properWeight);
// BMR
let bmr;
if (gender == 'female'){
bmr = Math.round(655.1 + (9.563 * weight) + (1.85 * height) - (4.676 * age));
} else if (gender == 'male'){
bmr = Math.round(66.5 + (13.75 * weight) + (5.003 * height) - (6.755 * age));
}
console.log('bmr: '+bmr);
// Activity Factor
let activityFactor;
if (activity== 'a'){
activityFactor = 1.2;
} else if (activity== 'b'){
activityFactor = 1.375;
} else if (activity== 'c'){
activityFactor = 1.55;
} else if (activity== 'd'){
activityFactor = 1.725;
} else if (activity== 'e'){
activityFactor = 1.9;
}
console.log('activ: '+activityFactor);
let weightgoalFactor;
// Weight Goal
if (weightgoal == 'Stay the same weight'){
weightgoalFactor = 0;
} else if (weightgoal == 'Lose 0.25 kg per week'){
weightgoalFactor = 250;
} else if (weightgoal == 'Lose 0.5 kg per week') {
weightgoalFactor = 500;
}
console.log('weightgoal: '+weightgoalFactor);
const requiredCalorie = Math.round((bmr * activityFactor) - weightgoalFactor);
console.log('req calorie: '+requiredCalorie)
return requiredCalorie;
}
server.use(bodyParser.json());
server.get('/',(req,res)=>{
res.sendFile(path.join(__dirname+'/home.html'));
})
server.get('/db',(req,res)=>{
txt=[]
db.any('select * from consumer')
.then(data => {
res.json(data);
})
.catch(error => {
res.send(error)
})
})
server.get('/insert',(req,res)=>{
txt=[]
db.any("insert into consumer(user_id,timestamp,age,gender,weight,height,weightgoal,activity) values('test',now(),'44','female','64','164','Stay the same weight','a')")
.then(data => {
res.json(data);
})
.catch(error => {
res.send(error)
})
})
server.get('/about',(req,res)=>{
res.sendFile(path.join(__dirname + '/about.html'));
})
server.get('/user-profile',(req,res)=>{
res.sendFile(path.join(__dirname + '/user-profile.html'));
})
server.get('/track-calorie',(req,res)=>{
res.sendFile(path.join(__dirname + '/track-calorie.html'));
})
server.get('/getreq',(req,res)=>{
user_id=req.query.user_id
db.any("select * from consumer where user_id=$1 AND weight>0 AND height>0 AND age>0",[user_id])
.then(data=>res.json(getRequiredCalorie(data[0]['weight'], data[0]['gender'], data[0]['height'], data[0]['age'], data[0]['activity'], data[0]['weightgoal'])))
.catch(error=>res.json(error))
})
server.get('/getdaily',(req,res)=>{
user_id=req.query.user_id
req=0;
db.any("select * from consumer where user_id=$1 AND weight>0 AND height>0 AND age>0",[user_id])
.then(data=>{
req=getRequiredCalorie(data[0]['weight'], data[0]['gender'], data[0]['height'], data[0]['age'], data[0]['activity'], data[0]['weightgoal'])
})
.catch(err=>console.log(err))
db.any("select sum(consume) as cons,sum(exercise) as exc from consumer where user_id=$1 and date(timestamp)=date(now())",[user_id])
.then(data=>{
rem = parseFloat(req)-parseFloat(data[0]['cons'])+parseFloat(data[0]['exc'])
res.json({
"speech":"Your daily calorie "+req+" k-calories. Intake "+data[0]['cons']+" kCal. Burned "+data[0]['exc']+". Remaining is "+rem,
"displayText":"Your daily calorie "+req+" k-calories. Intake "+data[0]['cons']+" kCal. Burned "+data[0]['exc']+". Remaining is "+rem,
"source":"daily-calorie"
})
})
.catch(err=>console.log(err))
})
server.get('/get-calorie',(req,res)=>{
rapid.call('Nutritionix', 'getFoodsNutrients', {
'applicationId': '4c64f5c3',
'foodDescription': req.query.what,
'applicationSecret': 'ad4538d485233756557afd8aee6f530b'
}) .on('success', (payload)=>{
res.json(payload[0].foods[0].nf_calories);
}).on('error', (payload)=>{
res.send(payload);
});
})
server.get('/get-burncalorie',(req,res)=>{
rapid.call('Nutritionix', 'getCaloriesBurnedForExercises', {
'exerciseDescription': req.query.what,
'applicationSecret': 'ad4538d485233756557afd8aee6f530b',
'applicationId': '4c64f5c3'
}).on('success', (payload)=>{
res.json(payload[0]);
}).on('error', (payload)=>{
res.send(payload);
});
})
server.post('/',(req,res)=>{
var user_id = req.body.originalRequest.data.source.userId;
if(req.body.result.action=='get-weight'){
rsp = {
"speech":req.body.result.resolveQuery,
"displayText":req.body.result.resolveQuery,
"source":"get-weight"
}
db.any("insert into consumer(user_id,timestamp,weight) values($1,now(),$2)",[user_id,req.body.result.resolvedQuery.split(' ')[0]])
.then(res=>console.log(res))
.catch(err=>console.log(err))
res.json(rsp)
}
if(req.body.result.action=='get-gender'){
rsp = {
"speech":req.body.result.resolveQuery,
"displayText":req.body.result.resolveQuery,
"source":"get-gender"
}
db.any("update consumer set gender=$1 where user_id=$2",[req.body.result.resolvedQuery.toLowerCase(),user_id])
.then(res=>console.log(res))
.catch(err=>console.log(err))
res.json(rsp)
}
if(req.body.result.action=='get-height'){
rsp = {
"speech":req.body.result.resolveQuery,
"displayText":req.body.result.resolveQuery,
"source":"get-height"
}
db.any("update consumer set height=$1 where user_id=$2",[req.body.result.resolvedQuery.split(' ')[0],user_id])
.then(res=>console.log(res))
.catch(err=>console.log(err))
res.json(rsp)
}
if(req.body.result.action=='get-age'){
rsp = {
"speech":req.body.result.resolveQuery,
"displayText":req.body.result.resolveQuery,
"source":"get-age"
}
db.any("update consumer set age=$1 where user_id=$2",[req.body.result.resolvedQuery.split(' ')[0],user_id])
.then(res=>console.log(res))
.catch(err=>console.log(err))
res.json(rsp)
}
if(req.body.result.action=='get-activity'){
rsp = {
"speech":req.body.result.resolveQuery,
"displayText":req.body.result.resolveQuery,
"source":"get-activity"
}
db.any("update consumer set activity=$1 where user_id=$2",[req.body.result.resolvedQuery.toLowerCase(),user_id])
.then(res=>console.log(res))
.catch(err=>console.log(err))
res.json(rsp)
}
if(req.body.result.action=='get-weightgoal'){
db.any("update consumer set weightgoal=$1 where user_id=$2",[req.body.result.resolvedQuery,user_id])
.then(res=>console.log(res))
.catch(err=>console.log(err))
db.any("select * from consumer where user_id=$1 order by timestamp desc limit 1",[user_id])
.then(data=>{
rsp = {
"speech":"In order to reach your weight goal, you would have to consume "+getRequiredCalorie(data[0]['weight'], data[0]['gender'], data[0]['height'], data[0]['age'], data[0]['activity'], data[0]['weightgoal'])+" kcal a day, Do you want to start your diet today?",
"displayText":"In order to reach your weight goal, you would have to consume "+getRequiredCalorie(data[0]['weight'], data[0]['gender'], data[0]['height'], data[0]['age'], data[0]['activity'], data[0]['weightgoal'])+" kcal a day, Do you want to start your diet today?",
"source":"get-weightgoal"
};
res.json(rsp)
})
.catch(err=>console.log(err))
}
if(req.body.result.action=='daily-calorie'){
req=0;
db.any("select * from consumer where user_id=$1 AND weight>0 AND height>0 AND age>0",[user_id])
.then(data=>{
req=getRequiredCalorie(data[0]['weight'], data[0]['gender'], data[0]['height'], data[0]['age'], data[0]['activity'], data[0]['weightgoal'])
})
.catch(err=>console.log(err))
db.any("select sum(consume) as cons,sum(exercise) as exc from consumer where user_id=$1 and date(timestamp)=date(now())",[user_id])
.then(data=>{
rem = parseFloat(req)-parseFloat(data[0]['cons'])+parseFloat(data[0]['exc'])
rsp = {
"speech":"Your daily calorie needs is "+req+" k-calories. Intake "+data[0]['cons']+" kCal. Burned is "+data[0]['exc']+" kCal. Remaining is "+rem+" kCal.",
"displayText":"Your daily calorie needs is "+req+" k-calories. Intake "+data[0]['cons']+" kCal. Burned is "+data[0]['exc']+" kCal. Remaining is "+rem+" kCal.",
"source":"daily-calorie"
}
})
.catch(err=>console.log(err))
res.json(rsp)
}
if(req.body.result.action=='get-calorie'){
rapid.call('Nutritionix', 'getFoodsNutrients', {
'applicationId': '4c64f5c3',
'foodDescription': req.body.result.parameters.what,
'foodDescription': req.body.result.resolvedQuery,
'applicationSecret': 'ad4538d485233756557afd8aee6f530b'
}).on('success', (payload)=>{
res.json(payload[0]);
rsp = {
"speech":"You eat "+payload[0].foods[0].nf_calories+" k-calories.",
"displayText":"You eat "+payload[0].foods[0].nf_calories+" k-calories.",
"source":"get-calorie"
}
res.json(rsp)
}).on('error', (payload)=>{
res.send(payload);
});
if (req.body.result.action == 'get-exercise') {
rapid
.call('Nutritionix', 'getCaloriesBurnedForExercises', {
exerciseDescription: req.body.result.resolvedQuery,
applicationSecret: 'ad4538d485233756557afd8aee6f530b',
applicationId: '4c64f5c3'
})
.on('success', payload => {
rsp = {
speech:
'Yay! You just burn ' +
payload[0].exercises[0].nf_calories +
' k-calories. Keep it up!',
displayText:
'Yay! You just burn ' +
payload[0].exercises[0].nf_calories +
' k-calories. Keep it up!',
source: 'burn-calorie'
};
db.any(
'insert into consumer(user_id,timestamp,exercise) values($1,now(),$2)',
[user_id, payload[0].exercises[0].nf_calories]
)
.then(row => console.log(row))
.catch(err => console.log(err));
res.json(rsp);
})
.on('error', payload => {
res.send(payload);
});
}
});
server.listen(PORT, () => {
console.log('Server is up and running...' + PORT);
});