-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
108 lines (98 loc) · 2.87 KB
/
app.js
File metadata and controls
108 lines (98 loc) · 2.87 KB
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
const express = require("express");
const app = express();
const fetch = require("node-fetch");
const geocoder = require("geocoder");
const cors = require("cors");
const bodyParser = require("body-parser");
const { PythonShell } = require("python-shell");
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get("/", (req, res) => {
res.send("Works");
});
app.get("/map_get", (req, res) => {
fetch(
`https://apis.mapmyindia.com/advancedmaps/v1/fxs1vleongo2371f3mcb4jsjn21ii73x/route_adv/driving/77.2333,28.6665;77.3178,28.4089?steps=true&alternatives=true`
)
.then(res => res.json())
.then(data => {
let size = Object.keys(data.routes).length;
let mainArray = [];
for (let i = 0; i < size; i++) {
// mainArray.push(data.routes[i].legs[0].steps[j]);
let tempArray = [];
for (let j = 0; j < data.routes[i].legs[0].steps.length; j++) {
tempArray.push(
data.routes[i].legs[0].steps[j].intersections[0].location
);
}
mainArray.push(tempArray);
}
let options = {
args: mainArray
};
PythonShell.run("model.py", options, (err, result) => {
if (err) {
throw err;
}
res.send(result);
});
})
.catch(err => {
res.send(err);
});
});
app.post("/map", (req, res) => {
const { start_lat, start_long, end_lat, end_long } = req.body;
fetch(
`https://apis.mapmyindia.com/advancedmaps/v1/fxs1vleongo2371f3mcb4jsjn21ii73x/route_adv/driving/${start_lat},${start_long};${end_lat},${end_long}?steps=true&alternatives=true`
)
.then(res => res.json())
.then(data => {
let size = Object.keys(data.routes).length;
let mainArray = [];
for (let i = 0; i < size; i++) {
// mainArray.push(data.routes[i].legs[0].steps[j]);
let tempArray = [];
for (let j = 0; j < data.routes[i].legs[0].steps.length; j++) {
tempArray.push(
data.routes[i].legs[0].steps[j].intersections[0].location
);
}
mainArray.push(tempArray);
}
let options = {
args: mainArray
};
PythonShell.run("model.py", options, (err, result) => {
if (err) {
throw err;
}
// res.send(result);
let minIndex=0;
for(let i=1;i<result.length;i++){
if(result[i]<result[minIndex]){
minIndex=i;
}
}
res.send(mainArray[minIndex]);
});
})
.catch(err => {
res.send(err);
});
});
app.post("/geocode", (req, res) => {
const { location } = req.body;
geocoder.geocode(`location`, function(err, data) {
if (err) {
res.send(err);
} else {
res.send(data);
}
});
});
app.listen(process.env.PORT || 7777, () => {
console.log("Server started on http://localhost:3344");
});