-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapers.ts
116 lines (93 loc) · 2.98 KB
/
scrapers.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
import { Fetcher } from "./fetcher";
export type MessageType = "info" | "warning" | "error";
export interface ScraperConfiguration {
messageHandler: (message: string, type: MessageType) => void,
itemHandler: (item: ScraperItem) => void,
entryHandler: (entry: ScraperEntry) => void,
runHandler?: (handler: ScraperHandler, input: string) => Promise<void>,
urlValidator?: (url: string) => string,
}
export type ScraperHandler = (input: string) => Promise<void>;
export class Scraper {
private _domain: string;
private _handler: ScraperHandler;
private _items: ScraperItem[]= [];
private _fetcher: Fetcher;
public get domain() {
return this._domain;
}
public get fetcher() {
return this._fetcher;
}
public get handler() {
return this._handler;
}
constructor(private configuration: ScraperConfiguration) {
this._fetcher = new Fetcher(this, configuration);
}
public info(message: string) {
this.configuration.messageHandler(message, "info");
}
public warn(message: string) {
this.configuration.messageHandler(message, "warning");
}
public error(message: string) {
this.configuration.messageHandler(message, "error");
}
public setNetworkDomain(networkDomain: string) {
this._domain = networkDomain;
}
public getSecret() {
return "";
}
public setHandler(handler: ScraperHandler) {
this._handler = handler;
}
public item() {
const item = new ScraperItem(this, this._items.length, this.configuration);
this._items.push(item);
return item;
}
public async run(input: string) {
if(this.configuration.runHandler) {
this.configuration.runHandler(this._handler, input);
}
}
}
export class ScraperItem {
constructor(public readonly scraper: Scraper, public readonly index: number, private configuration: ScraperConfiguration) {
this.configuration.itemHandler(this);
}
public info(message: string) {
this.scraper.info(`${this.index}: ${message}`);
}
public warn(message: string) {
this.scraper.warn(`${this.index}: ${message}`);
}
public error(message: string) {
this.scraper.error(`${this.index}: ${message}`);
}
public entry(path: string, value: string | number | string[] | number[]) {
if(Array.isArray(value)) {
for(const item of value) {
this.entry(path, item);
}
} else if(value != null) {
if(typeof value == "number") {
value = value.toString();
}
value = value.trim();
if(value != "") {
this.configuration.entryHandler(new ScraperEntry(this, path, value));
}
}
return this;
}
}
export class ScraperEntry {
public get scraper() {
return this.item.scraper;
}
constructor(public readonly item: ScraperItem, public readonly path: string, public readonly value: string) {
}
}