Skip to content

Instantly share code, notes, and snippets.

@ObjSal
Created February 7, 2023 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ObjSal/a83e60ee82193084b83b1cd05209572a to your computer and use it in GitHub Desktop.
Save ObjSal/a83e60ee82193084b83b1cd05209572a to your computer and use it in GitHub Desktop.
Print users not following back on twitter
#!/usr/bin/env python3
#####################################################################################
# WARNING: IF ACCOUNT HAVE MORE THAN 5K FOLLOWERS/FRIENDS THEN IT NEEDS TO BE TWEAKED
#####################################################################################
import math
import json
import requests
#####################################################################################
# INSTRUCTION: update USER_ID and the missing HEADERS values below
#####################################################################################
USER_ID = ""
HEADERS = {
"Authorization": "",
"x-csrf-token": "",
"User-Agent": "",
"Cookie": ''
"origin": "https://twitter.com",
"x-twitter-active-user": "yes",
"x-twitter-auth-type": "OAuth2Session",
"accept-encoding": "gzip, deflate, br",
}
def split(arr, size):
arrs = []
while len(arr) > size:
pice = arr[:size]
arrs.append(pice)
arr = arr[size:]
arrs.append(arr)
return arrs
def get_users_details(list):
url = 'https://api.twitter.com/1.1/users/lookup.json?user_id=' + list
request = requests.get(url, headers = HEADERS)
users = json.loads(request.text)
return users
def print_users_link(IDS):
# split the list in groups of 100 (API limitation)
groups = split(IDS, 100)
for group in groups:
ids = ''
for id in group:
ids += str(id) + ','
# remove last comma
ids = ids[:-1]
users = get_users_details(ids)
for user in users:
print('https://twitter.com/' + user['screen_name'])
# returns friends (accounts I follow)
# If account has more than 5K friends this function needs to be tweaked
def get_friends():
url = 'https://api.twitter.com/1.1/friends/ids.json?cursor=-1&count=5000&user_id=' + USER_ID
request = requests.get(url, headers = HEADERS)
users = json.loads(request.text)
return users['ids']
# return accounts that follow me
# If account has more than 5K followers this function needs to be tweaked
def get_followers():
url = 'https://api.twitter.com/1.1/followers/ids.json?cursor=-1&count=5000&user_id=' + USER_ID
request = requests.get(url, headers = HEADERS)
users = json.loads(request.text)
return users['ids']
friends = get_friends()
followers = get_followers()
# removes friends that follow back, leaving friends that don't.
not_following_back = list(set(friends) - set(followers))
print_users_link(not_following_back)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment