Skip to content

Instantly share code, notes, and snippets.

@n0sys
Last active March 29, 2023 21:27
Show Gist options
  • Save n0sys/fd4b921255f7059e91e6375fc5b6c56a to your computer and use it in GitHub Desktop.
Save n0sys/fd4b921255f7059e91e6375fc5b6c56a to your computer and use it in GitHub Desktop.
Script to find out who doesn't follow you back on Instagram
import requests
import json
import hashlib
from bs4 import BeautifulSoup
import re
import time
'''
Login to instagram and add the requested cookies to the script below
!!!! USE AT YOUR OWN RISK !!!!
Instagram are known for their continuous efforts to deal with bots on their platform !
This script might get detected: If detected your account would be locked and you will have to verify your identity through MFA
'''
session_id = $SESSION_ID # Replace $SESSION_ID with SESSION_ID cookie
csrf_token = $CSRF_TOKEN # Replace $CSRF_TOKEN with csrf cookie
ds_user_id = $DS_USER_ID # Replace $DS_USER_ID with ds_user_id cookie
# cookies
jar = requests.cookies.RequestsCookieJar()
jar.set('sessionid', session_id, domain='.instagram.com', path='/')
jar.set('csrftoken', csrf_token, domain='.instagram.com', path='/')
# headers
x_ig_www_claim = 'hmac.AR2HT9lidkf1n8obwEuEwuAGAcb7BA04y8XseRIisyFUUNZG'
x_ig_app_id = '936619743392459'
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.5481.78 Safari/537.36'
sec_ch_ua = '"Not A(Brand";v="24", "Chromium";v="110"'
sec_ch_ua_mobile = '?0'
headers: dict = {
'X-Ig-Www-Claim': x_ig_www_claim,
'X-Ig-App-Id': x_ig_app_id,
'User-Agent': user_agent,
'Sec-Ch-Ua': sec_ch_ua,
'Sec-Ch-Ua-Mobile': sec_ch_ua_mobile
}
# Get username
url = "https://www.instagram.com/"
a = requests.get(url, cookies=jar, headers=headers)
soup = BeautifulSoup(a.text, 'html.parser')
json_script = json.loads(soup.find_all('script', type='application/json')[15].text)
json_script2 = json.loads(json_script['require'][0][3][0]['__bbox']['define'][62][2]['raw'])
insta_username = json_script2['config']['viewer']['username']
print("Username: "+insta_username)
# Get followers and followings count
url = "https://www.instagram.com/"+insta_username
a = requests.get(url, cookies=jar, headers=headers)
soup = BeautifulSoup(a.text, 'html.parser')
content = soup.find("meta", property="og:description")["content"]
total_followers = int(re.match("[0-9]+",content)[0])
print('Total followers: ', total_followers)
total_followings = int(re.search(" [0-9]+",content)[0])
time.sleep(0.5)
print('Total followings: ', total_followings)
time.sleep(0.5)
# Fetch Followers
print("Fetching your followers...")
followers_hash: dict = {}
followers_counter = 0
base_count = 20
iteration_count = 0
max_id = None
while followers_counter < total_followers:
if total_followers - followers_counter < base_count:
base_count = total_followers - followers_counter + 1
if iteration_count == 0:
followers_url = "https://www.instagram.com/api/v1/friendships/"+ds_user_id+"/followers/?count="+str(base_count)+"&search_surface=follow_list_page"
else:
followers_url = "https://www.instagram.com/api/v1/friendships/"+ds_user_id+"/followers/?count="+str(base_count)+"&max_id="+max_id+"&search_surface=follow_list_page"
a = requests.get(followers_url, cookies=jar, headers=headers)
json_obj = json.loads(a.text)
try:
max_id = json_obj['next_max_id']
except:
pass
iteration_count += 1
for user in json_obj['users']:
username= user['username']
followers_hash[hashlib.sha256(username.encode('utf8')).hexdigest()] = username
followers_counter += base_count
time.sleep(1)
# Fetch Followings
print("Fetching your followings...")
followings_hash: dict = {}
followings_counter = 0
base_count = 20
max_id = 0
while followings_counter < total_followings:
if total_followings - followings_counter < base_count:
base_count = total_followings - followings_counter + 1
followings_url = "https://www.instagram.com/api/v1/friendships/"+ds_user_id+"/following/?count="+str(base_count)+"&max_id="+str(followings_counter)
a = requests.get(followings_url, cookies=jar, headers=headers)
json_obj = json.loads(a.text)
for user in json_obj['users']:
username= user['username']
followings_hash[hashlib.sha256(username.encode('utf8')).hexdigest()] = username
followings_counter += base_count
time.sleep(1)
print("People that follow you but you don't follow them :-)")
none_bool = False
for follower in followers_hash:
if follower not in followings_hash:
print('\t',followers_hash[follower])
none_bool = True
if none_bool:
print("Sad for them :D")
else:
print("Well there's no body..")
print("People that you follow but they don't follow you :-)")
none_bool = False
for following in followings_hash:
if following not in followers_hash:
print('\t',followings_hash[following])
none_bool = True
if none_bool:
print("Sad for you :D")
else:
print("Well there's no body..")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment