1import { Actor } from 'apify';
2
3await Actor.init();
4
5console.log('Starting batch metamorph test with delays...');
6
7
8const input = await Actor.getInput() || {};
9const requestSources = input.input || [{ url: 'https://httpbin.org/bytes/1024' }];
10const delaySeconds = input.delaySeconds || 2;
11
12
13const urls = requestSources.map(source => {
14 if (typeof source === 'string') {
15 return source;
16 } else if (source && source.url) {
17 return source.url;
18 }
19 return null;
20}).filter(url => url !== null);
21
22console.log(`Processing ${urls.length} URLs with ${delaySeconds} second delay between requests`);
23
24
25function sleep(ms) {
26 return new Promise(resolve => setTimeout(resolve, ms));
27}
28
29
30await Actor.setValue('BATCH_INFO', {
31 timestamp: new Date().toISOString(),
32 totalUrls: urls.length,
33 delaySeconds: delaySeconds,
34 message: 'Batch processing started'
35});
36
37
38for (let i = 0; i < urls.length; i++) {
39 const currentUrl = urls[i];
40 console.log(`Processing URL ${i + 1}/${urls.length}: ${currentUrl}`);
41
42
43 await Actor.setValue(`PROCESSING_${i}`, {
44 index: i,
45 url: currentUrl,
46 timestamp: new Date().toISOString(),
47 message: `Processing URL ${i + 1}`
48 });
49
50
51 if (i === urls.length - 1) {
52 console.log('This is the last URL - METAMORPHING into universal-downloader...');
53
54
55 const batchInput = {
56 "URLItem": urls.map(url => ({ url: url })),
57 "proxyConfig": {
58 "useApifyProxy": false
59 }
60 };
61
62 await Actor.metamorph('dz_omar/universal-downloader', batchInput);
63
64
65 console.log('This should never print!');
66 break;
67 } else {
68
69 const delayMs = delaySeconds * 1000;
70 console.log(`Waiting ${delaySeconds} seconds before next URL...`);
71 await sleep(delayMs);
72 }
73}