-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh2hubwatches.js
300 lines (231 loc) · 7.91 KB
/
h2hubwatches.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
const puppeteer = require("puppeteer");
const { productsFun } = require("./Products");
const { NotFoundedProductsFun } = require("./NotFound");
const ExcelJS = require("exceljs");
const NOT_FOUND = "not found";
let ScrapedDataIndicator = false;
// const products = [
// "NY0138-14X",
// "NY0099-81XB",
// "NH8350-83AB",
// "NB6021-68L",
// "FE7040-53E",
// "EU6094-53A",
// "ER0216-59D",
// "ER0214-54D",
// ];
const SOURCE_FILE = "./Search Products/Data.xlsx";
const NOT_FOUND_FILE = "./Search products/NotFound.xlsx";
const SearchedProductsArray = [];
// main();
async function getExcelFileFromH2HubWatches() {
const browser = await puppeteer.launch({ headless: "new" });
const page = await browser.newPage();
// const products = ["BI5095-05E"];
// console.log("start");
let products = await productsFun(SOURCE_FILE);
products.reverse();
// console.log(products);
let counter = 0;
for (let { Title: productTitle, IsScraped } of products) {
if (!IsScraped) {
console.log(++counter);
const url = urls(productTitle);
// console.log(productTitle);
const productPage = await getProductPage(page, url);
if (productPage == NOT_FOUND) {
// console.log(NOT_FOUND);
SearchedProductsArray.push({
Title: productTitle,
Name: NOT_FOUND,
Description: NOT_FOUND,
Images: [NOT_FOUND],
});
} else {
const productInfo = await ProductInfo(page, productTitle, productPage);
if (productInfo == NOT_FOUND) {
SearchedProductsArray.push({
Title: productTitle,
Name: NOT_FOUND,
Description: NOT_FOUND,
Images: [NOT_FOUND],
});
} else {
let { Name, Description, Images } = productInfo;
// console.log(Name);
// console.log(Images);
SearchedProductsArray.push({
Title: productTitle,
Name,
Description,
Images: [...Images],
});
}
}
} else {
ScrapedDataIndicator = true;
}
// console.log(productPage);
}
// console.log(SearchedProductsArray);
await PopulateExcelFile(SearchedProductsArray, ScrapedDataIndicator);
// console.log("finish");
await browser.close();
}
async function getProductPage(page, pageUrl) {
await page.goto(pageUrl);
let [e] = await page.$$(
"#shopify-section-search_page > div > div.row > div > div.products.nt_products_holder.row.fl_center.row_pr_1.cdt_des_1.round_cd_false.nt_cover.ratio_nt.position_8.space_30.nt_default > div:nth-child(1) > div > div.product-image.pr.oh.lazyloaded > div.pr.oh > a"
);
if (e == undefined) {
return NOT_FOUND;
}
let href = await e.getProperty("href");
href = await href.jsonValue();
return href;
}
async function ProductInfo(page, productTitle, PageUrl) {
await page.goto(PageUrl);
let result = {};
let [RightTitle] = await page.$x('//*[@id="pr_sku_ppr"]');
RightTitle = await page.evaluate((e) => e.textContent, RightTitle);
if (RightTitle.trim().toLowerCase() != productTitle.trim().toLowerCase()) {
// console.log(NOT_FOUND);
return NOT_FOUND;
}
// ---------------------------------
let [Name] = await page.$x('//*[@id="shopify-section-pr_summary"]/h1');
Name = await page.evaluate((e) => e.textContent, Name);
result.Name = Name;
// ----------------------------------------------
try {
await page.click(
"div.col-md-6.col-12.pr.product-images.img_action_zoom.pr_sticky_img > div > div.col-12.col-lg.col_thumb > div.p_group_btns.pa.flex > button"
);
} catch {
await page.click(
"div.col-md-6.col-12.pr.product-images.img_action_zoom.pr_sticky_img > div > div.col-12 > div.p_group_btns.pa.flex > button"
);
}
let counter = 0;
let Images = [];
do {
await page.waitForTimeout(400);
counter += 1;
Images = await page.evaluate(() => {
const imgElements = document.querySelectorAll(".pswp__img");
// Replace with your appropriate selector
const srcs = Array.from(imgElements)
.map((img) => img.getAttribute("src"))
.filter((img) => img != null);
return srcs;
});
} while (Images.length == 0 && counter < 5);
// console.log(Images);
// console.log(Images);
result.Images = [...Images];
// ---------------------------------------------
const [e] = await page.$x('//*[@id="tab_pr_deskl"]/div[3]');
const [table] = await e.$$("table");
const [tbody] = await table.$$("tbody");
const trs = await tbody.$$("tr");
let text = "";
for (let tr of trs) {
let tds = await tr.$$("td");
for (let i = 0; i < tds.length; i++) {
let td = tds[i];
text += await page.evaluate((e) => e.textContent, td);
text = text.trim();
if (i != tds.length - 1) {
text += " : ";
}
}
text += "\n";
}
result.Description = text;
return { ...result };
}
function urls(term) {
let RootUrl =
"https://www.h2hubwatches.com/search?type=product&options%5Bunavailable_products%5D=hide&options%5Bprefix%5D=none&q=";
let url = `${RootUrl}${term}`;
return url;
}
async function PopulateExcelFile(SearchedProducts, indicator) {
if (!indicator) {
const workbook = new ExcelJS.Workbook();
const workSheet = workbook.addWorksheet("Scraped Data");
workSheet.addRow(["Title", "Name", "Description", "images"]);
for (let product of SearchedProducts) {
let { Title, Name, Description, Images } = product;
workSheet.addRow([Title, Name, Description, ...Images]);
// console.log(Image);
}
await workbook.xlsx.writeFile(SOURCE_FILE);
} else {
await updateExcelFile(SearchedProducts, SOURCE_FILE);
}
// await NotFoundProductExcelFile(NOT_FOUND_FILE);
// await EliminateNotFoundInData(SearchedProducts);
}
async function updateExcelFile(newProductsInfo, file) {
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(file);
// Get the first worksheet
const worksheet = workbook.getWorksheet(1);
for (let product of newProductsInfo) {
worksheet.eachRow({ includeEmpty: true }, (row, rowNumber) => {
//
if (rowNumber != 1) {
row.eachCell({ includeEmpty: true }, (cell, colNumber) => {
if (colNumber == 1 && cell.value == product.Title) {
row.getCell(2).value = product.Name;
row.getCell(3).value = product.Description;
let count = 4;
for (let img of product.Images) {
row.getCell(count++).value = img;
}
row.commit();
}
});
}
});
}
await workbook.xlsx.writeFile(file);
}
async function NotFoundProductExcelFile(file, func = NotFoundedProductsFun) {
const workbook = new ExcelJS.Workbook();
const workSheet = workbook.addWorksheet("Scraped Data");
workSheet.addRow(["Title"]);
let notFoundProducts = await func(SOURCE_FILE);
for (let product of notFoundProducts) {
let { Title } = product;
workSheet.addRow([Title]);
}
await workbook.xlsx.writeFile(file);
}
// async function EliminateNotFoundInData(SearchedProducts) {
// const workbook = new ExcelJS.Workbook();
// const workSheet = workbook.addWorksheet("Scraped Data");
// console.log(SearchedProducts);
// workSheet.addRow(["Title", "Name", "Description", "images"]);
// let products = await productsFun(NOT_FOUND_FILE);
// console.log(products);
// products.reverse();
// for (let product of SearchedProducts) {
// let { Title, Name, Description, Images } = product;
// let title = Title;
// let Test = false;
// for (let { Title: NotFoundProductTitle } of products) {
// if (title == NotFoundProductTitle) {
// Test = true;
// break;
// }
// }
// if (!Test) {
// workSheet.addRow([Title, Name, Description, ...Images]);
// }
// }
// await workbook.xlsx.writeFile(SOURCE_FILE);
// }
module.exports = { getExcelFileFromH2HubWatches };