-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactor-example.js
More file actions
288 lines (250 loc) · 8.33 KB
/
actor-example.js
File metadata and controls
288 lines (250 loc) · 8.33 KB
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
/**
* Complete Actor Usage Example
* Demonstrates how to use various features of the Actor class
*/
import { Actor } from '@scrapeless-ai/sdk';
// Mock environment variables - in a real Actor runtime, these would be set by the platform
process.env.SCRAPELESS_ACTOR_ID = 'act_12345';
process.env.SCRAPELESS_RUN_ID = 'run_67890';
process.env.SCRAPELESS_USER_ID = 'user_12345';
process.env.SCRAPELESS_TEAM_ID = 'team_12345';
process.env.SCRAPELESS_API_KEY = 'your_api_key_here';
process.env.SCRAPELESS_DATASET_ID = 'dat_12345';
process.env.SCRAPELESS_KV_NAMESPACE_ID = 'ns_12345';
process.env.SCRAPELESS_BUCKET_ID = 'buck_12345';
process.env.SCRAPELESS_QUEUE_ID = 'q_12345';
process.env.SCRAPELESS_INPUT = JSON.stringify({
url: 'https://example.com',
maxResults: 10,
keywords: ['apple', 'banana', 'orange'],
useProxy: true,
captcha: {
enabled: true,
type: 'recaptcha'
},
options: {
headless: false,
screenshot: true,
waitTime: 5000
}
});
/**
* Run the complete Actor example
*/
async function runActorExample() {
console.log('Starting Actor example execution');
try {
// 1. Initialize Actor
const actor = new Actor();
console.log('Actor initialized');
// 2. Get input data
const input = await actor.input();
console.log('Actor input data:', input);
// 3. Dataset operations
console.log('\n--- Dataset Operations ---');
try {
// Add items to dataset
const items = input.keywords.map(keyword => ({
keyword,
url: input.url,
timestamp: new Date().toISOString()
}));
// Use Actor convenience method to add data
const addResult = await actor.addItems(items);
console.log('Added data to dataset:', addResult);
// Get data from dataset
const getResult = await actor.getItems({ page: 1, pageSize: 10 });
console.log('Retrieved data from dataset:', getResult);
// You can also use the underlying API directly
console.log('Using underlying API to operate on datasets');
await actor.storage.dataset.createDataset('New Dataset');
// In a real scenario, you might want to store the returned dataset ID for later use
} catch (error) {
console.error('Dataset operations error:', error.message);
}
// 4. KV storage operations
console.log('\n--- KV Storage Operations ---');
try {
// Store data
await actor.setValue({
key: 'last_run',
value: new Date().toISOString()
});
console.log('Set KV value successfully');
// Retrieve data
const lastRun = await actor.getValue('last_run');
console.log('Retrieved KV value:', lastRun);
// Store structured data
await actor.setValue({
key: 'config',
value: JSON.stringify(input.options)
});
// Retrieve and parse structured data
const configStr = await actor.getValue('config');
const config = JSON.parse(configStr);
console.log('Parsed configuration:', config);
} catch (error) {
console.error('KV storage operations error:', error.message);
}
// 5. Object storage operations
console.log('\n--- Object Storage Operations ---');
try {
// In a real environment, you can upload files here
// Due to example environment limitations, only showing the code
/*
// Upload file to object storage
const uploadResult = await actor.putObject({
file: './screenshot.png'
});
console.log('File upload result:', uploadResult);
// Download file
const fileBlob = await actor.getObject(uploadResult.object_id);
*/
console.log('Object storage operations example (for use in real environment)');
} catch (error) {
console.error('Object storage operations error:', error.message);
}
// 6. Queue operations
console.log('\n--- Queue Operations ---');
try {
// Push message to queue
const pushResult = await actor.pushMessage({
body: {
task: 'process_url',
params: {
url: input.url,
keywords: input.keywords
}
}
});
console.log('Pushed message to queue:', pushResult);
// Pull message from queue
const pullResult = await actor.pullMessage();
console.log('Pulled message from queue:', pullResult);
// Acknowledge message completion
if (pullResult && pullResult.length > 0) {
const ackResult = await actor.ackMessage(pullResult[0].id);
console.log('Acknowledged message completion:', ackResult);
}
} catch (error) {
console.error('Queue operations error:', error.message);
}
// 7. Browser automation
console.log('\n--- Browser Automation ---');
try {
// Create browser session
console.log('In a real environment, this would create a browser session for automation');
/*
// Create browser session
const sessionOptions = {
sessionTTL: 180,
proxyCountry: input.useProxy ? 'US' : null,
sessionRecording: true,
...input.options
};
const browserSession = await actor.browser.createSession(sessionOptions);
console.log('Browser session created successfully:', browserSession);
// Navigate to webpage
const navigateResult = await actor.browser.navigate({
session_id: browserSession.id,
url: input.url,
wait_until: 'networkidle2'
});
// Take screenshot
if (input.options.screenshot) {
const screenshotResult = await actor.browser.screenshot({
session_id: browserSession.id
});
// Save screenshot to object storage
await actor.putObject({
file: screenshotResult.data
});
}
// Extract data
const extractResult = await actor.browser.evaluate({
session_id: browserSession.id,
script: `
return {
title: document.title,
links: Array.from(document.querySelectorAll('a')).map(a => ({
text: a.innerText,
href: a.href
})).slice(0, ${input.maxResults})
}
`
});
// Save extracted data to dataset
await actor.addItems([{
url: input.url,
data: extractResult.result,
timestamp: new Date().toISOString()
}]);
// Close session
await actor.browser.closeSession({
session_id: browserSession.id
});
*/
} catch (error) {
console.error('Browser automation error:', error.message);
}
// 8. CAPTCHA solving
console.log('\n--- CAPTCHA Solving ---');
try {
if (input.captcha && input.captcha.enabled) {
console.log('In a real environment, this would handle CAPTCHAs');
/*
// Solve reCAPTCHA
const captchaResult = await actor.captcha.solveRecaptcha({
websiteURL: input.url,
websiteKey: 'site-key-here',
invisible: false
});
console.log('CAPTCHA solving result:', captchaResult);
*/
}
} catch (error) {
console.error('CAPTCHA solving error:', error.message);
}
// 9. Proxy usage
console.log('\n--- Proxy Usage ---');
try {
if (input.useProxy) {
console.log('In a real environment, this would obtain and use proxies');
/*
// Get proxy
const proxy = await actor.proxy.getSession({
country: 'US',
sessionTTL: 180
});
console.log('Proxy session:', proxy);
*/
}
} catch (error) {
console.error('Proxy usage error:', error.message);
}
// 10. Actor execution
console.log('\n--- Actor Execution ---');
try {
console.log('In a real environment, this would run other Actors');
/*
// Run another Actor
const runOptions = {
actorId: 'another-actor-id',
input: {
url: input.url,
processData: true
}
};
const runResult = await actor.runner.run(runOptions);
console.log('Actor run result:', runResult);
*/
} catch (error) {
console.error('Actor execution error:', error.message);
}
console.log('\nActor example execution completed');
} catch (error) {
console.error('Actor example execution error:', error);
}
}
// Run the example
runActorExample().catch(console.error);