Steps to perform:
- Install and import modules.
- Get the list of all your followers on Instagram.
- Store the follower's username in a text file using python.
- Get the current followers again.
- 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 objectL = instaloader.Instaloader()# Usernameusername = "test_insta_username"password = "test_insta_password"try:# Loading session stored in the systemL.load_session_from_file(username)except:# Login with username and passwordL.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 objectprofile = instaloader.Profile.from_username(L.context, 'username')# Current followerscurrent_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 timedef get_unfollowers(response_time):while True:# insta loader profile objectprofile = instaloader.Profile.from_username(L.context, 'username')# Current usernamescurrent_followers = [i.username for i in profile.get_followers()]# Getting previously stored usernameswith open('followers.txt', 'r') as f:usernames = f.read().splitlines()# Converting into setsset_of_current_followers = set(current_followers)set_of_previous_usernames = set(usernames)# Finding unfollowersunfollowers = set_of_previous_usernames.difference(set_of_current_followers)if len(unfollowers) == 0:passelse:print(unfollowers)# Storing the usernames to followers.txtwith open('followers.txt', 'w+') as f2:for followers in profile.get_followers():file2 = f2.write(followers.username+"\n")# Specifying time intervaltime.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 instaloaderimport time# Loading instaloader objectL = instaloader.Instaloader()# Usernameusername = "test_insta_username"password = "test_insta_password"try:# Loading session stored in the systemL.load_session_from_file(username)except:# Login with username and passwordL.login(username, password)def get_unfollowers(response_time):while True:# insta loader profile objectprofile = instaloader.Profile.from_username(L.context, 'username')# Current usernamescurrent_followers = [i.username for i in profile.get_followers()]# Getting previously stored usernameswith open('followers.txt', 'r') as f:usernames = f.read().splitlines()set_of_current_followers = set(current_followers)set_of_previous_usernames = set(usernames)# Finding unfollowersunfollowers = set_of_previous_usernames.difference(set_of_current_followers)if len(unfollowers) == 0:passelse:print(unfollowers)# Storing the usernames to followers.txtwith open('followers.txt', 'w+') as f2:for followers in profile.get_followers():file2 = f2.write(followers.username+"\n")# Specifying time intervaltime.sleep(response_time)