-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
449 lines (392 loc) · 14.2 KB
/
server.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
// Create the backend server
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { json } = require('body-parser');
const app = express();
const port = 3000;
app.use(cors()); // Enable CORS for all routes
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
let formData = ""; // Variable to store the form data
async function findPriceSq(address) {
//console.log('starting findPriceSq with addr:', address);
let addr = address;
let filter = "&excludeUndisclosedRate=true&types%5B%5D=Retail&types%5B%5D=Restaurant";
let loc = "&placeIds%5B%5D=ChIJIQBpAG2ahYAR_6128GcTUEo";
addr = addr.replaceAll(" ", "%20");
const fs = require('fs');
const puppeteer = require('puppeteer');
//console.log(addr);
async function run() {
const browser = await puppeteer.launch({headless : 'new'});
const page = await browser.newPage();
await page.goto('https://www.crexi.com/lease/properties?sort=Relevance&term=' + addr + filter + loc);
await page.waitForSelector('span.price');
const spanText = await page.$eval('span.price', (span) => span.textContent);
await page.waitForSelector('div.property-details.ng-star-inserted');
const detailsText = await page.$eval('div.property-details.ng-star-inserted', (div) => div.textContent);
console.log('Text within span:', spanText);
console.log('Text within span:', detailsText);
await browser.close();
// Parse spanText
let numStr = "";
for (let char of spanText) {
if (!isNaN(char) || (char === '-' && !numStr)) { // Allow negative sign at the beginning
numStr += char;
} else if (numStr) {
break; // Stop when a non-digit is encountered after the number
}
}
//console.log('number parsed:', numStr);
let returnVal = 0;
if (numStr) {
returnVal = parseInt(numStr, 10);
} else {
returnVal = null; // Return null if no integer is found
}
let timeRate = (spanText.charAt(spanText.length - 1) == 'R') ? 12 : 1;
//console.log('timerate', timeRate);
returnVal = returnVal / timeRate;
//console.log('returnval:', returnVal);
// Parse detailsText
let shortdetails = detailsText.substring(0, detailsText.length - 9); // length of 'sq. ft.'
function isNumeric(char) {
return /^[0-9]$/.test(char);
}
let finalArea = shortdetails.substring(shortdetails.lastIndexOf(' ') + 1, shortdetails.length);
if (!isNumeric(shortdetails.charAt(shortdetails.length - 1))) {finalArea = null;}
//console.log('upper:', upperBound, 'lower:', lowerBound);
// Parse store type
// parse type
let finalType = "Other";
if (detailsText.indexOf("Retail") != -1) finalType = "Retail";
if (detailsText.indexOf("Restaurant") != -1) finalType = "Restaurant";
return {
price: returnVal,
lower: finalArea,
upper: null,
type: finalType
};
}
let p = await run();
return p;
}
async function getUpdatedToken() {
//Set up URL to query
let appId = "2hz7e4tiy3";
let hashToken = "Mmh6N2U0dGl5M3xySm9XUVJ4S3pTMXVTYzA1T1duZUoxOW15SEVPMHpsWDVzSnh2RzZ2";
let url = `https://api.iq.inrix.com/auth/v1/appToken?appId=${appId}&hashToken=${hashToken}`;
//Set up query method
var requestOptions = {
method: 'GET'
};
//Query INRIX for token
let responseToken = await fetch(url, requestOptions);
let json = await responseToken.json();
return json;
}
// Find the latitude and longitude based on address
async function findLatLong(address) {
//console.log('starting findLatLong with address:', address);
const geocodeAPIkey = "c61726c4e1c641249c8c39c657e51ea5";
let url = `https://api.opencagedata.com/geocode/v1/json?key=${geocodeAPIkey}&q=${address.data}`;
var requestOptions = {
method: 'GET'
};
let latlong = await fetch(url, requestOptions);
let json = await latlong.json();
//console.log('this is the json:', json.results[0]);
let numResults = json.total_results;
if (numResults == 0) {
console.log("nothing found :(");
return {lat: 0, lng: 0};
} else {
let coords = json.results[0].geometry;
//console.log('these are the coords:', coords);
return coords;
}
}
// Call the INRIX API using the form data
async function pullINRIXData(coords) {
//console.log('Coords used for INRIX API call:', coords);
// Hard-coded authentication token
//const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhcHBJZCI6IjJoejdlNHRpeTMiLCJ0b2tlbiI6eyJpdiI6IjQxYWI5MDBhMzlhYTQ0ODBlZDY5YWE0YTQ0YWYyYmYyIiwiY29udGVudCI6IjkwYjZmMDBkMjRlMzEwZDA5M2NiNjVmNWRhYjA5ODNjZDEwMTFiOGE4MjUxYmE1NDljMGEwNzMwYzU2MmYzNTQ2MWMyNmYwNDU0YmE1Yjc0Njg2YjEwMGFiZjlkMTNkOGEzZDkxNDFkYzQ1Y2U3NjkzOGY4ZWFkMDBjNWMxNWU5MGU4Y2MwZjYxNmQyNjc4N2RjYjUyZTQyYmVj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8vHFVRQjNfLfgaZXzuB6zkCxqgqjqDQvE";
const token = (await getUpdatedToken()).result.token;
var myHeaders = new Headers();
myHeaders.append("Authorization", `Bearer ${token}`);
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
// Other parameters
let radius = 100;
let coord1 = coords.lat;
let coord2 = coords.lng;
// if invalid input
if (coord1 == 0) {
return {count: "n/a"}
}
// Call the API with the url and fetch the data
let url = `https://api.iq.inrix.com/v1/trips-count?od=destination&geoFilterType=circle&radius=${radius}m&points=${coord1}%7C${coord2}&limit=1000&startDateTime=%3E%3D2023-06-01T02%3A31&endDateTime=%3C%3D2023-06-15T02%3A31`;
let responseTrips = await fetch(url, requestOptions);
let json = await responseTrips.json();
return json;
}
async function getRefData() {
let json = {
list1: {
lat: 37.79414,
lng: -122.39858,
rating: 9.85,
url: 'https://www.crexi.com/lease/properties/599013/california-275-sacramento-former-walgreens'
},
list2: {
lat: 37.79758,
lng: -122.43343,
rating: 9.12,
url: 'https://www.crexi.com/lease/properties/674362/california-2072-union-st'
},
list3: {
lat: 37.76457,
lng: -122.43206,
rating: 9.03,
url: 'https://www.crexi.com/lease/properties/428720/california-2275-market-street'
},
list4: {
lat: 37.71047,
lng: -122.46747,
rating: 8.61,
url: 'https://www.crexi.com/lease/properties/702080/california-3931-alemany-blvd'
},
list5: {
lat: 37.76683,
lng: -122.43040,
rating: 8.40,
url: 'https://www.crexi.com/lease/properties/495076/california-225-7th-street'
},
list6: {
lat: 37.79172,
lng: -122.40593,
rating: 7.91,
url: 'https://www.crexi.com/lease/properties/642670/california-515-grant-avenue'
},
list7: {
lat: 37.78096,
lng: -122.45916,
rating: 7.62,
url: 'https://www.crexi.com/lease/properties/574775/california-inner-richmond-retail'
},
list8: {
lat: 37.75192,
lng: -122.42782,
rating: 7.79,
url: 'https://www.crexi.com/lease/properties/427906/california-retail-shop-for-lease-or-sale-in-noe-valley-san-francisco'
}
}
return json;
}
// Function to process form data
async function processFormData(formData) {
try {
//console.log('Processing form data:', formData);
// Modify the input: replace all spaces with plus signs
let formattedaddress = formData.data.replace(/\s/g, '+');
//console.log('new string', formattedaddress);
// Find the price per square ft
const priceSq = await findPriceSq(formData.data);
// Make the API calls
const coords = await findLatLong(formData);
const tripcount = await pullINRIXData(coords);
// Evaluate the property based on traffic data and price
const score = (-(priceSq.price-3.41)/1.22+1.5*(tripcount.count-76.6)/38.75)*1.35+5;
const zPrice = (priceSq.price-3.41)/1.22;
const zTrip = (tripcount.count-76.6)/38.75;
let pcntPrice = 50;
let pcntTrip = 50;
switch (true) {
case zPrice < -2.5:
pnctPrice = 0.62;
break;
case zPrice < -2:
pnctPrice = 2.27;
break;
case zPrice < -1.5:
pnctPrice = 6.68;
break;
case zPrice < -1.25:
pnctPrice = 9.68;
break;
case zPrice < -1:
pnctPrice = 15.87;
break;
case zPrice < -0.75:
pnctPrice = 21.19;
break;
case zPrice < -0.5:
pnctPrice = 30.85;
break;
case zPrice < -0.25:
pnctPrice = 38.21;
break;
case zPrice <= 0:
pnctPrice = 50;
break;
case zPrice < 0.25:
pnctPrice = 61.79;
break;
case zPrice < 0.5:
pnctPrice = 69.15;
break;
case zPrice < 0.75:
pnctPrice = 78.81;
break;
case zPrice < 1:
pnctPrice = 84.13;
break;
case zPrice < 1.25:
pnctPrice = 90.32;
break;
case zPrice < 1.5:
pnctPrice = 93.32;
break;
case zPrice < 2:
pnctPrice = 97.72;
break;
case zPrice < 2.5:
pnctPrice = 99.38;
break;
default:
pnctPrice = 99.87;
break;
}
switch (true) {
case zTrip < -2.5:
pcntTrip = 0.62;
break;
case zTrip < -2:
pcntTrip = 2.27;
break;
case zTrip < -1.5:
pcntTrip = 6.68;
break;
case zTrip < -1.25:
pcntTrip = 9.68;
break;
case zTrip < -1:
pcntTrip = 15.87;
break;
case zTrip < -0.75:
pcntTrip = 21.19;
break;
case zTrip < -0.5:
pcntTrip = 30.85;
break;
case zTrip < -0.25:
pcntTrip = 38.21;
break;
case zTrip <= 0:
pcntTrip = 50;
break;
case zTrip < 0.25:
pcntTrip = 61.79;
break;
case zTrip < 0.5:
pcntTrip = 69.15;
break;
case zTrip < 0.75:
pcntTrip = 78.81;
break;
case zTrip < 1:
pcntTrip = 84.13;
break;
case zTrip < 1.25:
pcntTrip = 90.32;
break;
case zTrip < 1.5:
pcntTrip = 93.32;
break;
case zTrip < 2:
pcntTrip = 97.72;
break;
case zTrip < 2.5:
pcntTrip = 99.38;
break;
default:
pcntTrip = 99.87;
break;
}
let jsonlists = {
list1: {
lat: 37.79414,
lng: -122.39858,
rating: 9.85,
url: 'https://www.crexi.com/lease/properties/599013/california-275-sacramento-former-walgreens'
},
list2: {
lat: 37.79758,
lng: -122.43343,
rating: 9.12,
url: 'https://www.crexi.com/lease/properties/674362/california-2072-union-st'
}
}
//console.log('Continuing processing with API response:', apiResponse);
//console.log('Number of trips that', formData.data, 'made:', apiResponse.count);
const data = {
address: formData.data,
price: priceSq.price,
upper: priceSq.upper,
lower: priceSq.lower,
type: priceSq.type,
lat: coords.lat,
lng: coords.lng,
tripcount: tripcount.count,
score: score,
pcntPrice: 100-pnctPrice,
pcntTrip: pcntTrip,
url1: jsonlists.list1.url,
rating1: jsonlists.list1.rating,
url2: jsonlists.list2.url,
rating2: jsonlists.list2.rating
}
return data;
}
catch (error) {
console.error('Error during form data processing:', error);
throw error;
}
}
// CALLED WHEN FORM IS SUBMITTED
app.post('/submit-form', async (req, res) => {
// Get the form data
formData = req.body;
//console.log('Received form data:', formData);
try {
// Call the processFormData function and wait for its completion
const result = await processFormData(formData);
//console.log('finished processing form data');
console.log('result:', result);
// Send the API response back to the frontend
res.json({
message: 'Form data received successfully!',
address: result.address,
upper: result.upper,
lower: result.lower,
price: result.price,
type: result.type,
lat: result.lat,
lng: result.lng,
tripcount: result.tripcount,
score: result.score,
pcntPrice: result.pcntPrice,
pcntTrip: result.pcntTrip
});
}
catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});