/ clis / instagram / follow.js
follow.js
 1  import { cli } from '@jackwener/opencli/registry';
 2  cli({
 3      site: 'instagram',
 4      name: 'follow',
 5      description: 'Follow an Instagram user',
 6      domain: 'www.instagram.com',
 7      args: [
 8          {
 9              name: 'username',
10              required: true,
11              positional: true,
12              help: 'Instagram username to follow',
13          },
14      ],
15      columns: ['status', 'username'],
16      pipeline: [
17          { navigate: 'https://www.instagram.com' },
18          { evaluate: `(async () => {
19    const username = \${{ args.username | json }};
20    const headers = { 'X-IG-App-ID': '936619743392459' };
21    const opts = { credentials: 'include', headers };
22  
23    // Get user ID
24    const r1 = await fetch('https://www.instagram.com/api/v1/users/web_profile_info/?username=' + encodeURIComponent(username), opts);
25    if (!r1.ok) throw new Error('User not found: ' + username);
26    const d1 = await r1.json();
27    const userId = d1?.data?.user?.id;
28    if (!userId) throw new Error('User not found: ' + username);
29  
30    const csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || '';
31    const r2 = await fetch('https://www.instagram.com/api/v1/friendships/create/' + userId + '/', {
32      method: 'POST',
33      credentials: 'include',
34      headers: { ...headers, 'X-CSRFToken': csrf, 'Content-Type': 'application/x-www-form-urlencoded' },
35    });
36    if (!r2.ok) throw new Error('Failed to follow: HTTP ' + r2.status);
37    const d2 = await r2.json();
38    const status = d2?.friendship_status?.following ? 'Following' : d2?.friendship_status?.outgoing_request ? 'Request sent' : 'Followed';
39    return [{ status, username }];
40  })()
41  ` },
42      ],
43  });