cd /news/developer-tools/this-is-our-community-this-is-our-fa… · home topics developer-tools article
[ARTICLE · art-10998] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

"This is our community, this is our family, these are our friends." https://www.youtube.com/watch?v=gk7iWgCk14U&t=425s

Based on the provided code, the article is a technical guide for a JavaScript script that uses Instagram's internal API to analyze a user's followers and following lists. The script identifies accounts that the user does not follow back and accounts that do not follow the user back. It includes rate-limiting delays and requires the user to input their Instagram username into the code before running it in the browser console.

read2 min views21 publishedDec 27, 2022
instagram-follower-following.js

      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

Show hidden characters

if (window.location.origin !== "https://www.instagram.com") { window.alert(

"Hey! You need to be on the instagram site before you run the code. I'm taking you there now but you're going to have to run the code into the console again.",
  );

  window.location.href = "https://www.instagram.com";

  console.clear();

}

const fetchOptions = {

  credentials: "include",

  headers: {

    "X-IG-App-ID": "936619743392459",

  },

  method: "GET",

};

let username;

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

const random = (min, max) => Math.floor(Math.random() * (max - min)) + min;

// This function handles all of the pagination logic

// Calls the API recursively until there are no more pages to load

const concatFriendshipsApiResponse = async (

  list,

  user_id,

  count,

next_max_id = "",

) => {

  let url = `https://www.instagram.com/api/v1/friendships/${user_id}/${list}/?count=${count}`;

  if (next_max_id) {

    url += `&max_id=${next_max_id}`;

  }

  const data = await fetch(url, fetchOptions).then((r) => r.json());

  if (data.next_max_id) {

    const timeToSleep = random(800, 1500);

    console.log(

      `Loaded ${data.users.length} ${list}. Sleeping ${timeToSleep}ms to avoid rate limiting`,

    );

    await sleep(timeToSleep);

    return data.users.concat(

      await concatFriendshipsApiResponse(

        list,

        user_id,

        count,

        data.next_max_id,

      ),

    );

  }

  return data.users;

};

// helper methods to make the code a bit more readable

const getFollowers = (user_id, count = 50, next_max_id = "") => {

  return concatFriendshipsApiResponse("followers", user_id, count, next_max_id);

};

const getFollowing = (user_id, count = 50, next_max_id = "") => {

  return concatFriendshipsApiResponse("following", user_id, count, next_max_id);

};

const getUserId = async (username) => {

  let user = username;

  const lower = user.toLowerCase();

  const url = `https://www.instagram.com/api/v1/web/search/topsearch/?context=blended&query=${lower}&include_reel=false`;

  const data = await fetch(url, fetchOptions).then((r) => r.json());

  const result = data.users?.find(

    (result) => result.user.username.toLowerCase() === lower,

  );

  return result?.user?.pk || null;

};

const getUserFriendshipStats = async (username) => {

  const user_id = await getUserId(username);

  if (!user_id) {

    throw new Error(`Could not find user with username ${username}`);

  }

  const followers = await getFollowers(user_id);

  const following = await getFollowing(user_id);

  const followersUsernames = followers.map((follower) =>

    follower.username.toLowerCase(),

  );

  const followingUsernames = following.map((followed) =>

    followed.username.toLowerCase(),

  );

  const followerSet = new Set(followersUsernames);

  const followingSet = new Set(followingUsernames);

  console.log(Array(28).fill("-").join(""));

console.log(

`Fetched`,
    followerSet.size,

    "followers and ",

    followingSet.size,

    " following.",

  );

console.log(

`If this doesn't seem right then some of the output might be inaccurate`,
  );

  const PeopleIDontFollowBack = Array.from(followerSet).filter(

    (follower) => !followingSet.has(follower),

  );

  const PeopleNotFollowingMeBack = Array.from(followingSet).filter(

    (following) => !followerSet.has(following),

  );

  return {

    PeopleIDontFollowBack,

    PeopleNotFollowingMeBack,

  };

};

// Make sure you don't delete the quotes

// Replace "example_username" below with your instagram username

//

// Change this:

username = "example_username";

//

//

//

getUserFriendshipStats(username).then(console.log);
── more in #developer-tools 4 stories · sorted by recency
── more on @instagram 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/this-is-our-communit…] indexed:0 read:2min 2022-12-27 ·