This repository was archived by the owner on Nov 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
102 lines (77 loc) · 3.08 KB
/
index.js
File metadata and controls
102 lines (77 loc) · 3.08 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
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
require('chromedriver');
const { By, Key, Builder } = require('selenium-webdriver');
const Chrome = require('selenium-webdriver/chrome');
var morgan = require('morgan')
var express = require('express')
var app = express();
var port = process.env.PORT || 3000;
app.use(morgan('combined'))
const COLOR_SEPARATOR = '-';
const SIZE_SEPARATOR = '-';
const LINE_BREAK = '<br/>';
const TEXT_OPEN_TAG = '<span>';
const TEXT_CLOSE_TAG = '</span>';
const LINE_SEPARATOR = `${LINE_BREAK}--------------------------------${LINE_BREAK}`;
async function getInfo(input, driver) {
let content = '';
let colors = [];
let sizes = [];
// Add product code
await driver.get('https://www.mwc.com.vn/search?s=' + input);
content += `${TEXT_OPEN_TAG}✔️ Mã sp: ${input}${TEXT_CLOSE_TAG}`;
// Add product colors
await driver.findElement(By.css('.product-grid-item a')).click();
const colorElements = await driver.findElements(By.css('.product-option #colorOptions .product-option-item'))
for (let color of colorElements) {
colors.push(await color.getAttribute('title'));
}
content += LINE_BREAK;
content += `${TEXT_OPEN_TAG}✔️ Màu sắc: ${colors.join(COLOR_SEPARATOR)}${TEXT_CLOSE_TAG}`;
// Add product sizes
const sizeElements = await driver.findElements(By.css('.product-option #sizeOptions a.product-option-item'))
for (let size of sizeElements) {
// await size.click();
// const inStock = await driver.findElement(By.css('.product-cart-actions #btnStatus')).getCssValue('display') === 'none';
// if (inStock)
sizes.push(await size.getText());
}
content += LINE_BREAK;
content += `${TEXT_OPEN_TAG}✔️ Size: ${sizes.join(SIZE_SEPARATOR)}${TEXT_CLOSE_TAG}`;
// Add product price
const price = await driver.findElement(By.css('.product-detail-main .product-grid-price .product-grid-price-new-text')).getText();
content += LINE_BREAK;
content += `${TEXT_OPEN_TAG}✔️ Giá: ${price}${TEXT_CLOSE_TAG}`;
return content;
}
function parseInputs(inputs) {
if (!inputs) return [];
// Split and trim input
inputs = inputs.split(/[,.]/).map((item) => item.trim()).filter(n => n);
// Unique input
inputs = [...new Set(inputs)];
return inputs;
}
app.get('/', async function(req, res) {
let contents = [];
const inputs = parseInputs(req.query.codes);
try {
const options = new Chrome.Options();
driver = await new Builder()
.setChromeOptions(options.addArguments('headless', 'disable-dev-shm-usage', 'no-sandbox'))
.forBrowser('chrome')
.build();
for (let input of inputs) {
console.log(`Đang lấy thông tin sản phẩm ${input}`)
contents.push(await getInfo(input, driver));
}
// Log results
res.setHeader('Content-type', 'text/html');
res.send(contents.join(LINE_SEPARATOR));
} catch {
res.send("Lỗi khi lấy thông tin sản phẩm")
}
driver.quit();
});
app.listen(port, function() {
console.log('App listening on port: ', port)
})