Skip to content

Instantly share code, notes, and snippets.

@fellipec
Created September 19, 2021 13:50
Show Gist options
  • Save fellipec/005f2d86c0a9666618069af9e60127c5 to your computer and use it in GitHub Desktop.
Save fellipec/005f2d86c0a9666618069af9e60127c5 to your computer and use it in GitHub Desktop.
Bloqueador de usuários no Twitter com o padrão NomeMONTEDENUMEROS
#!/usr/bin/python3
# coding=UTF-8
import tweepy
import datetime
import shelve
import re
#Block Pattern
bp = re.compile(r"^\D+\d{5,}$")
def store_reply(t):
with shelve.open('replies','c') as db:
if not t.id_str in db:
db[t.id_str] = t
def last_reply():
try:
with shelve.open('replies') as db:
k=list(db.keys())
return max(k)
except:
return 323720591371870218 # randon id to deal with edge cases
def matchbots(p):
res = []
with shelve.open('replies') as db:
for ids in db.keys():
usrnm = db[ids].user.screen_name
m = p.match(usrnm)
if m:
res.append(usrnm)
print("Encontrado bot " + usrnm)
res = list(dict.fromkeys(res)) # Remove duplicates
return res
def purge_bots(twtapi,botlist):
with shelve.open('bots','c') as db:
for b in botlist:
if not b in db:
try:
twtapi.create_block(b)
print("Bloqueando bot " + str(b))
except:
print("ERRO BLOQUEANDO BOT " + str(b))
db[b] = True
#MAIN PROGRAM
consumer_key="****"
consumer_secret="****"
access_token_key="****"
access_token_secret="****"
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token_key,access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
#List mentions
for tweet in tweepy.Cursor(api.mentions_timeline, since_id=last_reply()).items():
print(tweet.id_str)
print(tweet.created_at)
print(tweet.user.screen_name)
store_reply(tweet)
purge_bots(api,matchbots(bp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment