Get to know who unfollowed you in Instagram

Mrinmoy Das
3 min readNov 13, 2023

How often do you find the number of followers in your Instagram account has reduced, and when you try to check who did, you have to haggle through a long list of followers and check them one by one.

Although a number of applications are available in the Play Store and App Store that does the same for you, however, with the new release of Instagram’s Graph API, running a script on the profile is prohibited, often ending the session token’s duration.

When I faced this problem, I decided to rely on a codebase of Selenium, that keeps running on a remote machine, scrolling and checking the followers and following count ratio, every day, in realtime, to inform me about the drop or raise in the count.

However, this also wasn’t that fruitful, as it often failed because I have 2FA enabled on my account.

Wanted to find a roundabout to this problem and I came across an awesome Python library called Instaloader. This library allowed you to interact with your profile and with predefined function invocations, one can get the list of the followers/following in their account.

My implementation reached a halt when Meta decided to reframe the entire data schema of the response using GraphQL. On searching a bit, with others facing the same issue, realised that the solution was to download Firefox and set it as the default browser. Login to the Instagram account and create a session file for the same. Use this session file to interact with your account till the token is valid. This doesn’t count as a active “logged-in” session, and doesn’t time out easily. Here are the steps for anyone who wants to try it out :

Step 1:
Make firefox as your default browser and log in Instagram with your username and password in firefox browser

Step 2:
Paste the following code in a py file :

from argparse import ArgumentParser
from glob import glob
from os.path import expanduser
from platform import system
from sqlite3 import OperationalError, connect

try:
from instaloader import ConnectionException, Instaloader
except ModuleNotFoundError:
raise SystemExit("Instaloader not found.\n pip install [--user] instaloader")


def get_cookiefile():
default_cookiefile = {
"Windows": "~/AppData/Roaming/Mozilla/Firefox/Profiles/*/cookies.sqlite",
"Darwin": "~/Library/Application Support/Firefox/Profiles/*/cookies.sqlite",
}.get(system(), "~/.mozilla/firefox/*/cookies.sqlite")
cookiefiles = glob(expanduser(default_cookiefile))
if not cookiefiles:
raise SystemExit("No Firefox cookies.sqlite file found. Use -c COOKIEFILE.")
return cookiefiles[0]


def import_session(cookiefile, sessionfile):
print("Using cookies from {}.".format(cookiefile))
conn = connect(f"file:{cookiefile}?immutable=1", uri=True)
try:
cookie_data = conn.execute(
"SELECT name, value FROM moz_cookies WHERE baseDomain='instagram.com'"
)
except OperationalError:
cookie_data = conn.execute(
"SELECT name, value FROM moz_cookies WHERE host LIKE '%instagram.com'"
)
instaloader = Instaloader(max_connection_attempts=1)
instaloader.context._session.cookies.update(cookie_data)
username = instaloader.test_login()
if not username:
raise SystemExit("Not logged in. Are you logged in successfully in Firefox?")
print("Imported session cookie for {}.".format(username))
instaloader.context.username = username
instaloader.save_session_to_file(sessionfile)


if __name__ == "__main__":
p = ArgumentParser()
p.add_argument("-c", "--cookiefile")
p.add_argument("-f", "--sessionfile")
args = p.parse_args()
try:
import_session(args.cookiefile or get_cookiefile(), args.sessionfile)
except (ConnectionException, OperationalError) as e:
raise SystemExit("Cookie import failed: {}".format(e))

Step 3:
Run this file

python <File name>.py

After the script is run, it will save the data inside this local path:
C:\Users\{USER}\AppData\Local\Instaloader\session-{USERNAME}

For Example: C:\Users\Acer\AppData\Local\Instaloader\session-test123
Now copy this full file path from above and use it in step 4:

Step 4:
import instaloader

loader = instaloader.Instaloader()
try:
loader.load_session_from_file("XXXXX", r"/Users/aemdeei/.config/instaloader/session-XXXXX")
#1st argument should be username and 2nd argument should be the saved file location
print("Logged In Successfully")
except Exception as e:
print("Error", e)

Once done, you can use loader’s context to interact with your profile.

Modified the above code to give me the result that I want. Here is a sample :

import instaloader

loader = instaloader.Instaloader()
try:
loader.load_session_from_file("XXXXX", r"/Users/XXXXX/.config/instaloader/session-XXXXX")
print("Logged In Successfully")
profile = instaloader.Profile.from_username(loader.context, "XXXXX")
followers = profile.get_followers()
followers_arr = []
followees_arr = []
for each_followers in followers:
print(each_followers.username)
followers_arr.append(each_followers.username)
print("--- End of followers ---")
followees = profile.get_followees()
for each_followees in followees:
print(each_followees.username)
followees_arr.append(each_followees.username)
print("--- End of followees ---")
unfollower = set(followees_arr).difference(followers_arr)
for each_unfollower in unfollower:
print("~~", each_unfollower)
except Exception as e:
print("Error", e)

The above provided me the list of people who doesn’t follow me back, although, I follow them. One can use the same invocation to unfollow them automatically.

I didn’t do it because I do not expect Cristiano Ronaldo to follow me (till now) :)

--

--