DEV Community

Connor Van Etten
Connor Van Etten

Posted on

Find Instagram Users Who Don't Follow You Back With Python

Quick Start

  • Download User Data from Instagram
  • Retrieve Data from Insta via email
  • Locate Following and Followers file
  • Run Script
  • Code Below

Download User Data

To access your user data from instagram, please go to Instagram.com, and log into your account. Follow the order below :

☰ → Settings → Privacy and Security → Data Download → Request Download

Once here it will give you the option to download your data as either an HTML or a JSON file. For our python code to work properly, please select JSON and then hit next and enter your password to send data.

Retrieve Data from Insta via email

When email is recieved, please select download information to the desired loaction on your computer.

Next locate the file within your downloads.

Locate Following and Followers file

Follow the path with the given folder to find your Followers.json and Following.json file.

From here we need to add these two files to a folder that contains our main.py file.

Run Script

Once the files are located within the correct folder with our python script all the is needed to do is run your python script. Enter the terminal, locate the directory and run python main.py. This will return the list of people who you are following who do not follow you back.

Enjoy! Thank you for reading!

Code :

import json

# get information from followers file
with open('followers.json') as f:  
    follower = json.load(f) 

# get information from following file
with open('following.json') as g:  
    following = json.load(g) 

followers_list = []
following_list = []

for i in follower['relationships_followers']:
  followers_list.append(i['string_list_data'][0]['value'])

for i in following['relationships_following']:
  following_list.append(i['string_list_data'][0]['value'])

followers_list.sort()
following_list.sort()


print(len(followers_list), len(following_list))

please_unfollow = []

for _ in range(len(following_list)):
    if following_list[_] not in followers_list:
        please_unfollow.append(following_list[_])

for _ in range(len(please_unfollow)):
  print(please_unfollow[_])
exit()
for _ in range(len(please_unfollow)):
  curr = check(please_unfollow[_])
  print(curr)
  if ',' in curr:
    curr = curr.replace(',', '')
  elif 'K' in curr:
    please_unfollow[_] = 'official_account'
    continue
  elif 'M' in curr:
    please_unfollow[_] = 'official_account'
    continue

# Check if user is "official" aka a professional account adjust if needed.
  if int(curr) >= 2000:
    please_unfollow[_] = 'official_account'
  print(curr)

# remove all occurences of 'official_account' in please_unfollow
while 'official_account' in please_unfollow:
  please_unfollow.remove('official_account')

print(please_unfollow)
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
therayyanawaz profile image
Md Rayyan Nawaz • Edited

I've updated Connor Van Etten's code to fix the file fetching issues. I've made the necessary changes, so let me know if you encounter any problems.

CODE:

import json

with open('followers.json') as f:  
    followers_data = json.load(f)

with open('following.json') as g:  
    following_data = json.load(g)

followers_list = []
following_list = []

for item in followers_data:
    if 'string_list_data' in item and len(item['string_list_data']) > 0:
        followers_list.append(item['string_list_data'][0]['value'])
    else:
        print(f"Missing or empty 'string_list_data' in follower: {item}")

for item in following_data['relationships_following']:
    if 'string_list_data' in item and len(item['string_list_data']) > 0:
        username = item['string_list_data'][0]['value']
        following_list.append(username)
        print(f"Added {username} to following list")
    else:
        print(f"Missing or empty 'string_list_data' in following: {item}")

followers_list.sort()
following_list.sort()

print(f"Number of followers: {len(followers_list)}")
print(f"Number of following: {len(following_list)}")

please_unfollow = [user for user in following_list if user not in followers_list]

print("Users to unfollow (not following back):")
for user in please_unfollow:
    print(user)

def check(username):
    return "1000"

for index in range(len(please_unfollow)):
    user = please_unfollow[index]
    curr = check(user)

    if ',' in curr:
        curr = curr.replace(',', '')

    if 'K' in curr or 'M' in curr:
        please_unfollow[index] = 'official_account'
        continue

    if int(curr) >= 2000:
        please_unfollow[index] = 'official_account'

please_unfollow = [user for user in please_unfollow if user != 'official_account']

print("\nFinal list of users to unfollow (excluding official accounts):")
for user in please_unfollow:
    print(user)
Enter fullscreen mode Exit fullscreen mode