forked from carterbs/lunchBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupportFunctions.js
More file actions
242 lines (211 loc) · 8.19 KB
/
SupportFunctions.js
File metadata and controls
242 lines (211 loc) · 8.19 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
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
// TODO: Add restaurants to ignore
// TODO: Add daily specials
// TODO: Add ability to save config
// TODO: Add ability to overrule lunchbot
// TODO: Restaurants shouldn't be saved to config
// TODO: Might be better to have channel ID as an environment variable too
var request = require('sync-request');
var CONFIG = require('./config.json');
/**
* Mask for creating the OrderUp URL. Replace $SLUG$ with the restaurant slug to build the full URL.
*/
var ORDERUP_ORDER_URL_MASK = 'https://orderup.com/restaurants/$SLUG$/delivery';
/**
* Mask for creating the URL to check the time slots for a restaurant. Replace $SLUG$ with the restaurant slug to crete
* the full URL.
*/
var ORDERUP_TIME_SLOTS_URL_MASK = 'https://orderup.com/restaurants/$SLUG$/delivery/time_slots.json';
/**
* URL to request restaurant list from OrderUp.
*/
var RESTAURANT_LIST_URL = 'https://orderup.com/api/v2/restaurants?order_type=delivery' +
'&lon=' + CONFIG.location.longitude +
'&lat=' + CONFIG.location.latitude +
'&market_id=' + CONFIG.location.marketID;
/**
* list of possible valid poll options. Can have up to nine restaurants in a poll.
*/
var POSSIBLE_REACTIONS = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
/**
* Updates defaultRestaurants list with data from OrderUp.
*
* @returns new list of restaurants
*/
function updateRestaurants() {
var response = request('GET', RESTAURANT_LIST_URL);
var newRestaurants = [];
if (response.statusCode === 200) {
// parse data into defaultRestaurants format
var body = JSON.parse(response.body.toString('UTF-8'));
body.restaurants.forEach(function (restaurant) {
var categories = restaurant.restaurantCategories.filter(function (category) {
// Remove categories in the ignore list.
return !CONFIG.categoriesToIgnore.includes(category.name);
});
if (categories.length === 0) {
// Don't add restaurants that don't have a category.
return;
} else if ((restaurant.yelpReviewCount === null) ||
(restaurant.yelpReviewCount < CONFIG.yelp.minReviews) ||
(restaurant.yelpRating === null) ||
(restaurant.yelpRating < CONFIG.yelp.minRating)) {
// Restaurant doesn't meet minimum rating requirements
return;
}
//grab name from each item and join with a comma.
var categoryList = categories.map(function (category) { return category.name; }).join(', ');
var newRestaurant = {
"name": restaurant.name,
"categories": categories,
"categoryList": categoryList,
"slug": restaurant.slug,
"restaurantURL": restaurant.yelpURL,
"image": restaurant.yelpRatingImageUrlSmall,
"yelpRating": restaurant.yelpRating,
"yelpReviewCount": restaurant.yelpReviewCount
};
newRestaurants.push(newRestaurant);
});
}
return newRestaurants;
}
/**
* Select a set of restaurants.
*
* @param restaurants The list of restaurants from which to select.
* @param previouslySelectedRestaurants The list of restaurants that have previously been selected.
*/
var selectRestaurants = function (restaurants, previouslySelectedRestaurants) {
if (!previouslySelectedRestaurants) {
previouslySelectedRestaurants = [];
}
// TODO: This should be done on start up and saved to the config file.
var restaurantsPerPoll = CONFIG.restaurantsPerPoll;
if (typeof(restaurantsPerPoll) === 'undefined') {
// Default to five restaurants per poll
restaurantsPerPoll = 5;
} if (restaurantsPerPoll < 2) {
// Minimum of two restaurants in a poll
restaurantsPerPoll = 2;
} else if (restaurantsPerPoll > 9) {
// Maximum of 9 restaurants in a poll
restaurantsPerPoll = 9;
}
// Select reactions for the number of restaurants
var reactions = POSSIBLE_REACTIONS.slice(0, restaurantsPerPoll);
var remainingRestaurants = [];
var selectedRestaurants = previouslySelectedRestaurants.slice();
// Select the number of restaurants as the number of reactions.
for (var i = 0; i < reactions.length; ++i) {
// Don't select restaurants that match the categories of the already selected restaurants.
restaurants = filterRestaurants(restaurants, selectedRestaurants);
if (restaurants.length > 0) {
// Select random restaurant from remaining restaurant list.
var restaurantIndex = Math.floor(Math.random() * restaurants.length);
var selectedRestaurant = restaurants[restaurantIndex];
selectedRestaurant.reaction = reactions[i];
// Added selected restaurant to the list.
selectedRestaurants.push(selectedRestaurant);
} else {
console.error('Not enough restaurants to provide the full list');
}
}
return selectedRestaurants;
};
/**
* Filters the restaurants down to whose categories have not been selected.
*
* @param {any} restaurants The list of restaurants from which to select.
* @param {any} selectedRestaurants This of already selected restaurants.
*/
function filterRestaurants(restaurants, selectedRestaurants) {
// Get list of selected categories.
var selectedCategories = [];
selectedRestaurants.forEach(function (selectedRestaurant) {
selectedRestaurant.categories.forEach(function (category) {
if (selectedCategories.indexOf(category) < 0) {
selectedCategories.push(category.id);
}
});
});
// Get restaurants whose categories have not already been selected
var filteredRestaurants = restaurants.filter(function (restaurant) {
// Find the categories where there is overlap
var categories = restaurant.categories.map(function (category) { return category.id; });
var categoryIntersection = intersect(selectedCategories, categories);
if (categoryIntersection.length === 0) {
return isOpenToday(restaurant);
}
// Keep if there is no overlap.
return false;
});
return filteredRestaurants;
}
/**
* Determines whether the restaurant is open today.
*
* @param {any} restaurant
* @returns
*/
function isOpenToday(restaurant) {
var url = ORDERUP_TIME_SLOTS_URL_MASK.replace('$SLUG$', restaurant.slug);
var response = request('GET', url);
if (response.statusCode !== 200) {
// TODO: Log error
return false;
}
// parse data into defaultRestaurants format
var body = JSON.parse(response.body.toString('UTF-8'));
if (typeof body.days === 'undefined') {
return false;
}
var isOpen = false;
body.days.forEach(function(day){
if (day.display === 'Today') {
isOpen = true;
}
});
return isOpen;
}
/**
* Determine the intersection of two arrays.
*
* @param {any} array1 The first array.
* @param {any} array2 The second array.
* @returns The elements where the two arrays intersect.
*/
function intersect(array1, array2) {
if (array2.length > array1.length) {
var temp = array2;
array2 = array1;
array1 = temp;
}
// indexOf to loop over shorter
var intersection = array1.filter(function (e) {
return array2.indexOf(e) > -1;
});
return intersection;
}
/**
* Lists all of the unique categories returned from OrderUp.
*/
function listCategories() {
var categories = {};
var restaurants = updateRestaurants();
restaurants.forEach(function (restaurant) {
restaurant.categories.forEach(function (category) {
categories[category.name] = category;
});
});
var keys = Object.keys(categories);
console.log('There are ' + keys.length + ' available');
for (var key in categories) {
var value = categories[key];
console.log(value.name);
}
}
module.exports.isOpenToday = isOpenToday;
module.exports.updateRestaurants = updateRestaurants;
module.exports.selectRestaurants = selectRestaurants;
module.exports.filterRestaurants = filterRestaurants;
module.exports.intersect = intersect;