Who un-followed you on Instagram? check using python

This article will help you to figure out the names of your Instagram un-followers using python

How can I see my un-followers on Instagram? If you are so much crazy about Instagram, this question will surely bother you. But the fact is that Instagram doesn't send any notification when someone unfollows you or someone watched your profile. Even though there is a way you can find your un-followers which is to check it manually. This is possible when you have a limited number of followers, But what if you reach thousands of followers and still you need to figure out your un-followers? Here rather than doing it manually we need to use a programmatic way, which means, we need to do something that automates the process of finding who un-follows you at different times. Of course, python can surely help you to do this. So let's see how.

Steps to perform:

  1. Install and import modules.
  2. Get the list of all your followers on Instagram.
  3. Store the follower's username in a text file using python.
  4. Get the current followers again.
  5.  Compare it with the text file containing your previous time followers in constant intervals

Installing and Importing modules:

First, we need to install a module named 'instaloader' which is an API integration with Instagram using python.

Simply install using pip

$ pip install instaloader

Logging to Instagram account:

Note: Creating a test Instagram account is highly recommended since Instagram may detect that you are running automation scripts which may cause problems for your Instagram account.


There are two ways in which you can log in to your Instagram account using 'instaloader'. One is to log through the command shell and the other is to log through the code itself. In both cases, a session file is stored in your system so that you don't need to log in again.

Log in through the console:

>>> instaloader --login <instagram_username>

Login through code:

import instaloader

# Loading instaloader object
= instaloader.Instaloader()

# Username
username = "test_insta_username"
password = "test_insta_password"

try:
    # Loading session stored in the system
    L.load_session_from_file(username)
    
except:
    # Login with username and password
    L.login(username, password)

Here we're loading the instaloader object and then trying to load the session file, if the session file doesn't exist we just log in with our Instagram username and password.

Getting the followers

Now let's see how can we get the followers of your Instagram account. The interesting thing is that you can get anyone's followers by specifying their username except private accounts. So here you can create a test Instagram account and login with that, then you can specify the real username where you want to retrieve followers. This prevents you from logging in with your real account which may cause issues.

# instaloader profile object
profile = instaloader.Profile.from_username(L.context, 'username')

# Current followers
current_followers = [i.username for i in profile.get_followers()]

print(current_followers)

This will return a list of followers of the user account specified in the profile object.

Writing followers to a text file

Let's create a text file 'followers.txt' and write the followers into it.

with open('followers.txt', 'w+') as f:
    for followers in profile.get_followers():
        file = f.write(followers.username+"\n")

When you open the file 'followers.txt' you can see all your followers listed line by line. Here we use the 'w+' file access mode to update the followers in each iteration.

Getting un-followers

Now let's get into the main part. To do this first we need to store the currently fetched followers in a list, which we have done previously, and compare it with the 'followers.txt' file in two different time stamps.  If some followers on the current timestamp which is the current followers are not in the 'followers.txt' file simply print those names and that is our un-followers.

import time

def get_unfollowers(response_time): 

    while True:

        # insta loader profile object
        profile = instaloader.Profile.from_username(L.context, 'username')

        # Current usernames
        current_followers = [i.username for i in profile.get_followers()]

        # Getting previously stored usernames
        with open('followers.txt', 'r') as f:
            usernames = f.read().splitlines()
        
        # Converting into sets
        set_of_current_followers = set(current_followers)
        set_of_previous_usernames = set(usernames)

        # Finding unfollowers
        unfollowers = set_of_previous_usernames.difference(set_of_current_followers)

        if len(unfollowers) == 0:
            pass
        else:
            print(unfollowers)
            
        # Storing the usernames to followers.txt 
        with open('followers.txt', 'w+') as f2:
            for followers in profile.get_followers():
                file2 = f2.write(followers.username+"\n")

        
        # Specifying time interval
        time.sleep(response_time)

get_unfollowers(10)

Let's understand what this function does

First, it loads the followers of the username specified in the profile object and stores them in the current_followers list.

After that, it opens the file 'followers.txt' and compares it with the current followers. Here we are finding the difference between the two sets to filter our un-followers from the current and previous followers list. When someone unfollows you it is being printed as a set in the console lively.

Then the followers are stored in the 'followers.txt' file. This whole thing is running inside a while loop so this process will repeat every 10 seconds given as the response time.

You can modify this code and host it on any server so it runs lively. When hosting on a server make sure to give a very long response time. You can check the server logs for finding the un-followers list and can also create a webhook that sends a notification to your phone or computer whenever someone unfollows you.

Here is the full version of the code:

import instaloader
import time

# Loading instaloader object
= instaloader.Instaloader()

# Username
username = "test_insta_username"
password = "test_insta_password"

try:
    # Loading session stored in the system
    L.load_session_from_file(username)
    
except:
    # Login with username and password
    L.login(username, password)

def get_unfollowers(response_time): 

    while True:

        # insta loader profile object
        profile = instaloader.Profile.from_username(L.context, 'username')

        # Current usernames
        current_followers = [i.username for i in profile.get_followers()]

        # Getting previously stored usernames
        with open('followers.txt', 'r') as f:
            usernames = f.read().splitlines()
    
        set_of_current_followers = set(current_followers)
        set_of_previous_usernames = set(usernames)

        # Finding unfollowers
        unfollowers = set_of_previous_usernames.difference(set_of_current_followers)

        if len(unfollowers) == 0:
            pass
        else:
            print(unfollowers)
            
        # Storing the usernames to followers.txt 
        with open('followers.txt', 'w+') as f2:
            for followers in profile.get_followers():
                file2 = f2.write(followers.username+"\n")

        
        # Specifying time interval
        time.sleep(response_time)