Create a list of your followers & un-followers on Instagram using python.

create a list of all your followers, Unfollowers on Instagram using python.

How can we get the names of all our followers on Instagram? Simply just log in to your account and check the followers on Instagram itself. But it is way hard for you to get all the names of your Unfollowers whom you are following back especially if you have a lot of connections on Instagram. But python is able to do that, here we are creating a python solution that gives you a list of all your followers, whom you are following as well as your Unfollowers.

First of all, we need a module named Instaloader which is a tool used to fetch metadata from Instagram. So just run 'pip install instaloader' in your command shell.

$ pip install instaloader

Now you can log in to your Instagram account within the command shell itself. After logging it stores a session file on your system so that you can re-login easily.

$ instaloader --login <your_insta_username>

Loading the session file

import instaloader

class Insta_info:

    def __init__(selfusername):

        self.username = username
        self.loader = instaloader.Instaloader()
        self.profile = instaloader.Profile.from_username(self.loader.context,self.username)

    def Login(self):

        login = self.loader.load_session_from_file(self.username)

        return login

insta_info = Insta_info("username")


Here we created a class Insta_info that contains the username, the instaloader object, and the profile object as its instance variables. The Insta_info object is created and stored inside the variable inst_info as shown in the above code. The Login method is responsible for re-logging into our Instagram account by loading the session file we saved in our system.

Getting the list of followers

Before we dive into the code, we need to create two text files. One is for storing all your follower's usernames and the other is to store all the names of whom you are following.

Writing followers to followers.txt

def get_my_followers(self):
    for followers in self.profile.get_followers():
        with open("followers.txt","a+") as f:
            file = f.write(followers.username+'\n')
        print("Writing followers...")

The method 'get_my_followers' is written inside the class Insta_info. Now after calling insta_info.Login(), Call insta_info.get_my_followers()  


The 'get_followers' is an instaloader inbuild function that retrieves all the usernames of followers corresponding to a particular user account. Then the usernames of all your followers are written into 
followers.txt as shown in the above picture.

Writing followees(following) to followees.txt

def get_my_followees(self):
    for followees in self.profile.get_followees():
        with open("followees.txt","a+") as f:
            file = f.write(followees.username+'\n')
        print("Writing followees...")

Now we have the list of followers in the followers.txt file, So there is no need of calling insta_info.get_my_followers again so you can comment on it. The method get_my_followees is for getting a list of all the users whom you are following, So call insta_info.get_my_followees next. Same as in the previous case this method also writes all usernames to followees.txt

Getting the list of un-followers

Now we have the lists of our following usernames as well as followers' usernames. That's ok, but the question is, How to get all our Unfollowers whom we are following back?. So here we need to apply some logic, In fact, the Unfollowers are actually the names that are in your following list but not in the follower's list, so by comparing the two text files created earlier we get a list of names and that is actually your Unfollowers.

def get_my_unfollowers(self):
    followers_file = set(open("followers.txt").readlines())
    followees_file = set(open("followees.txt").readlines())

    unfollowers_set = followees_file.difference(followers_file)
    for unfollowers in unfollowers_set:

In the above method, we are appending all the usernames one by one to the corresponding sets. Set is a python implementation of actual sets in mathematics. Sets have different types of operations like union, intersection, difference, etc. Here we are using 'difference' as an operation.

When executing this method all your Unfollowers names will appear in the terminal( You don't need to call other methods this time).