/
base-scraper-with-browser.ts
296 lines (248 loc) · 9.29 KB
/
base-scraper-with-browser.ts
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
import puppeteer, { Browser, Frame, Page } from 'puppeteer';
import {
ScraperErrorTypes,
BaseScraper, ScaperScrapingResult, ScaperProgressTypes,
ScraperCredentials,
} from './base-scraper';
import { getCurrentUrl, waitForNavigation } from '../helpers/navigation';
import { clickButton, fillInput, waitUntilElementFound } from '../helpers/elements-interactions';
import { getDebug } from '../helpers/debug';
const VIEWPORT_WIDTH = 1024;
const VIEWPORT_HEIGHT = 768;
const OK_STATUS = 200;
const debug = getDebug('base-scraper-with-browser');
enum LoginBaseResults {
Success = 'SUCCESS',
UnknownError = 'UNKNOWN_ERROR'
}
const {
Timeout, Generic, General, ...rest
} = ScraperErrorTypes;
export const LoginResults = {
...rest,
...LoginBaseResults,
};
export type LoginResults = Exclude<ScraperErrorTypes,
ScraperErrorTypes.Timeout
| ScraperErrorTypes.Generic
| ScraperErrorTypes.General> | LoginBaseResults;
export type PossibleLoginResults = {
[key in LoginResults]?: (string | RegExp | ((options?: { page?: Page}) => Promise<boolean>))[]
};
export interface LoginOptions {
loginUrl: string;
checkReadiness?: () => Promise<void>;
fields: {selector: string, value: string}[];
submitButtonSelector: string | (() => Promise<void>);
preAction?: () => Promise<Frame | void>;
postAction?: () => Promise<void>;
possibleResults: PossibleLoginResults;
userAgent?: string;
}
async function getKeyByValue(object: PossibleLoginResults, value: string, page: Page): Promise<LoginResults> {
const keys = Object.keys(object);
for (const key of keys) {
// @ts-ignore
const conditions = object[key];
for (const condition of conditions) {
let result = false;
if (condition instanceof RegExp) {
result = condition.test(value);
} else if (typeof condition === 'function') {
result = await condition({ page, value });
} else {
result = value.toLowerCase() === condition.toLowerCase();
}
if (result) {
// @ts-ignore
return Promise.resolve(key);
}
}
}
return Promise.resolve(LoginResults.UnknownError);
}
function handleLoginResult(scraper: BaseScraperWithBrowser, loginResult: LoginResults) {
switch (loginResult) {
case LoginResults.Success:
scraper.emitProgress(ScaperProgressTypes.LoginSuccess);
return { success: true };
case LoginResults.InvalidPassword:
case LoginResults.UnknownError:
scraper.emitProgress(ScaperProgressTypes.LoginFailed);
return {
success: false,
errorType: loginResult === LoginResults.InvalidPassword ? ScraperErrorTypes.InvalidPassword :
ScraperErrorTypes.General,
errorMessage: `Login failed with ${loginResult} error`,
};
case LoginResults.ChangePassword:
scraper.emitProgress(ScaperProgressTypes.ChangePassword);
return {
success: false,
errorType: ScraperErrorTypes.ChangePassword,
};
default:
throw new Error(`unexpected login result "${loginResult}"`);
}
}
function createGeneralError(): ScaperScrapingResult {
return {
success: false,
errorType: ScraperErrorTypes.General,
};
}
class BaseScraperWithBrowser extends BaseScraper {
// NOTICE - it is discourage to use bang (!) in general. It is used here because
// all the classes that inherit from this base assume is it mandatory.
protected browser!: Browser;
// NOTICE - it is discourage to use bang (!) in general. It is used here because
// all the classes that inherit from this base assume is it mandatory.
protected page!: Page;
protected getViewPort() {
return {
width: VIEWPORT_WIDTH,
height: VIEWPORT_HEIGHT,
};
}
async initialize() {
debug('initialize scraper');
this.emitProgress(ScaperProgressTypes.Initializing);
let env: Record<string, any> | undefined;
if (this.options.verbose) {
env = { DEBUG: '*', ...process.env };
}
if (typeof this.options.browser !== 'undefined' && this.options.browser !== null) {
debug('use custom browser instance provided in options');
this.browser = this.options.browser;
} else {
const executablePath = this.options.executablePath || undefined;
const args = this.options.args || [];
const headless = !this.options.showBrowser;
debug(`launch a browser with headless mode = ${headless}`);
this.browser = await puppeteer.launch({
env,
headless,
executablePath,
args,
});
}
if (this.options.prepareBrowser) {
debug('execute \'prepareBrowser\' interceptor provided in options');
await this.options.prepareBrowser(this.browser);
}
if (!this.browser) {
debug('failed to initiate a browser, exit');
return;
}
const pages = await this.browser.pages();
if (pages.length) {
debug('browser has already pages open, use the first one');
[this.page] = pages;
} else {
debug('create a new browser page');
this.page = await this.browser.newPage();
}
if (this.options.preparePage) {
debug('execute \'preparePage\' interceptor provided in options');
await this.options.preparePage(this.page);
}
const viewport = this.getViewPort();
debug(`set viewport to width ${viewport.width}, height ${viewport.height}`);
await this.page.setViewport({
width: viewport.width,
height: viewport.height,
});
this.page.on('requestfailed', (request) => {
debug('Request failed: %s %s', request.failure()?.errorText, request.url());
});
}
async navigateTo(url: string, page?: Page, timeout?: number): Promise<void> {
const pageToUse = page || this.page;
if (!pageToUse) {
return;
}
const options = { ...(timeout === null ? null : { timeout }) };
const response = await pageToUse.goto(url, options);
// note: response will be null when navigating to same url while changing the hash part. the condition below will always accept null as valid result.
if (response !== null && (response === undefined || response.status() !== OK_STATUS)) {
throw new Error(`Error while trying to navigate to url ${url}`);
}
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
getLoginOptions(_credentials: ScraperCredentials): LoginOptions {
throw new Error(`getLoginOptions() is not created in ${this.options.companyId}`);
}
async fillInputs(pageOrFrame: Page | Frame, fields: { selector: string, value: string}[]): Promise<void> {
const modified = [...fields];
const input = modified.shift();
if (!input) {
return;
}
await fillInput(pageOrFrame, input.selector, input.value);
if (modified.length) {
await this.fillInputs(pageOrFrame, modified);
}
}
async login(credentials: Record<string, string>): Promise<ScaperScrapingResult> {
if (!credentials || !this.page) {
return createGeneralError();
}
debug('execute login process');
const loginOptions = this.getLoginOptions(credentials);
if (loginOptions.userAgent) {
debug('set custom user agent provided in options');
await this.page.setUserAgent(loginOptions.userAgent);
}
debug('navigate to login url');
await this.navigateTo(loginOptions.loginUrl);
if (loginOptions.checkReadiness) {
debug('execute \'checkReadiness\' interceptor provided in login options');
await loginOptions.checkReadiness();
} else if (typeof loginOptions.submitButtonSelector === 'string') {
debug('wait until submit button is available');
await waitUntilElementFound(this.page, loginOptions.submitButtonSelector);
}
let loginFrameOrPage: (Page | Frame | null) = this.page;
if (loginOptions.preAction) {
debug('execute \'preAction\' interceptor provided in login options');
loginFrameOrPage = await loginOptions.preAction() || this.page;
}
debug('fill login components input with relevant values');
await this.fillInputs(loginFrameOrPage, loginOptions.fields);
debug('click on login submit button');
if (typeof loginOptions.submitButtonSelector === 'string') {
await clickButton(loginFrameOrPage, loginOptions.submitButtonSelector);
} else {
await loginOptions.submitButtonSelector();
}
this.emitProgress(ScaperProgressTypes.LoggingIn);
if (loginOptions.postAction) {
debug('execute \'postAction\' interceptor provided in login options');
await loginOptions.postAction();
} else {
debug('wait for page navigation');
await waitForNavigation(this.page);
}
debug('check login result');
const current = await getCurrentUrl(this.page, true);
const loginResult = await getKeyByValue(loginOptions.possibleResults, current, this.page);
debug(`handle login results ${loginResult}`);
return handleLoginResult(this, loginResult);
}
async terminate(_success: boolean) {
debug(`terminating browser with success = ${_success}`);
this.emitProgress(ScaperProgressTypes.Terminating);
if (!_success && !!this.options.storeFailureScreenShotPath) {
debug(`create a snapshot before terminated in ${this.options.storeFailureScreenShotPath}`);
await this.page.screenshot({
path: this.options.storeFailureScreenShotPath,
fullPage: true,
});
}
if (!this.browser) {
return;
}
await this.browser.close();
}
}
export { BaseScraperWithBrowser };