-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.js
267 lines (235 loc) · 6.71 KB
/
helpers.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
import puppeteer from "puppeteer-extra";
import StealthPlugin from "puppeteer-extra-plugin-stealth";
import RecaptchaPlugin from "puppeteer-extra-plugin-recaptcha";
import fs from "fs";
import chalk from "chalk";
import {
randomInteger,
saveLanguages,
parseLanguages,
parseCategories,
saveCategories,
getCurrentDateTime,
} from "./common";
puppeteer.use(StealthPlugin());
puppeteer.use(
RecaptchaPlugin({
provider: {
id: "2captcha",
token: "82cf1e89938d61a3818ef86965c8a312",
},
visualFeedback: false,
})
);
export const LAUNCH_PUPPETEER_OPTS = {
headless: true,
ignoreHTTPSErrors: true,
};
export const PAGE_PUPPETEER_OPTS = {
networkidle2Timeout: 5000,
waitUntil: ["networkidle2", "load"],
};
export class PuppeteerHandler {
constructor(url, userAgent, viewport, languages) {
try {
if (typeof url != "string") {
throw "PuppeteerHandler -> url invalid";
}
if (typeof userAgent != "string") {
throw "PuppeteerHandler -> user-agent invalid";
}
if (
viewport instanceof Object &&
typeof viewport.width != "number" &&
typeof viewport.height != "number"
) {
throw "PuppeteerHandler -> viewport invalid";
}
if (!(languages instanceof Array)) {
throw "PuppeteerHandler -> languages invalid";
}
} catch (err) {
throw err;
}
this.browser = null;
this.houdiniMode = false;
this.url = url;
this.userAgent = userAgent;
this.viewport = viewport;
this.languages = languages;
}
async initBrowser() {
this.browser = await puppeteer.launch(LAUNCH_PUPPETEER_OPTS);
}
closeBrowser() {
this.browser.close();
}
async getScreenshot(page) {
try {
if (typeof page != "object") {
throw "PuppeteerHandler -> getScreenshot -> argument page is not a object";
}
return await page.screenshot({
encoding: "base64",
fullPage: true,
});
} catch (err) {
throw err;
}
}
saveScreenshot(name, base64) {
try {
if (typeof name != "string") {
throw "PuppeteerHandler -> saveScreenshot -> arg: name is not a string";
}
if (typeof base64 != "string") {
throw "PuppeteerHandler -> saveScreenshot -> arg: base64 is not a string";
}
const base64Data = base64.replace(/^data:image\/png;base64,/, "");
fs.writeFile(
`./screenshots/${name}(${getCurrentDateTime()}).png`,
base64Data,
"base64",
function (err) {
if (err) {
throw err;
}
}
);
} catch (err) {
throw err;
}
}
async checkHoudiniMode(page, timeout) {
try {
if (typeof page != "object") {
throw "PuppeteerHandler -> udemyShowLangMenu -> page is not a object";
}
} catch (err) {
throw err;
}
try {
const selector = ".dropdown-toggle.btn.btn-sm.btn-quaternary";
await page.waitForSelector(selector, {
timeout,
});
console.log(chalk.cyan("houdiniMode: inactive"));
} catch (err) {
try {
this.houdiniMode = true;
const selector =
".udlite-select.udlite-select-with-icon.udlite-text-sm";
await page.waitForSelector(selector, {
timeout,
});
console.log(chalk.cyan("houdiniMode: active"));
} catch (err) {
throw err;
}
}
}
async getLanguages(page) {
if (this.languages.length) {
console.log(chalk.green("getLanguages: success"));
return this.languages;
} else {
try {
if (typeof page != "object") {
throw "PuppeteerHandler -> udemyGetLangList -> page is not a object";
}
} catch (err) {
throw err;
}
try {
if (this.houdiniMode) {
const selector =
"footer select.udlite-select.udlite-select-with-icon.udlite-text-sm";
await page.waitForSelector(selector);
this.languages = parseLanguages(
await page.$eval(selector, (e) => e.innerHTML),
this.houdiniMode
);
} else {
const selector = "footer ul.dropdown-menu";
await page.waitForSelector(selector);
this.languages = parseLanguages(
await page.$eval(selector, (e) => e.innerHTML),
this.houdiniMode
);
}
saveLanguages(this.languages);
console.log(chalk.green("getLanguages: success"));
} catch (err) {
throw err;
}
}
}
async solveCaptcha(page, showLog = true) {
for (const frame of page.mainFrame().childFrames()) {
await frame.solveRecaptchas();
}
showLog && console.log(chalk.green("Captcha: success"));
}
async getСategoriesTopics(page, lang, href) {
const url = `https://www.udemy.com${href}?persist_locale=&locale=${lang}`;
const topics = [];
await page.goto(url, PAGE_PUPPETEER_OPTS);
console.log(chalk.cyan("topic url: ", url));
let selector;
if (this.houdiniMode) {
selector = 'a[class^="popular-topics-unit--link-tag-responsive--"]';
} else {
selector = 'a[class^="popular-topics-unit-desktop--topic-button--"]';
}
await page.waitForSelector(selector);
const topicElelemnts = await page.$$(selector);
for (let topicEl of topicElelemnts) {
topics.push(
await page.evaluate((el) => {
return { text: el.innerText, href: el.href };
}, topicEl)
);
}
console.log(chalk.green("getСategoriesTopics: success"));
return topics;
}
async getСategories(page) {
for (let lang of this.languages) {
const url = `${this.url}?persist_locale=&locale=${lang}`;
console.log(chalk.yellow("index url: ", url));
await page.goto(url, PAGE_PUPPETEER_OPTS);
await this.solveCaptcha(page, false);
const cats = parseCategories(await page.content());
for (let cat of cats) {
for (let subCat of cat.subCatList) {
subCat.topics = await this.getСategoriesTopics(
page,
lang,
subCat.href
);
}
}
saveCategories(cats, lang);
await page.waitFor(randomInteger(1500, 2000));
}
}
async start() {
if (!this.browser) {
await this.initBrowser();
}
try {
const page = await this.browser.newPage();
await page.setUserAgent(this.userAgent);
await page.setViewport(this.viewport);
await page.setDefaultTimeout(30000);
await page.goto(this.url, PAGE_PUPPETEER_OPTS);
await page.waitFor(5000);
await this.solveCaptcha(page);
await this.checkHoudiniMode(page, 5000);
await this.getLanguages(page);
await this.getСategories(page);
} catch (err) {
throw err;
}
}
}