-
Notifications
You must be signed in to change notification settings - Fork 0
/
thongsia.js
202 lines (163 loc) · 5.52 KB
/
thongsia.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
const { productsFun } = require("./Products");
const { NotFoundedProductsFun } = require("./NotFound");
const puppeteer = require("puppeteer");
const ExcelJS = require("exceljs");
const NOT_FOUND = "not found";
let ScrapedDataIndicator = false;
const SOURCE_FILE = "./Search Products/Data.xlsx";
const NOT_FOUND_FILE = "./Search Products/thongsiaNotFoundData.xlsx";
// let products = ["SRPE05K1", "SRPD85K1", "SRPD83K1", "SRPD81K1"];
let SearchedProductsArray = [];
function ResultObj(
Title,
Name = NOT_FOUND,
Description = NOT_FOUND,
Image = NOT_FOUND
) {
this.Title = Title;
this.Name = Name;
this.Description = Description;
this.Image = Image;
}
// getExcelFileFromThongsia();
async function getExcelFileFromThongsia() {
const browser = await puppeteer.launch({ headless: "new" });
const page = await browser.newPage();
// console.log("start");
// console.log(products);
let products = await productsFun(SOURCE_FILE);
products.reverse();
for (let { Title: productTitle, IsScraped } of products) {
if (!IsScraped) {
let PageUrl = getSearchingPage(productTitle);
let result = await getExactProduct(page, PageUrl);
if (result != NOT_FOUND) {
let { Name, Description, image } = await ProductInfo(page, result);
// console.log(image);
SearchedProductsArray.push(
Object.assign(
{},
new ResultObj(productTitle, Name, Description, image)
)
);
} else {
SearchedProductsArray.push(
Object.assign({}, new ResultObj(productTitle))
);
}
} else {
ScrapedDataIndicator = true;
}
}
// console.log("finish");
SearchedProductsArray.reverse();
await PopulatingExcelFile(SearchedProductsArray, ScrapedDataIndicator);
await browser.close();
}
async function getExactProduct(page, PageUrl) {
// const browser = await puppeteer.launch({ headless: "new" });
// const page = await browser.newPage();
// console.log("into getEx");
await page.goto(PageUrl);
// i think you need extra check here,for the exact product key
let [a] = await page.$x('//*[@id="content"]/div[2]/div/div/a[1]');
let value;
if (a != undefined) {
const href = await a.getProperty("href");
value = await href.jsonValue();
} else {
value = NOT_FOUND;
}
// await browser.close();
return value;
}
async function ProductInfo(page, productPageUrl) {
// const browser = await puppeteer.launch({ headless: "new" });
// const page = await browser.newPage();
// console.log("into prodinfo");
await page.goto(productPageUrl);
const [e1] = await page.$x(
'//*[@id="content"]/div[2]/div/div[2]/div/div[2]/div[2]'
);
const Name = await page.evaluate((e) => e.textContent, e1);
const [e2] = await page.$x(
'//*[@id="content"]/div[2]/div/div[2]/div/div[2]/div[4]'
);
const Description = await page.evaluate((e) => e.textContent, e2);
const [e3] = await page.$x(
'//*[@id="content"]/div[2]/div/div[2]/div/div[1]/div[1]/img'
);
let src = await e3.getProperty("src");
src = await src.jsonValue();
return { Name, Description, image: src };
// browser.close();
}
function getSearchingPage(ProductTitle) {
let baseUrl = "https://www.thongsia.com.sg/en/seiko/search/?search=";
return baseUrl + ProductTitle;
}
async function PopulatingExcelFile(SearchingProducts, indicator) {
if (!indicator) {
const workbook = new ExcelJS.Workbook();
const workSheet = workbook.addWorksheet("Scraped Data");
workSheet.addRow(["Title", "Name", "Description", "images"]);
for (let product of SearchingProducts) {
let { Title, Name, Description, Image } = product;
// console.log(Image);
if (Name == "not found" || Description == "not found") {
workSheet.addRow([Title]);
} else {
workSheet.addRow([Title, Name, Description, Image]);
}
}
await workbook.xlsx.writeFile(SOURCE_FILE);
} else {
await updateExcelFile(SearchingProducts, SOURCE_FILE);
}
// await NotFoundProductExcelFile(NOT_FOUND_FILE);
}
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) {
if (
!(product.Name == NOT_FOUND || product.Description == NOT_FOUND)
) {
row.getCell(2).value = product.Name;
row.getCell(3).value = product.Description;
let count = 4;
try {
for (let img of Images) {
row.getCell(count++).value = img;
}
} catch {
console.log("images problem");
}
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);
}
module.exports = { getExcelFileFromThongsia };