-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleApiRestaurantsManager.js
145 lines (112 loc) · 3.91 KB
/
googleApiRestaurantsManager.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
require('dotenv').config();
const { Client } = require("@googlemaps/google-maps-services-js");
const Restaurant = require('./classes/Restaurant.js');
const { setTimeout } = require("timers/promises");
const client = new Client({});
const getRestaurantsFromResults = (results) => {
const restaurants = []
// for filtering out fast food restaurants
const chainCount = [
{ name: "Starbucks", count: "0" },
{ name: "Mcdonalds", count: "0" },
{ name: "Subway", count: "0" },
{ name: "Olive Garden", count: "0" },
{ name: "Taco Bell", count: "0" }
]
for (const restaurant of results) {
let repeatChainFound = false;
for (const chain of chainCount) {
if (restaurant.name.toUpperCase().includes(chain.name.toUpperCase())) {
if (chain.count >= 1) {
repeatChainFound = true;
} else {
chain.count++;
}
}
}
if (repeatChainFound) {
continue;
}
const newRestaurant = new Restaurant(
restaurant.place_id,
restaurant.name,
restaurant.rating,
restaurant.price_level,
restaurant.user_ratings_total,
)
restaurants.push(newRestaurant);
}
return restaurants;
}
const getInitialResults = async (lat, lng, radius) => {
const radiusInMeters = radius * 1609.34;
const requestParams = {
location: `${lat},${lng}`,
type: "restaurant",
radius: radiusInMeters,
opennow: true,
key: process.env.GOOGLE_PLACES_API_KEY
}
let results = [];
let nextPageToken = null;
let queryLimitHit = false;
await client.placesNearby({
params: requestParams
}).then(async (response) => {
results = response.data.results;
nextPageToken = response.data.next_page_token;
}).catch((err) => {
console.log(err);
if (err.response.data.status === 'OVER_QUERY_LIMIT') {
queryLimitHit = true;
}
});
const returnObj = { results: results, nextPageToken: nextPageToken, queryLimitHit: queryLimitHit };
return returnObj;
}
const getAdditionalResults = async (pageToken) => {
if (!pageToken) {
return;
}
const requestParams = {
pagetoken: pageToken,
key: process.env.GOOGLE_PLACES_API_KEY
}
let returnObj;
let results = [];
let nextPageToken = null;
let queryLimitHit = false;
await client.placesNearby({
params: requestParams
}).then(async (response) => {
results = response.data.results;
nextPageToken = response.data.next_page_token;
}).catch((err) => {
console.log(err);
if (err.response.data.status = 'OVER_QUERY_LIMIT') {
queryLimitHit = true;
}
});
returnObj = { results: results, nextPageToken: nextPageToken, queryLimitHit: queryLimitHit };
return returnObj;
}
const getRestaurants = async (lat, lng, radius) => {
if (!lat || !lng || !radius) {
return { restaurants: [], nextPageToken: null };
}
let initialResults = await getInitialResults(lat, lng, radius);
if (initialResults.queryLimitHit) {
return { restaurants: [], nextPageToken: null, queryLimitHit: true }
}
let additionalResults = { results: [], nextPageToken: null }
if (initialResults.nextPageToken) {
await setTimeout(3000);
additionalResults = await getAdditionalResults(initialResults.nextPageToken);
}
if (additionalResults.queryLimitHit) {
return { restaurants: [], nextPageToken: null, queryLimitHit: true }
}
const restaurants = getRestaurantsFromResults([...initialResults.results, ...additionalResults.results]);
return { restaurants: restaurants, nextPageToken: additionalResults.nextPageToken, queryLimitHit: false }
}
module.exports = { getRestaurants };