-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
383 lines (356 loc) · 11.7 KB
/
scraper.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("program.db");
const puppeteer = require("puppeteer");
const cheerio = require("cheerio");
const fs = require("fs");
const proxies = fs
.readFileSync("./proxies.txt", "utf8")
.split("\r\n")
.map((proxy) => {
const [ip, port, username, password] = proxy.split(":");
return { ip, port, username, password };
});
function execute(query, params = []) {
return new Promise((resolve, reject) => {
db.all(query, params, function (err, rows) {
if (err) {
reject(err);
} else {
resolve(rows);
}
});
});
}
function run(query, params = []) {
return new Promise((resolve, reject) => {
db.run(query, params, function (err) {
if (err) {
reject(err);
} else {
resolve({ lastID: this.lastID, changes: this.changes });
}
});
});
}
async function Initialization() {
try {
await run(`CREATE TABLE IF NOT EXISTS links (
id TEXT PRIMARY KEY,
link TEXT NOT NULL,
category_id TEXT NOT NULL
)`);
console.log("Table 'links' created.");
await run(`CREATE TABLE IF NOT EXISTS scraped_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
price TEXT,
description TEXT,
seller TEXT,
location TEXT,
priority TEXT,
date TEXT,
link TEXT,
image TEXT,
sent TEXT NOT NULL,
base_category TEXT NOT NULL
)`);
console.log("Table 'scraped_data' created.");
await scrapeData(true);
} catch (err) {
console.error("Initialization error:", err);
}
}
function checkDuplicates(str) {
let regex = /(\S+\s?\S*)\1+/g;
return str.replace(regex, "$1");
}
async function singlePreload(url, proxyIndex = 0) {
let closed = false;
console.log(
"----------------------------------------------------------------"
);
console.log("Processing URL:", url);
const { ip, port, username, password } = proxies[proxyIndex];
console.log("Using proxy:", ip, "Port:", port);
let browser;
let totalBytes = 0;
const data = [];
try {
browser = await puppeteer.launch({
args: [`--proxy-server=${ip}:${port}`],
});
const page = await browser.newPage();
await page.authenticate({
username: username,
password: password,
});
page.on("response", async (response) => {
try {
const buffer = await response.buffer();
totalBytes += buffer.length;
} catch (error) {}
});
await page.goto(url);
console.log("Page loaded successfully");
await page.waitForSelector(".hz-Listing-item-wrapper");
console.log("Selector found");
const html = await page.content();
const $ = cheerio.load(html);
$(".hz-Listing-item-wrapper").each(async (index, element) => {
try {
const title = $(element).find(".hz-Listing-title").text().trim();
const price = $(element).find(".hz-Listing-price").text().trim();
const description = $(element)
.find(".hz-Listing-description")
.text()
.trim();
const seller = $(element).find(".hz-Listing-seller-name").text().trim();
const location = $(element)
.find(".hz-Listing-location .hz-Listing-distance-label")
.text()
.trim();
const priority = $(element)
.find(".hz-Listing-priority span")
.text()
.trim();
const date = $(element).find(".hz-Listing-date").text().trim();
const imageUrl = $(element).find("img").attr("src");
const visitWebsite = $(element)
.find(".hz-Listing-seller-external-link a")
.text()
.trim();
const sanitizedTitle = checkDuplicates(title);
const sanitizedDate = checkDuplicates(date);
const sanitizedPriority = checkDuplicates(priority);
let base = url.split("#")[0];
const visitWebsiteLink = `${base}#q:${(sanitizedTitle + " " + seller)
.replaceAll(" ", "+")
.toLowerCase()}|sortBy:SORT_INDEX|sortOrder:DECREASING|searchInTitleAndDescription:true`;
const existingData = await execute(
`SELECT * FROM scraped_data WHERE title = ?`,
[sanitizedTitle]
);
data.push({
title: sanitizedTitle,
price,
description,
seller,
location,
priority,
date: sanitizedDate,
link: visitWebsiteLink,
image: imageUrl,
});
if (
existingData.length == 0 &&
sanitizedPriority !== "Dagtopper" &&
sanitizedPriority !== "Topadvertentie" &&
!visitWebsite
) {
await run(
`INSERT INTO scraped_data (title, price, description, seller, location, priority, date, link, image, sent, base_category) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
sanitizedTitle,
checkDuplicates(price),
description,
checkDuplicates(seller),
checkDuplicates(location),
checkDuplicates(priority),
sanitizedDate,
visitWebsiteLink,
imageUrl,
"true",
url,
]
);
}
} catch (error) {
console.error("Error processing element:", error);
}
});
} catch (error) {
if (proxyIndex < proxies.length - 1) {
console.log("Proxy on cooldown. Retrying with a different proxy...");
if (browser && !closed) {
closed = true;
const pages = await browser.pages();
await Promise.all(pages.map((page) => page.close()));
await browser.close();
}
return await singlePreload(url, proxyIndex + 1);
} else {
console.error("All proxies failed. Skipping URL:", url);
}
} finally {
if (browser && !closed) {
closed = true;
const pages = await browser.pages();
await Promise.all(pages.map((page) => page.close()));
await browser.close();
}
console.log(
"Total bandwidth used:",
(totalBytes / (1024 * 1024)).toFixed(2),
"MB"
);
console.log("Data:", data);
console.log(
"----------------------------------------------------------------"
);
}
}
async function scrapeData(firstTime, startIndex = 0, proxyIndex = 0) {
let data = await execute(`SELECT * FROM links`);
const { ip, port, username, password } = proxies[proxyIndex];
const browser = await puppeteer.launch({
args: [`--proxy-server=${ip}:${port}`],
});
try {
for (let i = startIndex; i < data.length; i++) {
let url = data[i].link;
console.log("Processing URL:", url);
console.log("Using proxy:", ip, "Port:", port);
const page = await browser.newPage();
await page.authenticate({
username: username,
password: password,
});
let totalBytes = 0;
const newData = [];
try {
page.on("response", async (response) => {
try {
const buffer = await response.buffer();
totalBytes += buffer.length;
} catch (error) {}
});
await page.goto(url);
console.log("Page loaded successfully");
await page.waitForSelector(".hz-Listing-item-wrapper");
console.log("Selector found");
const html = await page.content();
const $ = cheerio.load(html);
let items = $(".hz-Listing-item-wrapper").toArray();
let itemCount = parseInt(items.length * 0.75);
console.log("Item count:", itemCount);
for (let j = 0; j < itemCount; j++) {
let element = items[j];
const title = $(element).find(".hz-Listing-title").text().trim();
const price = $(element).find(".hz-Listing-price").text().trim();
const description = $(element)
.find(".hz-Listing-description")
.text()
.trim();
const seller = $(element)
.find(".hz-Listing-seller-name")
.text()
.trim();
const location = $(element)
.find(".hz-Listing-location .hz-Listing-distance-label")
.text()
.trim();
const priority = $(element)
.find(".hz-Listing-priority span")
.text()
.trim();
const date = $(element).find(".hz-Listing-date").text().trim();
const imageUrl = $(element).find("img").attr("src");
const visitWebsite = $(element)
.find(".hz-Listing-seller-external-link a")
.text()
.trim();
const sanitizedTitle = checkDuplicates(title);
const sanitizedDate = checkDuplicates(date);
const sanitizedPriority = checkDuplicates(priority);
let base = url.split("#")[0];
const visitWebsiteLink = `${base}#q:${(sanitizedTitle + " " + seller)
.replaceAll(" ", "+")
.toLowerCase()}|sortBy:SORT_INDEX|sortOrder:DECREASING|searchInTitleAndDescription:true`;
const existingData = await execute(
`SELECT * FROM scraped_data WHERE title = ?`,
[sanitizedTitle]
);
if (
!visitWebsite &&
existingData.length == 0 &&
sanitizedPriority !== "Dagtopper" &&
sanitizedPriority !== "Topadvertentie"
) {
await run(
`INSERT INTO scraped_data (title, price, description, seller, location, priority, date, link, image, sent, base_category) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
sanitizedTitle,
checkDuplicates(price),
checkDuplicates(description),
checkDuplicates(seller),
checkDuplicates(location),
checkDuplicates(priority),
sanitizedDate,
visitWebsiteLink,
imageUrl,
firstTime ? "true" : "false",
url,
]
);
newData.push({
title: sanitizedTitle,
price,
description,
seller,
location,
priority,
date: sanitizedDate,
link: visitWebsiteLink,
image: imageUrl,
});
}
}
} catch (error) {
if (proxyIndex < proxies.length - 1) {
console.log("Proxy on cooldown. Retrying with a different proxy...");
if (browser) {
const pages = await browser.pages();
await Promise.all(pages.map((page) => page.close()));
await browser.close();
}
await scrapeData(firstTime, i, proxyIndex + 1);
return;
} else {
console.error("All proxies failed. Skipping URL:", url);
}
} finally {
await page.close();
if (newData.length > 0) {
console.log("New items found:\n", newData);
} else {
console.log("No new items.");
}
console.log(
"Total bandwidth used:",
(totalBytes / (1024 * 1024)).toFixed(2),
"MB"
);
console.log(
"----------------------------------------------------------------"
);
}
}
} catch (error) {
} finally {
if (browser) {
const pages = await browser.pages();
await Promise.all(pages.map((page) => page.close()));
await browser.close();
}
}
if (startIndex == 0) {
console.log(
"----------------------------------------------------------------"
);
console.log("DONE SCRAPING EVERYTHING IN THIS ROUND.");
console.log(
"----------------------------------------------------------------"
);
}
}
module.exports = { execute, run, singlePreload, Initialization, scrapeData };