-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinput.ts
198 lines (176 loc) · 8.5 KB
/
input.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
import { Actor } from 'apify';
import { BrowserName, CheerioCrawlerOptions, log, ProxyConfiguration } from 'crawlee';
import { firefox } from 'playwright';
import { ContentCrawlerTypes, defaults } from './const.js';
import { UserInputError } from './errors.js';
import type { Input, ContentScraperSettings, OutputFormats, StandbyInput, ContentCrawlerOptions } from './types.js';
/**
* Processes the input and returns an array of crawler settings. This is ideal for startup of STANDBY mode
* because it makes it simple to start all crawlers at once.
*/
export async function processStandbyInput(originalInput: Partial<Input> | Partial<StandbyInput>) {
const { input, searchCrawlerOptions, contentScraperSettings } = await processInputInternal(originalInput, true);
const proxy = await Actor.createProxyConfiguration(input.proxyConfiguration);
const contentCrawlerOptions: ContentCrawlerOptions[] = [
createPlaywrightCrawlerOptions(input, proxy),
createCheerioCrawlerOptions(input, proxy),
];
return { input, searchCrawlerOptions, contentCrawlerOptions, contentScraperSettings };
}
/**
* Processes the input and returns the settings for the crawler.
*/
export async function processInput(originalInput: Partial<Input> | Partial<StandbyInput>) {
const { input, searchCrawlerOptions, contentScraperSettings } = await processInputInternal(originalInput);
const proxy = await Actor.createProxyConfiguration(input.proxyConfiguration);
const contentCrawlerOptions: ContentCrawlerOptions = input.scrapingTool === 'raw-http'
? createCheerioCrawlerOptions(input, proxy)
: createPlaywrightCrawlerOptions(input, proxy);
return { input, searchCrawlerOptions, contentCrawlerOptions, contentScraperSettings };
}
/**
* Processes the input and returns the settings for the crawler (adapted from: Website Content Crawler).
*/
async function processInputInternal(
originalInput: Partial<Input> | Partial<StandbyInput>,
standbyInit: boolean = false,
) {
if (originalInput.outputFormats && typeof originalInput.outputFormats === 'string') {
originalInput.outputFormats = originalInput.outputFormats.split(',').map((format) => format.trim()) as OutputFormats[];
}
// noinspection SuspiciousTypeOfGuard
if (typeof originalInput.blockMedia === 'string') {
originalInput.blockMedia = originalInput.blockMedia === 'true' || originalInput.blockMedia === '1';
}
const input = { ...defaults, ...originalInput } as Input;
validateAndFillInput(input, standbyInit);
const {
debugMode,
dynamicContentWaitSecs,
keepAlive,
serpMaxRetries,
serpProxyGroup,
readableTextCharThreshold,
removeCookieWarnings,
} = input;
log.setLevel(debugMode ? log.LEVELS.DEBUG : log.LEVELS.INFO);
const proxySearch = await Actor.createProxyConfiguration({ groups: [serpProxyGroup] });
const searchCrawlerOptions: CheerioCrawlerOptions = {
keepAlive,
maxRequestRetries: serpMaxRetries,
proxyConfiguration: proxySearch,
autoscaledPoolOptions: { desiredConcurrency: 1 },
};
const contentScraperSettings: ContentScraperSettings = {
debugMode,
dynamicContentWaitSecs,
htmlTransformer: 'none',
maxHtmlCharsToProcess: 1.5e6,
outputFormats: input.outputFormats as OutputFormats[],
readableTextCharThreshold,
removeCookieWarnings,
removeElementsCssSelector: input.removeElementsCssSelector,
};
return { input, searchCrawlerOptions, contentScraperSettings };
}
function createPlaywrightCrawlerOptions(input: Input, proxy: ProxyConfiguration | undefined): ContentCrawlerOptions {
const { keepAlive, maxRequestRetries, initialConcurrency, maxConcurrency, minConcurrency } = input;
return {
type: ContentCrawlerTypes.PLAYWRIGHT,
crawlerOptions: {
headless: true,
keepAlive,
maxRequestRetries,
proxyConfiguration: proxy,
requestHandlerTimeoutSecs: input.requestTimeoutSecs,
launchContext: {
launcher: firefox,
},
browserPoolOptions: {
fingerprintOptions: {
fingerprintGeneratorOptions: {
browsers: [BrowserName.firefox],
},
},
retireInactiveBrowserAfterSecs: 60,
},
autoscaledPoolOptions: {
desiredConcurrency: initialConcurrency === 0 ? undefined : Math.min(initialConcurrency, maxConcurrency),
maxConcurrency,
minConcurrency,
},
},
};
}
function createCheerioCrawlerOptions(input: Input, proxy: ProxyConfiguration | undefined): ContentCrawlerOptions {
const { keepAlive, maxRequestRetries, initialConcurrency, maxConcurrency, minConcurrency } = input;
return {
type: ContentCrawlerTypes.CHEERIO,
crawlerOptions: {
keepAlive,
maxRequestRetries,
proxyConfiguration: proxy,
requestHandlerTimeoutSecs: input.requestTimeoutSecs,
autoscaledPoolOptions: {
desiredConcurrency: initialConcurrency === 0 ? undefined : Math.min(initialConcurrency, maxConcurrency),
maxConcurrency,
minConcurrency,
},
},
};
}
/**
* Validates the input and fills in the default values where necessary.
* Do not validate query parameter when standbyInit is true.
* This is a bit ugly, but it's necessary to avoid throwing an error when the query is not provided in standby mode.
*/
export function validateAndFillInput(input: Input, standbyInit: boolean) {
const validateRange = (
value: number | string | undefined,
min: number,
max: number,
defaultValue: number,
fieldName: string,
) => {
// parse the value as a number to check if it's a valid number
if (value === undefined) {
log.warning(`The \`${fieldName}\` parameter must be defined. Using the default value ${defaultValue} instead.`);
return defaultValue;
} if (typeof value === 'string') {
value = Number(value);
} if (value < min) {
log.warning(`The \`${fieldName}\` parameter must be at least ${min}, but was ${fieldName}. Using ${min} instead.`);
return min;
} if (value > max) {
log.warning(`The \`${fieldName}\` parameter must be at most ${max}, but was ${fieldName}. Using ${max} instead.`);
return max;
}
return value;
};
if (!input.query && !standbyInit) {
throw new UserInputError('The `query` parameter must be provided and non-empty.');
}
input.maxResults = validateRange(input.maxResults, 1, defaults.maxResultsMax, defaults.maxResults, 'maxResults');
input.requestTimeoutSecs = validateRange(input.requestTimeoutSecs, 1, defaults.requestTimeoutSecsMax, defaults.requestTimeoutSecs, 'requestTimeoutSecs');
input.serpMaxRetries = validateRange(input.serpMaxRetries, 0, defaults.serpMaxRetriesMax, defaults.serpMaxRetries, 'serpMaxRetries');
input.maxRequestRetries = validateRange(input.maxRequestRetries, 0, defaults.maxRequestRetriesMax, defaults.maxRequestRetries, 'maxRequestRetries');
if (!input.outputFormats || input.outputFormats.length === 0) {
input.outputFormats = defaults.outputFormats as OutputFormats[];
log.warning(`The \`outputFormats\` parameter must be a non-empty array. Using default value \`${defaults.outputFormats}\`.`);
} else if (input.outputFormats.some((format) => !['text', 'markdown', 'html'].includes(format))) {
throw new UserInputError('The `outputFormats` array may only contain `text`, `markdown`, or `html`.');
}
if (input.serpProxyGroup !== 'GOOGLE_SERP' && input.serpProxyGroup !== 'SHADER') {
throw new UserInputError('The `serpProxyGroup` parameter must be either `GOOGLE_SERP` or `SHADER`.');
}
if (input.dynamicContentWaitSecs >= input.requestTimeoutSecs) {
input.dynamicContentWaitSecs = Math.round(input.requestTimeoutSecs / 2);
}
if (input.scrapingTool !== 'browser-playwright' && input.scrapingTool !== 'raw-http') {
throw new UserInputError('The `scrapingTool` parameter must be either `browser-playwright` or `raw-http`.');
}
// handle case when blockMedia is not defined, coerce blockMedia to boolean
if (input.blockMedia === undefined || input.blockMedia === null) {
input.blockMedia = defaults.blockMedia;
}
}