-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (34 loc) · 1.25 KB
/
Copy pathindex.js
File metadata and controls
38 lines (34 loc) · 1.25 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
const fetch = require('isomorphic-fetch');
const play = require('play');
if (process.argv.length < 3) {
console.log('USAGE: node deliveroo.js <postcode>');
process.exit();
}
const postcode = process.argv[2];
const endpoint = `https://deliveroo.co.uk/restaurants/postcode=${postcode}`;
const timeoutDuration = 30000;
const normalNumberOfAsapRestaurants = 3;
const fetchFunction = (currentTimeout = null) => {
fetch(endpoint, {
headers: {
Accept: 'application/json',
} })
.then(result => result.json())
.then(({ restaurants }) => {
const availableRestaurants = restaurants.filter(({ asap }) => asap);
if (availableRestaurants.length < normalNumberOfAsapRestaurants) {
console.log('Deliveroo is not available at the moment');
console.log(`Checking again in ${timeoutDuration}msec`);
const timeout = setTimeout(() => fetchFunction(timeout), timeoutDuration);
} else {
clearTimeout(currentTimeout);
console.log('Deliveroo is available');
play.sound('./node_modules/wavs/sfx/alarm.wav');
}
})
.catch((error) => {
console.error(error);
clearTimeout(currentTimeout);
});
};
fetchFunction();