-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (141 loc) · 5.02 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
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
const puppeteer = require('puppeteer')
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
const PORT = 5100
async function autoScroll(page){
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
var totalHeight = 0;
var distance = 100;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if(totalHeight >= scrollHeight){
clearInterval(timer);
resolve();
}
}, 100);
});
});
}
async function start (term) {
const url = `https://www.swiggy.com/restaurants/${term}`
console.log('Scraping... ', url)
const browser = await puppeteer.launch({ headless: true, devtools: false })
const page = await browser.newPage()
const response = {
data: {
name: null
},
error: null
}
try {
await page.goto(url)
await page.setViewport({
width: 1200,
height: 800
});
await autoScroll(page);
await page.waitForSelector('div#root h1')
/* try{
let element = await page.$x('//*[@id="root"]/div[1]/div[1]/div[1]/div[3]/div[1]/div/div[2]/div/div[3]/div[1]')
const textObject = await element[0].getProperty('textContent');
const text = textObject._remoteObject.value;
console.log(text)
} catch (e) {
console.log(e)
} */
//page.on("console", msg => console.log("PAGE LOG:", msg.text()))
const [name, type, location, cuisine, ratings, dishes] = await Promise.all([
page.$eval('div#root h1', el => el.innerText),
page.$eval("div#root .JMACF", el => el.textContent),
page.$eval("div#root .Gf2NS", el => el.textContent),
page.$eval("div#root ._2C8Ic", el => el.textContent),
page.$$eval("div#root ._2aZit ._2iUp9 ._2l3H5", els => els.map(item => item.textContent)),
page.$$("div#root ._1J_la ._2dS-v", nodes => nodes)
])
let [ overallrating, deliveryTime, costForTwo ] = ratings
let listDishes = dishes.map(async (dish) => {
return await page.evaluate((el) => {
let a = el.querySelector('.M_o7R').textContent
let b = Array.from(el.querySelectorAll('._1Jgt5'))
let c
let allCourseItems
let srouce
if(b.length) {
c = b.map((node) => {
let itemType = node.querySelector('._2WzQq').textContent
let allArr = Array.from(node.querySelectorAll('._2wg_t'))
let allItems
if(allArr.length) {
allItems = allArr.map(el => {
source = null
let imageEl = el.querySelector('.styles_itemImageContainer__3_3Ig img')
if(imageEl) source = imageEl.src
return {
name: el.querySelector('.styles_itemName__2Aoj9').textContent,
price: el.querySelector('.styles_itemPortionContainer__PE0SY').textContent,
image: source
}
})
}
return { itemType, items: allItems }
})
} else {
coursesArr = Array.from(el.querySelectorAll('._2wg_t'))
if(coursesArr.length) {
allCourseItems = coursesArr.map(el => {
source = null
let imageEl = el.querySelector('.styles_itemImageContainer__3_3Ig img')
if(imageEl) source = imageEl.src
return {
name: el.querySelector('.styles_itemName__2Aoj9').textContent,
price: el.querySelector('.styles_itemPortionContainer__PE0SY').textContent,
image: source
}
})
}
}
return { category: a, singleItems: c, allCourseItems }
}, dish)
})
allDishes = await Promise.all(listDishes)
response.data.name = name
response.data.restaurant_type = type
response.data.location = location
response.data.cuisine = cuisine
response.data.overallrating = overallrating
response.data.deliveryTime = deliveryTime
response.data.costForTwo = costForTwo
response.data.allDishes = allDishes
} catch (error) {
console.log(error)
response.error = 'Not Found'
}
await browser.close()
return response
}
app.get('/getResturantDetails', async (req, res) => {
const { term } = req.query
let data = {}
if(!term || req.query.term == '')
return res.status(400).json({ error: "Term was not supplied" })
try {
data = await start(decodeURI(req.query.term))
} catch (error) {
return res.status(400).json(data)
}
return res.status(200).json(data)
})
const startApp = async () => {
try {
await app.listen(PORT)
console.log('node server connected on port ' + PORT)
} catch (error) {
console.log(error)
}
}
startApp()