-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreateZomatoRatings.js
95 lines (88 loc) · 2.46 KB
/
createZomatoRatings.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
const axios = require('axios')
let {zomatoApiKey} = require('../secrets')
const {Review} = require('../server/db/models')
const iconUrl =
'https://images-na.ssl-images-amazon.com/images/I/21Wc%2BuzZURL._SY355_.png'
if (!zomatoApiKey) {
zomatoApiKey = process.env.zomatoApiKey
}
async function getZomatoRestaurants(start) {
try {
const {data} = await axios.get(
`https://developers.zomato.com/api/v2.1/search?entity_type=city&entity_id=280&start=${start}&sort=rating`,
{
headers: {
'user-key': zomatoApiKey
}
}
)
return data.restaurants
} catch (error) {
console.error(error)
}
}
async function getRestaurants() {
try {
let restaurants = []
for (let i = 0; i < 5; i++) {
let response = await getZomatoRestaurants(i * 20)
restaurants = restaurants.concat(Array.from(response))
}
return restaurants
} catch (error) {
console.error(error)
}
}
const createZomatoRestaurantObj = async () => {
try {
let shackCount = 0
const restaurantsArr = await getRestaurants()
return restaurantsArr.map(place => {
let finalRestaurant = place.restaurant
if (finalRestaurant.name === 'Shake Shack') {
if (!shackCount) {
shackCount++
return {
name: finalRestaurant.name,
rating: finalRestaurant.user_rating.aggregate_rating,
reviewUrl: finalRestaurant.url
}
}
} else {
return {
name: finalRestaurant.name,
rating: finalRestaurant.user_rating.aggregate_rating,
reviewUrl: finalRestaurant.url
}
}
})
} catch (error) {
console.error(error)
}
}
async function zomatoCreate(restaurantObj) {
try {
console.log('Creating Zomato reviews...')
let zomatoRestaurants = await createZomatoRestaurantObj()
zomatoRestaurants = zomatoRestaurants.filter(restaurant => {
if (restaurant) return true
})
let reviewsForCreate = []
zomatoRestaurants.forEach(restaurant => {
if (restaurantObj[restaurant.name]) {
reviewsForCreate.push({
source: 'Zomato',
rating: restaurant.rating,
restaurantId: restaurantObj[restaurant.name].id,
sourceLogo: iconUrl,
reviewUrl: restaurant.reviewUrl
})
}
})
await Review.bulkCreate(reviewsForCreate)
console.log('Zomato is done!')
} catch (error) {
console.error(error)
}
}
module.exports = zomatoCreate