-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
219 lines (183 loc) · 6.18 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
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
const webdriver = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const { Builder, By } = webdriver;
const process = require("node:process");
const fs = require("fs");
const options = new chrome.Options().headless();
let exited = false;
const driver = new Builder()
.forBrowser("chrome")
.setChromeOptions(options)
.build();
async function getListOfProductCategories(driver) {
let result = [];
let xpath =
'//div[contains(@class,"product-categories")]//div[contains(@class,"product-category with-icons")]';
let locator = By.xpath(xpath);
let elements = await driver.wait(driver.findElements(locator));
for (let i in elements) {
const dataHashValue = await elements[i].getAttribute("data-hash-value");
const innerText = (await elements[i].getAttribute("innerText")).trim();
result.push({
id: dataHashValue,
name: innerText,
});
}
return result;
}
async function getListOfProductsForCategory(driver, category) {
let result = [];
let xpath = `//div[contains(@class,"product-category ${category}")]`;
let locator = By.xpath(xpath);
let elements = await driver.wait(driver.findElements(locator));
for (let i in elements) {
const dataPanelKey = await elements[i].getAttribute("data-panel-key");
const innerText = (await elements[i].getAttribute("innerText")).trim();
result.push({
id: dataPanelKey,
name: innerText.trim(),
});
}
return result;
}
async function getListOfSubProductsForProduct(driver, product) {
let result = [];
let xpath = `//div[@data-parent-panel-key="${product}"]//div[contains(@class,"product") and @data-order]//a`;
let locator = By.xpath(xpath);
let elements = await driver.wait(driver.findElements(locator));
for (let i in elements) {
const innerText = (await elements[i].getAttribute("innerText")).trim();
const href = await elements[i].getAttribute("href");
result.push({
name: innerText,
href: href,
});
}
return result;
}
async function getListOfSKUsForSubProduct(driver) {
let xpath = '//table[@id="product-table"]';
let locator = By.xpath(xpath);
let table = await driver.wait(driver.findElement(locator));
let headers = await table.findElements(By.xpath("//thead//th"));
let rows = await table.findElements(By.xpath("//tbody//tr"));
let headersText = [];
for (let i in headers) {
headersText.push((await headers[i].getAttribute("innerText")).trim());
}
let headersLength = headersText.length;
let result = [];
for (let j = 0; j < rows.length; j++) {
let item = {};
for (let k = 0; k < headersLength; k++) {
let xpath = `//table[@id="product-table"]//tbody//tr[${j + 1}]//td[${
k + 1
}]`;
let locator = By.xpath(xpath);
let td = await driver.findElement(locator);
let text = (await td.getAttribute("innerText")).trim();
item[headersText[k]] = text;
if (k == 0) {
let xpath = `//table[@id="product-table"]//tbody//tr[${j + 1}]//td[${
k + 1
}]//a`;
let locator = By.xpath(xpath);
let a = await driver.findElement(locator);
let href = await a.getAttribute("href");
item["Url"] = href;
}
}
result.push(item);
}
return result;
}
async function getSpecsForSKU(driver) {
let xpath = '//section[@class="blade specs-blade specifications"]';
let locator = By.xpath(xpath);
let specs = await driver.findElements(locator);
let result = {};
for (let i = 0; i < specs.length; i++) {
let xpath1 = `//section[@class="blade specs-blade specifications"][${
i + 1
}]//h2`;
let locator1 = By.xpath(xpath1);
let specTitle = (
await driver.findElement(locator1).getAttribute("innerText")
).trim();
result[specTitle] = {};
let xpath2 = `//section[@class="blade specs-blade specifications"][${
i + 1
}]//ul[@class="specs-list"]//li//span[@class="label"]`;
let locator2 = By.xpath(xpath2);
let specLabels = await driver.findElements(locator2);
let xpath3 = `//section[@class="blade specs-blade specifications"][${
i + 1
}]//ul[@class="specs-list"]//li//span[@class="value"]`;
let locator3 = By.xpath(xpath3);
let specValues = await driver.findElements(locator3);
for (let j = 0; j < specLabels.length; j++) {
let specLabel = (await specLabels[j].getAttribute("innerText")).trim();
let specValue = (await specValues[j].getAttribute("innerText")).trim();
result[specTitle][specLabel] = specValue;
}
}
return result;
}
async function main() {
await driver.get("https://ark.intel.com/");
let categoryList = await getListOfProductCategories(driver);
for (let i in categoryList) {
let category = categoryList[i];
category.products = await getListOfProductsForCategory(driver, category.id);
for (let j in category.products) {
let product = category.products[j];
product.subproducts = await getListOfSubProductsForProduct(
driver,
product.id
);
}
}
for (let i in categoryList) {
let category = categoryList[i];
for (let j in category.products) {
let product = category.products[j];
for (let k in product.subproducts) {
let subproduct = product.subproducts[k];
console.log(subproduct.href);
await driver.get(subproduct.href);
try {
subproduct.skus = await getListOfSKUsForSubProduct(driver);
} catch (e) {
console.error(e);
}
}
}
}
for (let i in categoryList) {
let category = categoryList[i];
for (let j in category.products) {
let product = category.products[j];
for (let k in product.subproducts) {
let subproduct = product.subproducts[k];
for (let l in subproduct.skus) {
let sku = subproduct.skus[l];
console.log(sku.Url);
await driver.get(sku.Url);
try {
sku.specs = await getSpecsForSKU(driver);
} catch (e) {
console.error(e);
}
}
}
}
}
await fs.promises.writeFile("db.json", JSON.stringify(categoryList), "utf-8");
}
process.on("beforeExit", async () => {
if (!exited) {
exited = true;
await driver.quit();
}
});
main();