-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
302 lines (271 loc) · 12.1 KB
/
server.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
'use strict';
const express = require('express'),
http = require('http'),
puppeteer = require('puppeteer'),
devices = require('puppeteer/DeviceDescriptors'),
iphone = devices['iPhone 6'],
FuzzySet = require('fuzzyset.js');
var marketplaces = [
{
name: 'amazon',
defaultCurrency: 'USD',
domain: 'https://www.amazon.com/s/?field-keywords=',
resultItem: '.s-result-item',
productName: 'span.a-size-small.a-color-base.a-text-normal',
productURL: 'a.a-link-normalaliba.s-faceout-link.a-text-normal',
productImage: 'img',
productCurrency: 'span.a-price-symbol',
productPrice: 'span.a-price-whole',
productCurrency: undefined
},
{
name: 'coolblue',
defaultCurrency: 'EUR',
domain: 'https://www.coolblue.be/nl/zoeken?query=',
resultItem: '.product-grid__card',
productName: '.product-card__title h3.text-color--link',
productURL: '.product-card__title a.link',
productImage: 'img',
productCurrency: '.sales-price__current',
productPrice: '.sales-price__current',
productCurrency: ''
},
{
name: 'grainger',
defaultCurrency: 'USD',
domain: 'https://www.grainger.com/search?searchBar=true&ts_optout=true&searchQuery=',
//domain: 'https://www.grainger.com/mobile/search?optOut=0&NLSCM=17&searchQuery=',
resultItem: '.product',
productName: '.product__details-link',
productURL: 'a.product__details-link',
productImage: 'img.image',
productPrice: '.pricing__price',
productCurrency: ''
},
{
name: 'msc',
defaultCurrency: 'USD',
domain: 'https://www.mscdirect.com/browse/lookahead/?hdrsrh=true&searchterm=',
resultItem: '.result.clear.reactive ',
productName: '.productName .ui-link',
productURL: '.pidp-link',
productImage: '.image.left',
productPrice: '.price',
productCurrency: ''
},
{
name: 'alibaba',
defaultCurrency: 'USD',
domain: 'https://m.alibaba.com/trade/search?SearchText=',
resultItem: '.product-item',
productName: '.product-title',
productURL: '.product-detail',
productImage: 'img',
productPrice: '.product-price',
productCurrency: 'USD'
},
{
name: 'shi',
defaultCurrency: 'USD',
domain: 'https://punchout.shi.com/shop/search?k=',
resultItem: '.srProduct',
productName: '.srh_pr.pnm',
productURL: '.srh_pr.pnm',
productImage: 'img.productPhoto',
productPrice: 'price-range',
productCurrency: 'USD'
},
{
name: 'google',
defaultCurrency: 'EUR',
domain: 'https://www.google.com/search?newwindow=1&hl=nl&tbm=shop&q=',
resultItem: '.sh-pr__product-result',
productName: '.sh-ti__ltitle-link',
productURL: 'a',
productImage: 'img',
productPrice: '._-Bz._-By',
productCurrency: 'USD'
},
{
name: 'gem',
defaultCurrency: 'INR',
domain: 'https://mkp.gem.gov.in/search?q=',
resultItem: '.variant-wrapper.btn-add_to_cart.rle-add_to_cart bre-.product-init',
productName: '.variant-title > a',
productURL: '.variant-title > a',
productImage: 'img',
productPrice: '.m-w',
productCurrency: 'INRUSD'
}
]
// App
const app = express();
app.set('port', 8080);
app.get('/scrapeProducts/:marketplace', async (req, res) => {
let product = req.query.product
let marketPlace = req.params.marketplace
console.log("What is the marketplace: " + marketPlace)
console.log("What is the product: " + product)
let browser = undefined
let page = undefined
if((product != undefined) && (marketPlace != undefined)) {
try {
let SELECTORS = marketplaces.find(el => el.name.toUpperCase() === marketPlace.toUpperCase())
browser = await puppeteer.launch(
{
headless: true, //Change it back
defaultViewport: {
width: 360, //1240
height: 640 //820
},
args: [
'--ignore-certificate-errors',
'--no-sandbox',
'--disable-setuid-sandbox',
'--window-size=360,640',
"--disable-accelerated-2d-canvas",
"--disable-gpu",
'--disable-dev-shm-usage'
]
}
)
page = await browser.newPage();
page.on('error', msg => {
console.log('Got an errror.....')
throw msg ;
});
// Configure the navigation timeout
//await page.setDefaultNavigationTimeout(0);
await page.emulate(devices['devicesMap']['iPhone 6'])
//await page.emulate(iphone)
await page.goto(`${SELECTORS.domain}${product}!`, {timeout: 90000})
/* Some special stuff here for Grainger */
//For Grainger > check if the link "nls-opt-out ui-link" exists and if so click it
/*try {
console.log("Checking this element")
await page.$eval('.nls-opt-out', e => {
console.log("Found the element so have to click it ");
return e.click();
});
console.log("Done searching it")
}catch(e) {
console.log("No grainger element found so continuing...")
}*/
console.log("Searching for: " + SELECTORS.resultItem);
const els = await page.$$(SELECTORS.resultItem);
console.log("Got some results: " + els.length);
let products = [];
for (const result of els) {
try {
//First one is price > if not price is found, an exception is thrown and nothing is added to the array
var prodPrice = await result.$eval(SELECTORS.productPrice, el => el.textContent);
//Replace comma by dot
prodPrice = prodPrice.replace(",", ".")
console.log("Prod Price: " + prodPrice)
//Specific to Grainger > check if it is not a range
if(prodPrice.includes('-')) {
let tempPrice = prodPrice.replace(/\s+/g, '')
let split = tempPrice.split('-')
console.log('What is the split: ' + JSON.stringify(split))
prodPrice = split[0].replace(",", ".") //Specifically for alibaba
console.log('This is the prod price: ' + prodPrice)
}
const productName = await result.$eval(SELECTORS.productName, el => el.textContent);
console.log("Got a product name: " + productName)
let productImage = '';
let productUrl = '';
let currency = SELECTORS.defaultCurrency;
console.log("Got a currency: " + currency)
try {
productImage = await result.$eval(SELECTORS.productImage, el => el.src);
console.log("Got an image: " + productImage)
productUrl = await result.$eval(SELECTORS.productURL, el => el.href);
console.log("Got a URL: " + productUrl)
currency = await result.$eval(SELECTORS.productCurrency, el => el.textContent);
console.log('What is the currency: ' + currency);
}catch(error) {
console.log('No currency')
}
let product = {
name: productName,
price: Number(prodPrice.replace(/[^0-9\.]+/g,"")),
image: productImage,
url: productUrl,
currency: currency,
marketplace: SELECTORS.name
}
console.log("Adding the product: " + JSON.stringify(product))
products.push(product);
}catch(error) {
console.log('Nothing found...')
}
//
//console.log(`Image source "${imgSrc}".`)
}
console.log("What are the products: " + JSON.stringify(products))
//await browser.close()
/*await page.goto(SELECTORS.domain, {
timeout: 3000000
});*/
//await page.goto(SELECTORS.domain, {waitUntil: 'networkidle2'});
//Accept the popup
//await page.click('.js-confirm-button');
// search and wait the product list
//await page.type(SELECTORS.searchBox, product);
/*await page.waitForSelector(SELECTORS.searchBox, { visible: true });
await page.$eval(SELECTORS.searchBox, el => el.value = 'kut');
await page.$eval(SELECTORS.searchBox, (el, value) => el.value = value, product);
await page.click(SELECTORS.searchButton);
const response = await page.waitForSelector(SELECTORS.waitFor, {visible: true, timeout: 3000 })
const products = await page.evaluate((SELECTORS) => {
const links = Array.from(document.querySelectorAll(SELECTORS.resultItem));
return links.map(link => {
if (link.querySelector(SELECTORS.wholePrice)) {
return {
name: link.querySelector(SELECTORS.productName).textContent,
url: link.querySelector(SELECTORS.productURL).href,
image: link.querySelector(SELECTORS.productImage).src,
currency: link.querySelector(SELECTORS.productCurrency).textContent,
price: parseFloat(link.querySelector(SELECTORS.productPrice).textContent.replace(/[,.]/g, m => (m === ',' ? '.' : '')))
};
}
});
}, SELECTORS);*/
let fSet = FuzzySet();
for(let prod of products) {
if(prod != null) {
fSet.add(prod.name);
}
}
let result = fSet.get(product, products, 0);
console.log('Result: ' + JSON.stringify(result));
//Take the first 5 products
let index = 0;
let fProducts = [];
for(let res of result) {
if(index < 5) {
let nProd = products.find(el => el.name == res[1]);
fProducts.push(nProd);
}
index++;
}
console.log("Final products: " + JSON.stringify(fProducts))
res.send({ response : 'OK', products: fProducts })
}catch (error) {
// display errors
console.log("Got an error: " + error.message)
res.send({ response : 'KO', error: JSON.stringify(error) });
}finally{
//Close the pages
//var pages = await browser.pages();
//pages.forEach((page) => page.close());
browser.close();
}
}else {
res.status(500).send('Missing "product" and/or "marketplace" query parameter')
}
});
//Start the server
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});