-
Notifications
You must be signed in to change notification settings - Fork 799
/
Copy pathcrawler_commons.ts
328 lines (285 loc) · 12.6 KB
/
crawler_commons.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import type { BatchAddRequestsResult, Dictionary } from '@crawlee/types';
// @ts-expect-error This throws a compilation error due to got-scraping being ESM only but we only import types, so its alllll gooooood
import type { OptionsInit, Response as GotResponse } from 'got-scraping';
import type { ReadonlyDeep } from 'type-fest';
import type { Configuration } from '../configuration';
import type { EnqueueLinksOptions } from '../enqueue_links/enqueue_links';
import type { Log } from '../log';
import type { ProxyInfo } from '../proxy_configuration';
import type { Request, Source } from '../request';
import type { Session } from '../session_pool/session';
import type { Dataset, RecordOptions, RequestQueueOperationOptions } from '../storages';
import { KeyValueStore } from '../storages';
/** @internal */
export type IsAny<T> = 0 extends 1 & T ? true : false;
/** @internal */
export type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
export type LoadedRequest<R extends Request> = WithRequired<R, 'id' | 'loadedUrl'>;
/** @internal */
export type LoadedContext<Context extends RestrictedCrawlingContext> = IsAny<Context> extends true
? Context
: {
request: LoadedRequest<Context['request']>;
} & Omit<Context, 'request'>;
export interface RestrictedCrawlingContext<UserData extends Dictionary = Dictionary>
// we need `Record<string & {}, unknown>` here, otherwise `Omit<Context>` is resolved badly
extends Record<string & {}, unknown> {
id: string;
session?: Session;
/**
* An object with information about currently used proxy by the crawler
* and configured by the {@apilink ProxyConfiguration} class.
*/
proxyInfo?: ProxyInfo;
/**
* The original {@apilink Request} object.
*/
request: Request<UserData>;
/**
* This function allows you to push data to a {@apilink Dataset} specified by name, or the one currently used by the crawler.
*
* Shortcut for `crawler.pushData()`.
*
* @param [data] Data to be pushed to the default dataset.
*/
pushData(data: ReadonlyDeep<Parameters<Dataset['pushData']>[0]>, datasetIdOrName?: string): Promise<void>;
/**
* This function automatically finds and enqueues links from the current page, adding them to the {@apilink RequestQueue}
* currently used by the crawler.
*
* Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions
* and override settings of the enqueued {@apilink Request} objects.
*
* Check out the [Crawl a website with relative links](https://crawlee.dev/js/docs/examples/crawl-relative-links) example
* for more details regarding its usage.
*
* **Example usage**
*
* ```ts
* async requestHandler({ enqueueLinks }) {
* await enqueueLinks({
* globs: [
* 'https://www.example.com/handbags/*',
* ],
* });
* },
* ```
*
* @param [options] All `enqueueLinks()` parameters are passed via an options object.
*/
enqueueLinks: (options?: ReadonlyDeep<Omit<EnqueueLinksOptions, 'requestQueue'>>) => Promise<unknown>;
/**
* Add requests directly to the request queue.
*
* @param requests The requests to add
* @param options Options for the request queue
*/
addRequests: (
requestsLike: ReadonlyDeep<(string | Source)[]>,
options?: ReadonlyDeep<RequestQueueOperationOptions>,
) => Promise<void>;
/**
* Returns the state - a piece of mutable persistent data shared across all the request handler runs.
*/
useState: <State extends Dictionary = Dictionary>(defaultValue?: State) => Promise<State>;
/**
* Get a key-value store with given name or id, or the default one for the crawler.
*/
getKeyValueStore: (
idOrName?: string,
) => Promise<Pick<KeyValueStore, 'id' | 'name' | 'getValue' | 'getAutoSavedValue' | 'setValue' | 'getPublicUrl'>>;
/**
* A preconfigured logger for the request handler.
*/
log: Log;
}
export interface CrawlingContext<Crawler = unknown, UserData extends Dictionary = Dictionary>
extends RestrictedCrawlingContext<UserData> {
crawler: Crawler;
/**
* This function automatically finds and enqueues links from the current page, adding them to the {@apilink RequestQueue}
* currently used by the crawler.
*
* Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions
* and override settings of the enqueued {@apilink Request} objects.
*
* Check out the [Crawl a website with relative links](https://crawlee.dev/js/docs/examples/crawl-relative-links) example
* for more details regarding its usage.
*
* **Example usage**
*
* ```ts
* async requestHandler({ enqueueLinks }) {
* await enqueueLinks({
* globs: [
* 'https://www.example.com/handbags/*',
* ],
* });
* },
* ```
*
* @param [options] All `enqueueLinks()` parameters are passed via an options object.
* @returns Promise that resolves to {@apilink BatchAddRequestsResult} object.
*/
enqueueLinks(
options?: ReadonlyDeep<Omit<EnqueueLinksOptions, 'requestQueue'>> & Pick<EnqueueLinksOptions, 'requestQueue'>,
): Promise<BatchAddRequestsResult>;
/**
* Get a key-value store with given name or id, or the default one for the crawler.
*/
getKeyValueStore: (idOrName?: string) => Promise<KeyValueStore>;
/**
* Fires HTTP request via [`got-scraping`](https://crawlee.dev/js/docs/guides/got-scraping), allowing to override the request
* options on the fly.
*
* This is handy when you work with a browser crawler but want to execute some requests outside it (e.g. API requests).
* Check the [Skipping navigations for certain requests](https://crawlee.dev/js/docs/examples/skip-navigation) example for
* more detailed explanation of how to do that.
*
* ```ts
* async requestHandler({ sendRequest }) {
* const { body } = await sendRequest({
* // override headers only
* headers: { ... },
* });
* },
* ```
*/
sendRequest<Response = string>(overrideOptions?: Partial<OptionsInit>): Promise<GotResponse<Response>>;
}
/**
* A partial implementation of {@apilink RestrictedCrawlingContext} that stores parameters of calls to context methods for later inspection.
*
* @experimental
*/
export class RequestHandlerResult {
private _keyValueStoreChanges: Record<string, Record<string, { changedValue: unknown; options?: RecordOptions }>> =
{};
private pushDataCalls: Parameters<RestrictedCrawlingContext['pushData']>[] = [];
private addRequestsCalls: Parameters<RestrictedCrawlingContext['addRequests']>[] = [];
private enqueueLinksCalls: Parameters<RestrictedCrawlingContext['enqueueLinks']>[] = [];
constructor(
private config: Configuration,
private crawleeStateKey: string,
) {}
/**
* A record of calls to {@apilink RestrictedCrawlingContext.pushData}, {@apilink RestrictedCrawlingContext.addRequests}, {@apilink RestrictedCrawlingContext.enqueueLinks} made by a request handler.
*/
get calls(): ReadonlyDeep<{
pushData: Parameters<RestrictedCrawlingContext['pushData']>[];
addRequests: Parameters<RestrictedCrawlingContext['addRequests']>[];
enqueueLinks: Parameters<RestrictedCrawlingContext['enqueueLinks']>[];
}> {
return {
pushData: this.pushDataCalls,
addRequests: this.addRequestsCalls,
enqueueLinks: this.enqueueLinksCalls,
};
}
/**
* A record of changes made to key-value stores by a request handler.
*/
get keyValueStoreChanges(): ReadonlyDeep<
Record<string, Record<string, { changedValue: unknown; options?: RecordOptions }>>
> {
return this._keyValueStoreChanges;
}
/**
* Items added to datasets by a request handler.
*/
get datasetItems(): ReadonlyDeep<{ item: Dictionary; datasetIdOrName?: string }[]> {
return this.pushDataCalls.flatMap(([data, datasetIdOrName]) =>
(Array.isArray(data) ? data : [data]).map((item) => ({ item, datasetIdOrName })),
);
}
/**
* URLs enqueued to the request queue by a request handler, either via {@apilink RestrictedCrawlingContext.addRequests} or {@apilink RestrictedCrawlingContext.enqueueLinks}
*/
get enqueuedUrls(): ReadonlyDeep<{ url: string; label?: string }[]> {
const result: { url: string; label?: string }[] = [];
for (const [options] of this.enqueueLinksCalls) {
result.push(...(options?.urls?.map((url) => ({ url, label: options?.label })) ?? []));
}
for (const [requests] of this.addRequestsCalls) {
for (const request of requests) {
if (
typeof request === 'object' &&
(!('requestsFromUrl' in request) || request.requestsFromUrl !== undefined) &&
request.url !== undefined
) {
result.push({ url: request.url, label: request.label });
} else if (typeof request === 'string') {
result.push({ url: request });
}
}
}
return result;
}
/**
* URL lists enqueued to the request queue by a request handler via {@apilink RestrictedCrawlingContext.addRequests} using the `requestsFromUrl` option.
*/
get enqueuedUrlLists(): ReadonlyDeep<{ listUrl: string; label?: string }[]> {
const result: { listUrl: string; label?: string }[] = [];
for (const [requests] of this.addRequestsCalls) {
for (const request of requests) {
if (
typeof request === 'object' &&
'requestsFromUrl' in request &&
request.requestsFromUrl !== undefined
) {
result.push({ listUrl: request.requestsFromUrl, label: request.label });
}
}
}
return result;
}
pushData: RestrictedCrawlingContext['pushData'] = async (data, datasetIdOrName) => {
this.pushDataCalls.push([data, datasetIdOrName]);
};
enqueueLinks: RestrictedCrawlingContext['enqueueLinks'] = async (options) => {
this.enqueueLinksCalls.push([options]);
};
addRequests: RestrictedCrawlingContext['addRequests'] = async (requests, options = {}) => {
this.addRequestsCalls.push([requests, options]);
};
useState: RestrictedCrawlingContext['useState'] = async (defaultValue) => {
const store = await this.getKeyValueStore(undefined);
return await store.getAutoSavedValue(this.crawleeStateKey, defaultValue);
};
getKeyValueStore: RestrictedCrawlingContext['getKeyValueStore'] = async (idOrName) => {
const store = await KeyValueStore.open(idOrName, { config: this.config });
return {
id: this.idOrDefault(idOrName),
name: idOrName,
getValue: async (key) => this.getKeyValueStoreChangedValue(idOrName, key) ?? (await store.getValue(key)),
getAutoSavedValue: async <T extends Dictionary = Dictionary>(key: string, defaultValue: T = {} as T) => {
let value = this.getKeyValueStoreChangedValue(idOrName, key);
if (value === null) {
value = (await store.getValue(key)) ?? defaultValue;
this.setKeyValueStoreChangedValue(idOrName, key, value);
}
return value as T;
},
setValue: async (key, value, options) => {
this.setKeyValueStoreChangedValue(idOrName, key, value, options);
},
getPublicUrl: store.getPublicUrl.bind(store),
};
};
private idOrDefault = (idOrName?: string): string => idOrName ?? this.config.get('defaultKeyValueStoreId');
private getKeyValueStoreChangedValue = (idOrName: string | undefined, key: string) => {
const id = this.idOrDefault(idOrName);
this._keyValueStoreChanges[id] ??= {};
return this.keyValueStoreChanges[id][key]?.changedValue ?? null;
};
private setKeyValueStoreChangedValue = (
idOrName: string | undefined,
key: string,
changedValue: unknown,
options?: RecordOptions,
) => {
const id = this.idOrDefault(idOrName);
this._keyValueStoreChanges[id] ??= {};
this._keyValueStoreChanges[id][key] = { changedValue, options };
};
}