-
Star
(538)
You must be signed in to star a gist -
Fork
(96)
You must be signed in to fork a gist
-
-
Save abir-taheer/0d3f1313def5eec6b78399c0fb69e4b1 to your computer and use it in GitHub Desktop.
| 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); |
why dont you parallelize requests?
const [followers, following] = await Promise.all([ getFollowers(user_id), getFollowing(user_id) ]);
instead of
const followers = await getFollowers(user_id); const following = await getFollowing(user_id);
2/26/2025 works fine
i'd suggest you change the cooldown to something like 2500-3000ms, i think it may be too fast, i got an email that my account might be botted
it doesn't work for some reason
Hey the code isn't working I tried everything. Did instagram block the code from being used?
Hi - is there any code to see who stalks your profile?
Haven't been able to get this to work as of Oct 6 - anyone figure anything out?
I've been using this for a year and it works!! I see in previous comments, it doesn't work for some so I'll show you how I do it (I don't understand coding or tech at well so the fact that I can use it is amazing)
- Open instagram on a web browser (google)
- Once you're logged in, right click and a tab will show up. At the bottom of the tab, it will show an ''inspect'' option.
- Click on ''inspect'' and a window will open with different headings (elements, console, sources, network etc).
- Click on the ''console'' heading.
- Sometimes there'll be words or whatever in the heading box, so all you have to do is right click and select clear console.
- Then copy and paste the code in the console box.
- Scroll down all the way until you see the ''username = ''example_username'' part of the code.
- Replace the ''example_username'' with your instagram username but don't delete the quotation marks.
- Press enter
- Give it some time to load and it'll show you who doesn't follow you back.
Don't do this every day otherwise your account can possibly be flagged, so what I do is check every 2 weeks or so.
make one for letterboxd pleasee!!!!

It used to work last week but now it's stuck at this only
