1const Apify = require('apify');
2
3
4
5
6Apify.main(async () => {
7 const input = (await Apify.getInput()) || {};
8 const rawUsernames = Array.isArray(input.usernames) ? input.usernames : [];
9
10
11 const handles = rawUsernames
12 .map((u) => String(u).trim())
13 .filter(Boolean)
14 .map((u) => u
15 .replace(/^@/, '')
16 .replace(/^https?:\/\/(www\.)?instagram\.com\//i, '')
17 .replace(/[/?#].*$/, '')
18 .trim())
19 .filter(Boolean);
20
21 if (handles.length === 0) {
22 throw new Error('No usernames provided. Add at least one Instagram username to the "usernames" field.');
23 }
24
25 console.log(`Resolving ${handles.length} profile(s) via apify/instagram-profile-scraper...`);
26
27
28 const run = await Apify.call('apify/instagram-profile-scraper', {
29 usernames: handles,
30 });
31
32
33 const client = Apify.newClient();
34 const { items: profiles } = await client.dataset(run.defaultDatasetId).listItems();
35
36
37 const urlRegex = /\b((?:https?:\/\/)?(?:www\.)?[a-z0-9-]+(?:\.[a-z0-9-]+)+(?:\/[^\s]*)?)/gi;
38
39 let pushed = 0;
40 for (const p of profiles) {
41 const bio = p.biography || '';
42 const inBio = (bio.match(urlRegex) || []).map((s) => s.trim());
43 const dedicated = [];
44 if (p.externalUrl) dedicated.push(p.externalUrl);
45 if (Array.isArray(p.bioLinks)) {
46 for (const b of p.bioLinks) {
47 if (b && b.url) dedicated.push(b.url);
48 }
49 }
50 const bioLinks = Array.from(new Set([...dedicated, ...inBio].filter(Boolean)));
51
52 await Apify.pushData({
53 username: p.username || null,
54 fullName: p.fullName || null,
55 biography: bio,
56 externalUrl: p.externalUrl || null,
57 bioLinks,
58 isVerified: typeof p.verified === 'boolean' ? p.verified : (p.isVerified || null),
59 followersCount: typeof p.followersCount === 'number' ? p.followersCount : null,
60 profileUrl: p.username ? `https://www.instagram.com/${p.username}/` : null,
61 scrapedAt: new Date().toISOString(),
62 });
63 pushed += 1;
64 }
65
66 console.log(`Done. Pushed ${pushed} profile(s) to the dataset.`);
67});