/ clis / instagram / like.js
like.js
 1  import { cli } from '@jackwener/opencli/registry';
 2  cli({
 3      site: 'instagram',
 4      name: 'like',
 5      description: 'Like an Instagram post',
 6      domain: 'www.instagram.com',
 7      args: [
 8          {
 9              name: 'username',
10              required: true,
11              positional: true,
12              help: 'Username of the post author',
13          },
14          { name: 'index', type: 'int', default: 1, help: 'Post index (1 = most recent)' },
15      ],
16      columns: ['status', 'user', 'post'],
17      pipeline: [
18          { navigate: 'https://www.instagram.com' },
19          { evaluate: `(async () => {
20    const username = \${{ args.username | json }};
21    const idx = \${{ args.index }} - 1;
22    const headers = { 'X-IG-App-ID': '936619743392459' };
23    const opts = { credentials: 'include', headers };
24  
25    const r1 = await fetch('https://www.instagram.com/api/v1/users/web_profile_info/?username=' + encodeURIComponent(username), opts);
26    if (!r1.ok) throw new Error('User not found: ' + username);
27    const userId = (await r1.json())?.data?.user?.id;
28  
29    const r2 = await fetch('https://www.instagram.com/api/v1/feed/user/' + userId + '/?count=' + (idx + 1), opts);
30    const posts = (await r2.json())?.items || [];
31    if (idx >= posts.length) throw new Error('Post index ' + (idx + 1) + ' not found, user has ' + posts.length + ' recent posts');
32    const pk = posts[idx].pk;
33    const caption = (posts[idx].caption?.text || '').substring(0, 60);
34  
35    const csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || '';
36    const r3 = await fetch('https://www.instagram.com/api/v1/web/likes/' + pk + '/like/', {
37      method: 'POST', credentials: 'include',
38      headers: { ...headers, 'X-CSRFToken': csrf, 'Content-Type': 'application/x-www-form-urlencoded' },
39    });
40    if (!r3.ok) throw new Error('Failed to like: HTTP ' + r3.status);
41    return [{ status: 'Liked', user: username, post: caption || '(post #' + (idx+1) + ')' }];
42  })()
43  ` },
44      ],
45  });