Skip to content

Instantly share code, notes, and snippets.

@Alexhuszagh
Last active August 30, 2019 14:23
Show Gist options
  • Save Alexhuszagh/df1e17e0f8664362153aba15d4e626b4 to your computer and use it in GitHub Desktop.
Save Alexhuszagh/df1e17e0f8664362153aba15d4e626b4 to your computer and use it in GitHub Desktop.
Block Followers of Accounts
'''
block_followers
===============
Block followers of a given account.
Block followers **does** not block an account if:
1. The account is verified (override with the `whitelist_verified=False`).
2. You are following the account (override with `whitelist_following=False`).
3. You sent the account a follow request (override with `whitelist_follow_request_sent=False`).
4. The account follows you (override with `whitelist_friendship=False`).
# Warnings
The rate-limiting factor is the number of API calls, which increases
exponentially with the number of whitelisted users. A memo is provided
to avoid repeatedly querying seen users, which also allows you to
continue an interrupted query with minimal overhead. Only whitelist
a small number of users.
'''
import tweepy
# Authenticate to Twitter
# You need to provide these values yourself.
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(
auth,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
compression=True
)
# Memo for accounts to whitelist.
WHITELIST = set()
# Memo for accounts previously seen, to avoid repeating work.
SEEN = set()
ME = api.me()
def add_whitelist(screen_name):
'''Add screen-name to the whitelist set.'''
user = api.get_user(screen_name=screen_name)
WHITELIST.add(user.id)
def get_followers(screen_name):
'''List all followers for account by screen_name.'''
ids = []
for page in tweepy.Cursor(api.followers_ids, screen_name=screen_name).pages():
ids.extend(page)
return ids
def get_user(user_id):
'''Get user information for user_id.'''
return api.get_user(user_id=user_id)
def get_friendship(source_id, target_id):
'''Get friendship object between two users.'''
return api.show_friendship(source_id=source_id, target_id=target_id)
def has_friendship(source_id, target_id):
'''Check if there exists a friendship between two users.'''
# Will return two friendships, in arbitrary order. We just want either
# following or followed_by./
friendship = get_friendship(source_id, target_id)[0]
return friendship.following or friendship.followed_by
def whitelist_user(user, **kwds):
'''Whitelist (do not block) a given user.'''
if user.verified and kwds.get('whitelist_verified', True):
# If the account is verified, whitelist them if `whitelist_verified`
# is set to True (the default).
return True
if user.following and kwds.get('whitelist_following', True):
# Whitelist the account if you follow them.
return True
elif user.follow_request_sent and kwds.get('whitelist_follow_request_sent', True):
# Whitelist the account if you sent a follow request to them.
return True
elif has_friendship(user.id, ME.id) and kwds.get('whitelist_friendship', True):
# Check if there's a friendship with the user.
return True
# Check if they follow or are followed by any of the whitelisted accounts.
return any(has_friendship(user.id, i) for i in WHITELIST)
def block_users(screen_names, **kwds):
'''Block all users for a given account, except whitelisted accounts.'''
for screen_name in screen_names:
followers = get_followers(screen_name)
for user_id in followers:
if user_id not in SEEN:
user = get_user(user_id)
SEEN.add(user_id)
if not whitelist_user(user, **kwds):
print(f'Blocked user={user.screen_name}')
api.create_block(user_id=user.id)
# SAMPLE USAGE
# add_whitelist(...)
# add_whitelist(...)
# block_users([...])
#
# For example, the following code will block all users following MSNBC, but whitelist those following CNN:
# add_whitelist('cnn')
# block_users(['msnbc'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment