# Requirements: pip install tweepy fuzzywuzzy python-Levenshtein | |
import tweepy | |
import re | |
from fuzzywuzzy import fuzz | |
# Credentials go here (generate at: https://apps.twitter.com) | |
auth = tweepy.OAuthHandler('consumer_key', 'consumer_secret') | |
auth.set_access_token('access_token', 'access_token_secret') | |
# Connect to Twitter | |
api = tweepy.API(auth) | |
# Grab last 200 mentions | |
mentions = api.mentions_timeline(count=200) | |
# Loop through the mentions | |
for tweet in mentions: | |
# Calculate simple Levenshtein distance ratio (non-alpha characters stripped with regex) | |
ratio = fuzz.ratio("Your Profile Name Here", re.sub(r'([^\s\w]|_)+', '', tweet.user.name)) | |
# If the ratio is > 80 and followers below 100, report the user | |
if ratio > 80 and tweet.user.followers_count < 100: | |
# Submit the report | |
report = api.report_spam(user_id=tweet.user.id, screen_name=tweet.user.screen_name) | |
# Feedback with report | |
print("Reported: " + tweet.text + " - " + tweet.user.screen_name) |
For "Your Profile Name Here", make sure it's your profile name and not your screen name. For instance, mine is "Jackson Palmer"
What a blast from the past. Instructions I used to provide people via Twitter/email when I shared this gist with them:
- Install dependencies by running command in first line
- Schedule a cron (or preferred scheduler) job to execute the Python script every 60 seconds
Note: This worked at catching phishing bots back in 2018, but they've gotten a lot more advanced since. This likely won't do much in 2022.
Disclaimer: Comments will be deleted because this gist was getting spammed, and GitHub oddly don't provide a way to turn off commenting on gists.
I don't believe this has been spammed , just 4 replies right?
This actually simple and effective way of taking care of spambots with the use of Levenshtein diffing .
Modern day you could replace with Deep Learning model based classifiers in place of Levenshtein and it would work great.
^ should probably call out, you'll want to be running Python 3.*