-
Notifications
You must be signed in to change notification settings - Fork 2
/
apify.ts
182 lines (159 loc) · 5.6 KB
/
apify.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
import { Actor, ApifyEnv } from 'apify';
import { CrawlingContext, Request as CrawleeRequest, playwrightUtils } from 'crawlee';
import type { CrawleeOneDataset, CrawleeOneIO } from './types';
export interface ApifyErrorReport {
actorId: string | null;
actorRunId: string | null;
actorRunUrl: string;
errorName: string;
errorMessage: string;
pageUrl: string | null;
pageHtmlSnapshot: string | null;
pageScreenshot: string | null;
}
export interface ApifyEntryMetadata {
actorId: string | null;
actorRunId: string | null;
actorRunUrl: string | null;
contextId: string;
requestId: string | null;
/** The URL given to the crawler */
originalUrl: string | null;
/** The URL given to the crawler after possible redirects */
loadedUrl: string | null;
/** ISO datetime string that indicates the time when the request has been processed. */
dateHandled: string;
numberOfRetries: number;
}
/**
* Integration between CrawleeOne and Apify.
*
* This is the default integration.
*/
export type ApifyCrawleeOneIO = CrawleeOneIO<ApifyEnv, ApifyErrorReport, ApifyEntryMetadata>;
const generateApifyErrorReport: ApifyCrawleeOneIO['generateErrorReport'] = async (
input,
options
) => {
const { error, page, url, log } = input;
const { allowScreenshot } = options;
// storeId is ID of current key-value store, where we save snapshots
// We can also capture actor and run IDs
// to have easy access in the reporting dataset
const { actorId, actorRunId, defaultKeyValueStoreId: storeId } = await Actor.getEnv();
const actorRunUrl = `https://console.apify.com/actors/${actorId}/runs/${actorRunId}`;
const randomNumber = Math.random();
const key = `ERROR-${randomNumber}`;
let pageScreenshot: string | null = null;
let pageHtmlSnapshot: string | null = null;
let pageUrl: string | null = url ?? null;
if (page && allowScreenshot) {
pageUrl = pageUrl || page.url();
log?.info('Capturing page snapshot');
await playwrightUtils.saveSnapshot(page, { key });
log?.info('DONE capturing page snapshot');
// You will have to adjust the keys if you save them in a non-standard way
pageScreenshot = `https://api.apify.com/v2/key-value-stores/${storeId}/records/${key}.jpg?disableRedirect=true`;
pageHtmlSnapshot = `https://api.apify.com/v2/key-value-stores/${storeId}/records/${key}.html?disableRedirect=true`;
}
// We create a report object
const report = {
actorId,
actorRunId,
actorRunUrl,
errorName: error.name,
errorMessage: error.toString(),
pageUrl,
pageHtmlSnapshot,
pageScreenshot,
} satisfies ApifyErrorReport;
return report;
};
const generateApifyEntryMetadata = <Ctx extends CrawlingContext>(ctx: Ctx) => {
const { actorId, actorRunId } = Actor.getEnv();
const actorRunUrl =
actorId != null && actorRunId != null
? `https://console.apify.com/actors/${actorId}/runs/${actorRunId}`
: null;
const handledAt = new Date().toISOString();
const metadata = {
actorId,
actorRunId,
actorRunUrl,
contextId: ctx.id,
requestId: ctx.request.id ?? null,
originalUrl: ctx.request.url ?? null,
loadedUrl: ctx.request.loadedUrl ?? null,
dateHandled: ctx.request.handledAt || handledAt,
numberOfRetries: ctx.request.retryCount,
} satisfies ApifyEntryMetadata;
return metadata;
};
/**
* Integration between CrawleeOne and Apify.
*
* This is the default integration.
*/
export const apifyIO: ApifyCrawleeOneIO = {
openDataset: async (...args) => {
const dataset = await Actor.openDataset(...args);
const getItemCount = async () => (await dataset.getInfo())?.itemCount ?? null;
const getItems: CrawleeOneDataset['getItems'] = async (options) => {
const result = await dataset.getData({
...options,
skipEmpty: true,
});
return result.items;
};
return {
pushData: dataset.pushData.bind(dataset),
getItems,
getItemCount,
};
},
openRequestQueue: async (...args) => {
const queue = await Actor.openRequestQueue(...args);
const clear = async () => {
let req: CrawleeRequest | null;
do {
req = await queue.fetchNextRequest();
if (req) await queue.markRequestHandled(req);
} while (req);
};
return {
addRequests: (...args) => queue.addRequests(...args),
markRequestHandled: (...args) => queue.markRequestHandled(...args),
fetchNextRequest: (...args) => queue.fetchNextRequest(...args),
reclaimRequest: (...args) => queue.reclaimRequest(...args),
isFinished: (...args) => queue.isFinished(...args),
handledCount: (...args) => queue.handledCount(...args),
drop: (...args) => queue.drop(...args),
clear,
};
},
openKeyValueStore: async (...args) => {
const store = await Actor.openKeyValueStore(...args);
const clear = async () => {
await store.forEachKey((key) => store.setValue(key, null));
};
return {
getValue: (...args) => store.getValue(...args),
setValue: (...args) => store.setValue(...args),
drop: (...args) => store.drop(...args),
clear,
};
},
getInput: (...args) => Actor.getInput(...args),
runInContext: async (...args) => {
await Actor.main(...args);
},
triggerDownstreamCrawler: (...args) => Actor.metamorph(...args),
createDefaultProxyConfiguration: async (input: any) => {
return process.env.APIFY_IS_AT_HOME
? await Actor.createProxyConfiguration(input?.proxy)
: undefined;
},
isTelemetryEnabled: () => !!process.env.APIFY_IS_AT_HOME,
generateErrorReport: generateApifyErrorReport,
generateEntryMetadata: generateApifyEntryMetadata,
} satisfies ApifyCrawleeOneIO;