-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
140 lines (114 loc) · 3.65 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
//NODE MODULES
var fs = require("fs");
const express=require('express');
const fetch = require('node-fetch');
const bodyParser = require('body-parser');
const app=express();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
// Start Express
//SERVING STATIC CONTENT
app.use('/css',express.static('css'));
app.use('/js',express.static('js'));
app.use('/images',express.static('images'));
app.use('/',express.static(__dirname));
app.use(bodyParser.json());
//SETTING THE VIEW ENGINE
app.set('view engine','ejs');
//SERVING HTML PAGES
app.get('/',function(req,res){
// {club: req.params.club, dp:req.user.thumbnail }
res.render('index',{club: '', dp:'' });
});
app.post('/search',urlencodedParser,(req,res)=>{
var details=req.body;
//SEARCH QUERY "q"
searchQuery= details.searchQuery;
// console.log(searchQuery);
var longitude= details.longitude;
// console.log(longitude);
var latitude=details.latitude;
// console.log(latitude);
//FETCH API
var minval=Number.MAX_VALUE;
var finalname;
var branch;
var jsonData = {temp:"ok"};
var arr=searchQuery.split(" ");
var q="";
for (i=0; i<arr.length; i++){
if (i!=arr.length-1){
q+=arr[i]+"+";}
else{
q+=arr[i];
}
}
// console.log(q);
//'https://developers.zomato.com/api/v2.1/search?q='+q+'&lat='+lat+'&lon='+long
fetch('https://developers.zomato.com/api/v2.1/search?q='+q+'&lat='+latitude+'&lon='+longitude, {
method: 'get',
headers: { 'Content-Type': 'application/json', 'user-key': '5cdc9aed20a04f2f6310c10ca2b7dfd8' },
})
.then(res => res.json())
.then(function(json){
// EXTRACT NAME OF CLOSEST AND MOST RELVANT RESTAURANT
function is_inside(s1,s2)
{ s1=s1.toLowerCase();
s2=s2.toLowerCase();
s2=s2.replace(/[^a-zA-Z0-9 ]/g, "");
s1=s1.replace(/[^a-zA-Z0-9 ]/g, "");
return s2.includes(s1);
}
console.log(json);
var myarr=json.restaurants;
for (i=0;i<myarr.length;i++){
var x=myarr[i];
// console.log(x);
var a=0;
// console.log(x.restaurant.name.toLowerCase(),"blah");
if (is_inside(searchQuery,x.restaurant.name)!=-1){
a=Math.abs(latitude-x.restaurant.location.latitude)+Math.abs(longitude-x.restaurant.location.longitude);
if (a<minval){
minval=a;
finalname=x.restaurant.name;
branch=x.restaurant.location.locality;
}
}
}
console.log('OUTLET',finalname+' '+branch);
return [finalname,branch];
// return ['Burger Singh','Kalkaji'];
}).then(function(finalname_branch){
//SIDHANT AND AYAAN USE FILENAME FOR RESTERAUNT NAME AND BRANCH FOR LOCATION
//
var spawn = require("child_process").spawn;
console.log('NAMES',finalname_branch[0],finalname_branch[1]);
var process = spawn('python',["./main.py",finalname_branch[0],finalname_branch[1]]);
process.stdout.on('data', function(data) {
// jsonData = fs.readFileSync('temp.json');
res.render('results',{data:data});
return;
});
// return data={
// restaurant_name:'Burger Singh',
// location:'Kalkaji Mandir',
// menu:{'Potato Crunch Burger':['description',['₹68','₹100']]},
// links:['swiggy.com','zomato.com'],
// photo_link:"http://www.dwarka21.com/uploads/images/burger-1.jpg"
// };
})
});
// app.get('/dev',function(req,res){
// // {club: req.params.club, dp:req.user.thumbnail }
// res.render('results',{data: ''});
// });
// 404 if no other route
// THIS ROUTE SHOULD BE AT THE END OF THE FILE
app.use(function (req, res) {
res.status(404).sendFile(__dirname+'/404.html');
})
// gets local IP
const ip = require("ip");
var port = process.env.PORT || 8000
app.listen(port, function() {
console.log('Running now on ' + ip.address() + ":" + port);
});