Last active
April 27, 2025 21:10
-
-
Save ilyahuman/c44a939af8afcd700bea1ce684563218 to your computer and use it in GitHub Desktop.
Instagram check own followers/unfollowers
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
| // --- Constants --- | |
| const INSTAGRAM_URL = "https://www.instagram.com"; | |
| const IG_APP_ID = "936619743392459"; | |
| const fetchOptions = { | |
| credentials: "include", | |
| headers: { "X-IG-App-ID": IG_APP_ID }, | |
| method: "GET", | |
| }; | |
| // --- Utilities --- | |
| const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | |
| const random = (min, max) => Math.floor(Math.random() * (max - min)) + min; | |
| // --- Environment Check --- | |
| if (window.location.origin !== INSTAGRAM_URL) { | |
| alert("Redirecting to Instagram. Please re-run the script after landing."); | |
| window.location.href = INSTAGRAM_URL; | |
| console.clear(); | |
| } | |
| // --- Core API Functions --- | |
| const fetchFriendships = async (type, userId, count = 50, maxId = "") => { | |
| const params = new URLSearchParams({ count, ...(maxId && { max_id: maxId }) }); | |
| const url = `${INSTAGRAM_URL}/api/v1/friendships/${userId}/${type}/?${params}`; | |
| const { users = [], next_max_id } = await (await fetch(url, fetchOptions)).json(); | |
| if (next_max_id) { | |
| const delay = random(800, 1500); | |
| console.log(`Loaded ${users.length} ${type}. Sleeping ${delay}ms...`); | |
| await sleep(delay); | |
| const moreUsers = await fetchFriendships(type, userId, count, next_max_id); | |
| return [...users, ...moreUsers]; | |
| } | |
| return users; | |
| }; | |
| const getFollowers = (userId, count = 50) => fetchFriendships("followers", userId, count); | |
| const getFollowing = (userId, count = 50) => fetchFriendships("following", userId, count); | |
| const fetchUserId = async (username) => { | |
| const url = `${INSTAGRAM_URL}/api/v1/web/search/topsearch/?context=blended&query=${encodeURIComponent(username)}&include_reel=false`; | |
| const { users = [] } = await (await fetch(url, fetchOptions)).json(); | |
| return users.find(({ user }) => user.username.toLowerCase() === username.toLowerCase())?.user?.pk ?? null; | |
| }; | |
| // --- Main Business Logic --- | |
| const getUserFriendshipStats = async (username) => { | |
| const userId = await fetchUserId(username); | |
| if (!userId) throw new Error(`User "${username}" not found.`); | |
| const [followers, following] = await Promise.all([ | |
| getFollowers(userId), | |
| getFollowing(userId), | |
| ]); | |
| const followersSet = new Set(followers.map(({ username }) => username.toLowerCase())); | |
| const followingSet = new Set(following.map(({ username }) => username.toLowerCase())); | |
| console.log("-".repeat(40)); | |
| console.log(`Fetched ${followersSet.size} followers and ${followingSet.size} following.`); | |
| console.log("Note: Output might be partial if Instagram limits the response."); | |
| const PeopleIDontFollowBack = [...followersSet].filter(user => !followingSet.has(user)); | |
| const PeopleNotFollowingMeBack = [...followingSet].filter(user => !followersSet.has(user)); | |
| return { PeopleIDontFollowBack, PeopleNotFollowingMeBack }; | |
| }; | |
| // --- Run (replace "example_username" with your Instagram username) --- | |
| const username = "example_username"; | |
| getUserFriendshipStats(username) | |
| .then((stats) => console.log(stats)) | |
| .catch((error) => console.error(error.message)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment