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
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
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
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.
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).
You may also like: Download Instagram-saved posts automatically.