-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_.js
636 lines (515 loc) · 17.7 KB
/
app_.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
const express = require('express');
const newsRoute = require('./src/routes/newsRoute');
const moviesRoute = require('./src/routes/moviesRoute');
const curerencyRoute = require('./src/routes/currencyRoute');
const weatherRoute = require('./src/routes/weatherRoute');
const musicRoute = require('./src/routes/musicRoute');
const foodCalories = require('./src/routes/foodCalorieRoute');
const googleTranslate = require('./src/routes/googleTranslateRoute');
const airportSearch = require('./src/routes/airportSearchRoute');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var neo4j = require('neo4j-driver');
var http = require("http");
var Parse = require('parse');
var URL = require('url');
var fs = require('fs');
// Initialize app
const port = 3000;
var app = express();
//view engine
app.set('views', path.join(__dirname, "views"));
app.set('view engine', 'ejs');
// Middleware to handle json data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
var driver = neo4j.driver('bolt://localhost:11002', neo4j.auth.basic('neo4j', 'password'));
var session = driver.session();
var nodeType = 'parameter';
var labelArr = [];
var featureArr = [];
class Output {
constructor(answer) {
this.API_answer = answer;
}
}
session
.run('Match(n {type: $type}) Return n', { type: nodeType })
.then(function(result) {
result.records.forEach(function(record, feature) {
labelArr.push({
label: record._fields[0].labels
});
featureArr.push({
feature: record._fields[0].properties.feature
})
})
labelArr.forEach(function(label) {
console.log(label);
})
})
.then(function(feature) {
if (featureArr[0]['feature'] == 'music') {
try {
return music_feature(labelArr, featureArr);
} catch {
console.log('We dont have enough data about the artist');
return -1;
}
} else if (featureArr[0]['feature'] == 'calorie') {
try {
return (calorie_feature(labelArr, featureArr));
} catch {
console.log('we dont have enough data about this food');
}
} else if (featureArr[0]['feature'] == 'translate') {
try {
return (translate_feature(labelArr, featureArr));
} catch {
console.log('we dont have enough data about this language');
}
} else if (featureArr[0]['feature'] == 'airport') {
try {
return (airport_feature(labelArr, featureArr));
} catch {
console.log('we dont have enough data about this language');
}
} else if (featureArr[0]['feature'] == 'Corona') {
try {
return (corona_feature(labelArr, featureArr));
} catch {
console.log('we dont have enough data about this country');
}
} else if (featureArr[0]['feature'] == 'calculator') {
try {
return (calculator_feature(labelArr, featureArr));
} catch {
console.log('we dont have enough result');
}
} else if (featureArr[0]['feature'] == 'news') {
try {
return (calculator_feature(labelArr, featureArr));
} catch {
console.log('we dont have enough news');
}
} else if (featureArr[0]['feature'] == 'movies') {
try {
return (calculator_feature(labelArr, featureArr));
} catch {
console.log('we dont have enough data about this movie');
}
} else {
return -1;
}
// else if (featureArr[0]['feature'] == 'music'){
// }
labelArr = []
featureArr = []
})
.catch(function(err) {
console.log("error in reading the Database " + err);
process.exit(1)
})
.then(() => {
// Close the Session
return session.close();
})
.then(() => {
// Close the Driver
return driver.close();
});
function parse_music(Object) {
try {
random_track_index = Math.floor(Math.random() * Object['data'].length + 1);
console.log(Object['data'][random_track_index]['title']);
console.log(Object['data'][random_track_index]['link']);
} catch {
console.log('Sorry, e cant find enough data for the artist');
}
var obj = {
name: '',
url: ''
};
obj.name = Object['data'][random_track_index]['title'];
obj.url = Object['data'][random_track_index]['link'];
var data = JSON.stringify(obj);
fs.writeFileSync('apiresp.json', data, finished);
function finished(err) {
console.log('ALL SET');
}
}
function music_feature(labelArr, featureArr) {
// Asuming that our service is running on https://www.ourservice.com
// This is how we can access our music-search feature in any node.js app
// Url as in documentation file
var par = labelArr[0]['label'][0];
try {
var url = `http://localhost:3000/music/search?q=${par}`.replace(/\s/g, '');
} catch {
return -1;
}
http.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var jsonObj = JSON.parse(body);
parse_music(jsonObj);
process.exit(1)
// Now data is ready in json obj use it whatever you want
});
}).on('error', function(e) {
console.log("error in API: ", e);
process.exit(1)
});
}
function parse_calorie(Object) {
try {
random_food_index = Math.floor(Math.random() * Object.length + 1);
console.log(Object[random_food_index]['id']);
} catch {
console.log('Sorry, e cant find enough data for the food');
}
var obj = {
energyCal: 0,
suger: 0,
fat_sat: 0,
carbs: 0,
protien: 0,
cholestrol: 0,
iron: 0,
sodium: 0,
pottassium: 0,
};
obj.energyCal = Object[random_food_index]['energy_kcal'];
obj.suger = Object[random_food_index]['sugar_tot'];
obj.fat_sat = Object[random_food_index]['fa_sat'];
obj.carbs = Object[random_food_index]['carbohydrt'];
obj.protien = Object[random_food_index]['protien'];
obj.cholestrol = Object[random_food_index]['cholestrl'];
obj.iron = Object[random_food_index]['iron'];
obj.sodium = Object[random_food_index]['sodium'];
obj.pottassium = Object[random_food_index]['potassium'];
var data = JSON.stringify(obj);
fs.writeFileSync('apiresp.json', data, finished);
function finished(err) {
console.log('ALL SET');
}
}
function calorie_feature(labelArr, featureArr) {
// Asuming that our service is running on https://www.ourservice.com
// This is how we can access our music-search feature in any node.js app
// Url as in documentation file
var par = labelArr[0]['label'][0];
// http://localhost:3000/foodCalorie/search?q="text
var url = `http://localhost:3000/foodCalorie/search?q=${par}`.replace(/\s/g, '');
http.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var jsonObj = JSON.parse(body);
parse_calorie(jsonObj);
process.exit(1)
// Now data is ready in json obj use it whatever you want
});
}).on('error', function(e) {
console.log("error in API: ", e);
process.exit(1)
});
}
function airport_feature(labelArr, featureArr) {
// Asuming that our service is running on https://www.ourservice.com
// This is how we can access our music-search feature in any node.js app
// Url as in documentation file
var par = labelArr[0]['label'][0];
// http://localhost:3000/foodCalorie/search?q="text
var url = `http://localhost:3000/airportSearch/search?q=${par}`.replace(/\s/g, '');
http.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var jsonObj = JSON.parse(body);
parse_airport(jsonObj);
process.exit(1)
// Now data is ready in json obj use it whatever you want
});
}).on('error', function(e) {
console.log("error in API: ", e);
process.exit(1)
});
}
function parse_airport(Object) {
console.log(Object);
// try {
// random_track_index = Math.floor(Math.random() * Object['data'].length + 1);
// console.log(Object['data'][random_track_index]['title']);
// console.log(Object['data'][random_track_index]['link']);
// } catch {
// console.log('Sorry, e cant find enough data for the artist');
// }
// var obj = {
// name: '',
// url: ''
// };
// obj.name = Object['data'][random_track_index]['title'];
// obj.url = Object['data'][random_track_index]['link'];
// var data = JSON.stringify(obj);
// fs.writeFileSync('apiresp.json', data, finished);
// function finished(err) {
// console.log('ALL SET');
// }
}
function parse_translate(Object) {
// console.log(Object['data']['translations'][0]['translatedText']);
try {
console.log(Object['data']['translations'][0]['translatedText']);
// random_track_index = Math.floor(Math.random() * Object['data'].length + 1);
// console.log(Object['data'][random_track_index]['title']);
// console.log(Object['data'][random_track_index]['link']);
} catch {
console.log('Sorry, e cant find enough data for the language');
}
var obj = {
translation: ''
};
obj.translation = Object['data']['translations'][0]['translatedText']
var data = JSON.stringify(obj);
fs.writeFileSync('apiresp.json', data, finished);
function finished(err) {
console.log('ALL SET');
}
}
function translate_feature(labelArr, featureArr) {
console.log(labelArr);
var par1 = labelArr[0]['label'][0];
var par2 = labelArr[1]['label'][0];
var url = `http://localhost:3000/googleTranslate/trans?q=${par2}&target=${par1}`.replace(/\s/g, '');
http.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var jsonObj = JSON.parse(body);
parse_translate(jsonObj);
process.exit(1)
// Now data is ready in json obj use it whatever you want
});
}).on('error', function(e) {
console.log("error in API: ", e);
process.exit(1)
});
}
function parse_corona(Object) {
try {
console.log(Object['data']['recovered']);
console.log(Object['data']['death']);
console.log(Object['data']['confirmed']);
} catch {
console.log('Sorry, e cant find enough data for the artist');
}
var obj = {
recovered: '',
death: '',
confirmed: ''
};
obj.recovered = Object['data']['recovered'];
obj.death = Object['data']['death'];
obj.confirmed = Object['data']['confirmed'];
var data = JSON.stringify(obj);
fs.writeFileSync('apiresp.json', data, finished);
function finished(err) {
console.log('ALL SET');
}
}
function corona_feature(labelArr, featureArr) {
// Asuming that our service is running on https://www.ourservice.com
// This is how we can access our music-search feature in any node.js app
// Url as in documentation file
var par = labelArr[0]['label'][0];
var url = `http://localhost:3000/coronavirusStatistics/search?q=Egypt${par}`.replace(/\s/g, '');
http.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var jsonObj = JSON.parse(body);
parse_corona(jsonObj);
process.exit(1)
// Now data is ready in json obj use it whatever you want
});
}).on('error', function(e) {
console.log("error in API: ", e);
process.exit(1)
});
}
function calculator_feature(labelArr, featureArr) {
// Asuming that our service is running on https://www.ourservice.com
// This is how we can access our calcolator-search feature in any node.js app
// Url as in documentation file
var par = labelArr[0]['label'][0];
var url = `http://localhost:3000/calculator/search?exp=${par}`.replace(/\s/g, '');
http.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var jsonObj = JSON.parse(body);
parse_calculator(jsonObj);
process.exit(1)
// Now data is ready in json obj use it whatever you want
});
}).on('error', function(e) {
console.log("error in API: ", e);
process.exit(1)
});
}
function parse_calculator(Object) {
try {
console.log([0]).tostring;
} catch {
console.log('Sorry, e cant find the result');
}
var obj = {
num: '',
};
obj.num = console.log([0]);
var data = JSON.stringify(obj);
fs.writeFileSync('apiresp.json', data, finished);
function finished(err) {
console.log('ALL SET');
}
}
function parse_news(Object) {
try {
} catch {
console.log('Sorry, e cant find enough data for the artist');
}
var obj = {
titel: '',
description: '',
url: ''
};
obj.titel = Object['data'][random_track_index]['title'];
obj.url = Object['data'][random_track_index]['link'];
obj.daescription = Object['data'][random_track_index]['description'];
var data = JSON.stringify(obj);
fs.writeFileSync('apiresp.json', data, finished);
function finished(err) {
console.log('ALL SET');
}
}
function news_feature(labelArr, featureArr) {
// Asuming that our service is running on https://www.ourservice.com
// This is how we can access our news-search feature in any node.js app
// Url as in documentation file
var url = `http://localhost:3000/news`;
http.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var jsonObj = JSON.parse(body);
parse_news(jsonObj);
process.exit(1)
// Now data is ready in json obj use it whatever you want
});
}).on('error', function(e) {
console.log("error in API: ", e);
process.exit(1)
});
}
function news_feature(labelArr, featureArr) {
// Asuming that our service is running on https://www.ourservice.com
// This is how we can access our news-search feature in any node.js app
// Url as in documentation file
var par = labelArr[0]['label'][0];
var url = `http://localhost:3000/music/search?q=${par}`.replace(/\s/g, '');
http.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var jsonObj = JSON.parse(body);
parse_news(jsonObj);
process.exit(1)
// Now data is ready in json obj use it whatever you want
});
}).on('error', function(e) {
console.log("error in API: ", e);
process.exit(1)
});
}
function parse_movies(Object) {
try {
} catch {
console.log('Sorry, Not Enough Data');
}
var obj = {
name: '',
discription: ''
};
obj.name = Object['data'][random_track_index]['title'];
obj.description = Object['data'][random_track_index]['overiew'];
var data = JSON.stringify(obj);
fs.writeFileSync('apiresp.json', data, finished);
function finished(err) {
console.log('ALL SET');
}
}
function movies_feature(labelArr, featureArr) {
// Asuming that our service is running on https://www.ourservice.com
// This is how we can access our music-search feature in any node.js app
// Url as in documentation file
var par = labelArr[0]['label'][0];
var url = `http://localhost:3000/movies/recommendations?id=${par}`.replace(/\s/g, '');
http.get(url, function(response) {
var body = '';
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var jsonObj = JSON.parse(body);
parse_Movies(jsonObj);
process.exit(1)
// Now data is ready in json obj use it whatever you want
});
}).on('error', function(e) {
console.log("error in API: ", e);
process.exit(1)
});
}
app.listen(3000);
console.log('Server Stater on port 3000');
module.exports = app;
process.on('SIGTERM', () => {
console.info('SIGTERM signal received.');
console.log('Closing http server.');
server.close(() => {
console.log('Http server closed.');
// boolean means [force], see in mongoose doc
});
});
// Service Routes
newsRoute(app);
moviesRoute(app);
curerencyRoute(app);
weatherRoute(app);
musicRoute(app);
foodCalories(app);
googleTranslate(app);
airportSearch(app);