-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
92 lines (77 loc) · 3.09 KB
/
index.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
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const argv = yargs(hideBin(process.argv)).argv
const axios = require('axios');
const orderBy = require('lodash.orderby');
const Table = require('cli-table');
const WOLT_EPS = {
getLocationId: 'https://restaurant-api.wolt.com/v1/google/places/autocomplete/json?input=',
getLocationGeo: 'https://restaurant-api.wolt.com/v1/google/geocode/json?place_id=',
getRestaurants: 'https://restaurant-api.wolt.com/v1/pages/restaurants?'
}
const COMPARATORS = {
rating: 'rating.rating',
price: 'price_range',
deliveryTime: 'estimate',
deliveryPrice: 'delivery_price'
}
const getRestsURLBuilder = (lat, lon) => `${WOLT_EPS.getRestaurants}lat=${lat}&lon=${lon}`;
function present(venues){
const arrReadyVenues = [].concat(venues);
const table = new Table({
head: ['Name', 'Type', 'Delivery est. (minutes)', 'Delivery price', 'Price range', 'Rating', 'URL']
});
table.push(
...arrReadyVenues.map(({venue}) => {
const results = [];
results.push(venue.name);
results.push((venue.categories || []).map(v=>v.name));
results.push(venue.estimate);
results.push(venue.delivery_price);
results.push(new Array(Number(venue.price_range || 0)).fill('$').join(''))
results.push(new Array(Number((venue.rating|| {}).rating) || 0).fill('★').join(''));
return results;
})
)
return table.toString();
}
async function run(){
if(argv.help || argv.h){
return `
Usage is 'wolty <location> [--random | --sort=<see below>]'
// If your location has space in it, use quotes.
// Also, please use precise location as there are no IO checks nor testing <3
// -h to show help
// --random to randomize a place to order from
// --sort=[rating | price | deliveryTime | deliveryPrice]
`
}
// Make sure we only get one arg
if(argv._.length > 1) {
return '❌ Immediate arg should be the location';
}
if(argv.sort){
if(!COMPARATORS[argv.sort]) return `Invalid sort option, "${argv.sort}"; Use one of ${Object.keys(COMPARATORS).join(', ')}`
}
// 1. Get the location ID from wolt
const woltLocations = await axios.get(`${WOLT_EPS.getLocationId}${encodeURIComponent(argv._[0])}`);
const woltLocationId = woltLocations.data.predictions[0].place_id; // This is why we only use single arg with no verifications. Feel free to PR this <3
// 2. Get coords from location
const woltGeoResponse = await axios.get(`${WOLT_EPS.getLocationGeo}${woltLocationId}`);
// Coords shape is {lat: number, lng: number}. Yes, no TS.
const coords = woltGeoResponse.data.results[0].geometry.location;
const nearbyRests = (await axios.get(getRestsURLBuilder(coords.lat, coords.lng)))
.data
.sections[0] // not sure, wolt api ¯\_(ツ)_/¯
.items;
// Filter for open & delivering
let relevantVenues = nearbyRests.filter(v=>v.venue.online && v.venue.delivers);
if (argv.random){
return present(relevantVenues[Math.floor(Math.random() * relevantVenues.length)])
}
if(argv.sort){
relevantVenues = orderBy(relevantVenues, `venue.${COMPARATORS[argv.sort]}`)
}
return present(relevantVenues);
}
run().then(console.log).catch(console.error);