forked from eshaham/israeli-bank-scrapers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-scraper.ts
105 lines (87 loc) · 3.5 KB
/
base-scraper.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
import { EventEmitter } from 'events';
import moment from 'moment-timezone';
import { TimeoutError } from '../helpers/waiting';
import { createGenericError, createTimeoutError } from './errors';
import {
Scraper,
ScraperCredentials,
ScraperGetLongTermTwoFactorTokenResult,
ScraperLoginResult,
ScraperOptions,
ScraperScrapingResult,
ScraperTwoFactorAuthTriggerResult,
} from './interface';
import { CompanyTypes, ScraperProgressTypes } from '../definitions';
const SCRAPE_PROGRESS = 'SCRAPE_PROGRESS';
export class BaseScraper<TCredentials extends ScraperCredentials> implements Scraper<TCredentials> {
private eventEmitter = new EventEmitter();
constructor(public options: ScraperOptions) {
}
// eslint-disable-next-line @typescript-eslint/require-await
async initialize() {
this.emitProgress(ScraperProgressTypes.Initializing);
moment.tz.setDefault('Asia/Jerusalem');
}
async scrape(credentials: TCredentials): Promise<ScraperScrapingResult> {
this.emitProgress(ScraperProgressTypes.StartScraping);
await this.initialize();
let loginResult;
try {
loginResult = await this.login(credentials);
} catch (e) {
loginResult = e instanceof TimeoutError ?
createTimeoutError(e.message) :
createGenericError(e.message);
}
let scrapeResult;
if (loginResult.success) {
try {
scrapeResult = await this.fetchData();
} catch (e) {
scrapeResult =
e instanceof TimeoutError ?
createTimeoutError(e.message) :
createGenericError(e.message);
}
} else {
scrapeResult = loginResult;
}
try {
const success = scrapeResult && scrapeResult.success === true;
await this.terminate(success);
} catch (e) {
scrapeResult = createGenericError(e.message);
}
this.emitProgress(ScraperProgressTypes.EndScraping);
return scrapeResult;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
triggerTwoFactorAuth(_phoneNumber: string): Promise<ScraperTwoFactorAuthTriggerResult> {
throw new Error(`triggerOtp() is not created in ${this.options.companyId}`);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
getLongTermTwoFactorToken(_otpCode: string): Promise<ScraperGetLongTermTwoFactorTokenResult> {
throw new Error(`getPermanentOtpToken() is not created in ${this.options.companyId}`);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
protected async login(_credentials: TCredentials): Promise<ScraperLoginResult> {
throw new Error(`login() is not created in ${this.options.companyId}`);
}
// eslint-disable-next-line @typescript-eslint/require-await
protected async fetchData(): Promise<ScraperScrapingResult> {
throw new Error(`fetchData() is not created in ${this.options.companyId}`);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/require-await
protected async terminate(_success: boolean) {
this.emitProgress(ScraperProgressTypes.Terminating);
}
protected emitProgress(type: ScraperProgressTypes) {
this.emit(SCRAPE_PROGRESS, { type });
}
protected emit(eventName: string, payload: Record<string, any>) {
this.eventEmitter.emit(eventName, this.options.companyId, payload);
}
onProgress(func: (companyId: CompanyTypes, payload: {type: ScraperProgressTypes}) => void) {
this.eventEmitter.on(SCRAPE_PROGRESS, func);
}
}