-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
256 lines (230 loc) · 8.55 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
var request = require('request');
var express = require('express');
var bodyParser = require('body-parser');
var app = express(); //inititate app
var assert = require("assert");
var PlaceSearch = require("googleplaces");
var config = require("./config.js");
app.listen(process.env.PORT || 3000); //listen on port 3000
app.use("/public",express.static("public"))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.get("/",function(req,res){
res.sendFile(__dirname + "/form.html");
})
app.get("/dev", function(req,res){
res.send("The server is running !");
})
app.post("/post/sendAddressInfo", function(req,res){
var rawAddressString = req.body.rawAddressString;
var superScore = 0;
var latitude;
var longitude;
var hubwayAnswer = [];
var transitAnswer = [];
var hospitalAnswer = [];
var schoolAnswer = [];
var googleMapsClient = require('@google/maps').createClient({
key: 'AIzaSyCAnengb3KwPesSKMd7BY5WZFQvXxBIRfc'
});
googleMapsClient.geocode({
address: rawAddressString},
function(err, response) {
if (!err) {
latitude = response.json.results[0]["geometry"]["location"].lat;
longitude = response.json.results[0]["geometry"]["location"].lng;
var hubwaySearch = new PlaceSearch(config.apiKey, config.outputFormat);
console.log("The lat is " + latitude + " and the type is " + typeof(latitude));
console.log("The long is " + longitude + " and the type is " + typeof(longitude));
var hubwayParameters = {
location: [latitude, longitude],
keyword: "hubway",
radius: "800"
};
//console.log("Hubway params are " + hubwayParameters.location);
hubwaySearch.placeSearch(hubwayParameters, function (error, response) {
if (error) {
return; // no results found
//assert.notEqual(response.results.length, 0, "Place search must not return 0 results");
}
else{
var i;
for(i = 0; i < response.results.length; i++)
{
if (!hubwayAnswer.includes(response.results[i].name))
hubwayAnswer.push(response.results[i].name);
if(response.results[i].name === "Hubway" || response.results[i].name === "Hubway Station" || response.results[i].name === "Hubway Bike Stand" || response.results[i].anme === "Bike share hubway")
continue;
}
superScore += 5;
console.log("Hubway stations within a " + hubwayParameters.radius + "\n" + hubwayAnswer);
}
});
var transitSearch = new PlaceSearch(config.apiKey, config.outputFormat);
var transitParameters = {
location: [latitude, longitude],
types: "transit_station",
radius: "400"
};
transitSearch.placeSearch(transitParameters, function (error, response) {
if (error) {
return; // no results found
//assert.notEqual(response.results.length, 0, "Place search must not return 0 results");
}
else{
var i;
for(i = 0; i < response.results.length; i++)
{
if (!transitAnswer.includes(response.results[i].name))
transitAnswer.push(response.results[i].name);
}
superScore += 10;
console.log("Transit stops are");
console.log(transitAnswer);
}
});
var hospitalSearch = new PlaceSearch(config.apiKey, config.outputFormat);
var hospitalParameters = {
location: [latitude, longitude],
keyword: "hospital",
radius: "1600"
};
hospitalSearch.placeSearch(hospitalParameters, function (error, response) {
if (error) {
return; // no results found
//assert.notEqual(response.results.length, 0, "Place search must not return 0 results");
}
else{
var i;
for(i = 0; i < response.results.length; i++)
{
if (!hospitalAnswer.includes(response.results[i].name))
hospitalAnswer.push(response.results[i].name);
}
superScore += 10;
console.log("Hospitals are ");
console.log(hospitalAnswer);
}
});
var gymSearch = new PlaceSearch(config.apiKey, config.outputFormat);
var gymParameters = {
location: [latitude, longitude],
types: "gym",
radius: "1000"
};
gymSearch.placeSearch(gymParameters, function (error, response) {
if (error) {
return; // no results found
//assert.notEqual(response.results.length, 0, "Place search must not return 0 results");
}
else{
var i;
var gymAnswer = [];
for(i = 0; i < response.results.length; i++)
{
if (!gymAnswer.includes(response.results[i].name))
gymAnswer.push(response.results[i].name);
}
superScore += 5;
console.log("Gyms are");
console.log(gymAnswer);
}
});
var schoolSearch = new PlaceSearch(config.apiKey, config.outputFormat);
var schoolParameters = {
location: [latitude, longitude],
types: "school",
radius: "1600"
};
schoolSearch.placeSearch(schoolParameters, function (error, response) {
if (error) {
return; // no results found
//assert.notEqual(response.results.length, 0, "Place search must not return 0 results");
}
else{
var i;
for(i = 0; i < response.results.length; i++)
{
if (!schoolAnswer.includes(response.results[i].rating, response.results[i].name))
schoolAnswer.push(response.results[i].rating, response.results[i].name);
}
superScore += 15;
console.log("Schools are ");
console.log(schoolAnswer);
}
});
}
var addressArray = rawAddressString.split(" "); //split up addressArray
var houseNum = addressArray[0]; //set house number
var streetName = "";
var zipCode = addressArray[-1]; //set zipcode
if(addressArray.length == 6){
streetName = addressArray[1] + " " + addressArray[2];
}
else{
streetName = addressArray[1] + " " + addressArray[2] + " " + addressArray[3];
}
var options = {
"url": "https://apis.solarialabs.com/shine/v1/total-home-scores/reports",
"method": "GET",
"qs": {
"street-number": houseNum,
"street-name": streetName,
"city": "Boston",
"state": "Massachusetts",
"zip-code": zipCode,
"apikey": "rgN2P63pYYz95MbDWoAx4ny4Fb23yrj0"
}
}
var finalResponse = {};
var safetyReport = []; //push all incidences of safety concerns into safetyReport;
var noiseReport = []; //push all incidences of quietFactors into noiseReport array;
var rating;
request(options, function(err,response,body){
var result = JSON.parse(body);
finalResponse.quietValue = Math.round(result.totalHomeScores.quiet.value);
finalResponse.safetyValue = Math.round(result.totalHomeScores.safety.value);
superScore += (finalResponse.safetyValue * 0.5);
var quietFactors = result.totalHomeScores.quiet.factors;
var safetyFactors = result.totalHomeScores.safety.factors;
if(quietFactors.cycleway){
superScore += 5;
noiseReport.push("Designated bicycle paths exist here");
}
if(quietFactors.light_rail || quietFactors.tram){
noiseReport.push("The T runs close to here");
}
if(quietFactors.motorway || quietFactors.primary){
superScore -= 3;
noiseReport.push("A major road or highway runs close to here");
}
if(quietFactors.rail){
superScore -= 1;
noiseReport.push("The commuter rail runs close to here");
}
if(safetyFactors.accel || safetyFactors.speed){
superScore -= 5;
safetyReport.push("There have been reports of above average speeding in this area");
}
if(superScore > 0 && superScore <=30 ){
rating = "That's OK ! You live in neighborhood that is below the city average for quality of life";
}
if(superScore > 30 && superScore <=55){
rating = "Good ! You live in a neighborhood that has average quality of life";
}
if(superScore > 55){
rating = "Great ! You live in a neighborhood with a high quality of life according to city data";
}
finalResponse.noiseReport = noiseReport;
finalResponse.safetyReport = safetyReport;
finalResponse.hubwayStations = hubwayAnswer;
finalResponse.transitStations = transitAnswer;
finalResponse.hospitals = hospitalAnswer;
finalResponse.schools = schoolAnswer;
finalResponse.superScore = superScore;
finalResponse.rating = rating;
res.json(finalResponse);
})
});
})
//111 W 8th St, Boston, MA, 02127