1import { Actor } from 'apify';
2import { ApifyClient } from 'apify-client';
3
4await Actor.init()
5
6const {
7 isDryRun,
8 sourceActorId,
9 sourceApiToken,
10 targetActorId,
11 targetApiToken,
12} = await Actor.getInput();
13
14if (isDryRun) console.log('This is a dry run!');
15
16console.log(sourceApiToken);
17
18const sourceClient = new ApifyClient({ token: sourceApiToken });
19const targetClient = new ApifyClient({ token: targetApiToken });
20
21const sourceActor = await sourceClient.actor(sourceActorId).get();
22const sourceUser = await sourceClient.user().get();
23const targetActor = await targetClient.actor(targetActorId).get();
24const targetUser = await targetClient.user().get();
25
26console.log(`Copying from ${sourceUser.username}/${sourceActor.name} to ${targetUser.username}/${targetActor.name}`);
27
28const tasksToBeCopied = [];
29
30let hasMore = true;
31let offset = 0;
32while (hasMore) {
33 const response = await sourceClient.tasks().list({ limit: 100, offset });
34
35 for (const task of response.items) {
36 if (task.actId === sourceActorId) tasksToBeCopied.push(task);
37 }
38
39 hasMore = response.items.length > 0;
40 offset += response.limit;
41}
42
43console.log('Tasks to be copied:')
44console.log(tasksToBeCopied.map(task => `- ${sourceUser.username}/${task.name}`).join('\n'));
45
46if (!isDryRun) {
47 console.log('Copying tasks...');
48 for (const { id: taskId } of tasksToBeCopied) {
49 const task = await sourceClient.task(taskId).get();
50 await targetClient.tasks().create({
51 actId: targetActorId,
52 name: task.name,
53 options: task.options,
54 input: task.input,
55 });
56 console.log(`${sourceUser.username}/${task.name} copied.`);
57 }
58}
59
60console.log('Done.');
61await Actor.exit();