Created
November 5, 2022 13:21
-
-
Save assafelovic/603f5589201d2a1e77ffed8200aa5cac to your computer and use it in GitHub Desktop.
Remove all bot followers from your twitter account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tweepy | |
from googletrans import Translator | |
__author__ = 'assafelovic' | |
''' | |
Run this script carefully! | |
This script will remove all potential bot followers from your twitter account. The bot detection function | |
is very basic mainly for inspiration, but feel free to enhance it or use 3rd party tools. | |
The current method for detecting bots is based on language, description and total followers. | |
The bot will block user one by one and then unblock them. | |
If you are following your followers, you won't be subscribed to them anymore once you run this job. | |
- Install tweepy module using pip. To install tweepy run below command in your terminal. | |
sudo pip install tweepy | |
- (optional) Install google translater using pip. To install run sudo pip install googletrans | |
Please replace consumer_key and consumer_secret. Visit https://apps.twitter.com to get your consumer_key and | |
consumer_secret. | |
Once you generate consumer key create an access token from same twitter page. Replace access_token and | |
access_token_secret with provided values. Then run the script. | |
''' | |
consumer_key = '<consumer_key>' | |
consumer_secret = '<consumer_secret>' | |
access_token = '<access_token>' | |
access_token_secret = '<access_token_secret>' | |
translator = Translator() | |
client = tweepy.Client(bearer_token=bearer_token, | |
wait_on_rate_limit=True, | |
consumer_key=consumer_key, | |
consumer_secret=consumer_secret, | |
access_token=access_token, | |
access_token_secret=access_token_secret) | |
def detect_lang(text): | |
lang = 'en' | |
try: | |
lang = translator.detect(text).lang | |
except Exception as e: | |
print(e) | |
return lang | |
def is_user_bot(user_id): | |
id = user.id | |
current_user = client.get_user(id=id, user_fields=['description', 'public_metrics']) | |
description = current_user.data.description | |
total_followers = current_user.data.public_metrics.get("followers_count") | |
user_lang = detect_lang(description) | |
return total_followers < 2 or (user_lang != 'en' or not description) | |
# Instantiate a new Paginator with the Tweepy method and its arguments | |
paginator = tweepy.Paginator(client.get_users_followers, client.get_me().data.id, user_auth=True, max_results=1000, user_fields=['description']) | |
for user in paginator.flatten(): | |
follower_id = user.id | |
if is_user_bot(follower_id): | |
client.block(target_user_id=follower_id) | |
client.unblock(target_user_id=follower_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment