followers!

1const username = "USER_NAME_HERE";
2
3/**
4 * Initialized like this so we can still run it from browsers,
5 * but also use typescript on a code editor for intellisense.
6 */
7let followers = [{ username: "", full_name: "" }];
8let followings = [{ username: "", full_name: "" }];
9let dontFollowMeBack = [{ username: "", full_name: "" }];
10let iDontFollowBack = [{ username: "", full_name: "" }];
11
12followers = [];
13followings = [];
14dontFollowMeBack = [];
15iDontFollowBack = [];
16
17(async () => {
18  try {
19    console.log(`Process started! Give it a couple of seconds`);
20
21    const userQueryRes = await fetch(
22      `https://www.instagram.com/web/search/topsearch/?query=${username}`
23    );
24
25    const userQueryJson = await userQueryRes.json();
26
27    const userId = userQueryJson.users
28      .map((u) => u.user)
29      .filter((u) => u.username === username)[0].pk;
30
31    let after = null;
32    let has_next = true;
33
34    while (has_next) {
35      const query_hash = "c76146de99bb02f6415203be841dd25a";
36      await fetch(
37        `https://www.instagram.com/graphql/query/?query_hash=${query_hash}&variables=` +
38          encodeURIComponent(
39            JSON.stringify({
40              id: userId,
41              include_reel: true,
42              fetch_mutual: true,
43              first: 50,
44              after: after,
45            })
46          )
47      )
48        .then((res) => res.json())
49        .then((res) => {
50          has_next = res.data.user.edge_followed_by.page_info.has_next_page;
51          after = res.data.user.edge_followed_by.page_info.end_cursor;
52          followers = followers.concat(
53            res.data.user.edge_followed_by.edges.map(({ node }) => {
54              return {
55                username: node.username,
56                full_name: node.full_name,
57              };
58            })
59          );
60        });
61    }
62
63    after = null;
64    has_next = true;
65
66    while (has_next) {
67      const query_hash = "d04b0a864b4b54837c0d870b0e77e076";
68      await fetch(
69        `https://www.instagram.com/graphql/query/?query_hash=${query_hash}&variables=` +
70          encodeURIComponent(
71            JSON.stringify({
72              id: userId,
73              include_reel: true,
74              fetch_mutual: true,
75              first: 50,
76              after: after,
77            })
78          )
79      )
80        .then((res) => res.json())
81        .then((res) => {
82          has_next = res.data.user.edge_follow.page_info.has_next_page;
83          after = res.data.user.edge_follow.page_info.end_cursor;
84          followings = followings.concat(
85            res.data.user.edge_follow.edges.map(({ node }) => {
86              return {
87                username: node.username,
88                full_name: node.full_name,
89              };
90            })
91          );
92        });
93    }
94
95    console.log('--------------------------------');
96    console.log("These are the people who don't follow you back:");
97    dontFollowMeBack = followings.filter((following) => {
98      return !followers.find(
99        (follower) => follower.username === following.username
100      );
101    });
102    console.table(dontFollowMeBack);
103
104    console.log('--------------------------------');
105    console.log("These are the people you don't follow back:");
106    iDontFollowBack = followers.filter((follower) => {
107      return !followings.find(
108        (following) => following.username === follower.username
109      );
110    });
111    console.table(iDontFollowBack);
112    console.log('--------------------------------');
113  } catch (err) {
114    console.log({ err });
115  }
116})();
117