Skip to content

Instantly share code, notes, and snippets.

@wybiral
Last active August 24, 2022 20:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wybiral/53b6a0e32bf9a03437f904553707a245 to your computer and use it in GitHub Desktop.
Save wybiral/53b6a0e32bf9a03437f904553707a245 to your computer and use it in GitHub Desktop.
from time import sleep
try:
from twitter import Api
except:
print('Requires python-twitter: pip install python-twitter')
exit(1)
api = Api(
consumer_key='...',
consumer_secret='...',
access_token_key='...',
access_token_secret='...',
)
SEARCH_TERMS = [
'bernie',
'#WarrenEndorseBernie',
'#Bernie2020',
'#NotMeUs',
'#BernieWon',
'#BernieBeatsTrump',
'#FeelTheBern',
'#BernieOrBust',
'#BernieOrVest',
'#BernieSanders2020',
'#BernieSquad',
'#NeverWarren',
'#DropOutWarren',
]
BLOCK_BIO = [
'socialist',
'socialism',
'marxist',
'marxism',
'🌹',
'☭',
'#dsa',
'dsam4a',
'demsocialists',
'dsaforbernie',
'jacobin',
'bernie2020',
'bernie 2020',
'sanders2020',
'sanders 2020',
'notmeus',
'feelthebern',
'bernieorbust',
'bernieorvest',
'sanders2020',
'freeassange',
'neverwarren',
'berniebeatstrump',
'bernie supporter',
'tulsi2020',
'bernie bro.',
]
BLOCK_URL = [
'bernie.',
'berniesanders.com',
'forbernie',
'donate/bern',
]
BLOCK_NAME = [
'🌹',
'☭',
'bernie2020',
'sanders2020',
'the bern',
'4bernie',
'freeassange',
'neverwarren',
]
def normalize(tweet):
user = tweet['user']
name = user.get('name', user['screen_name'])
bio = user['description']
if bio is None:
bio = ''
bio = ' '.join(bio.replace('\n', ' ').split())
url = user['url']
if url is None:
url = ''
return {
'id': user['id'],
'name': name.lower(),
'handle': user['screen_name'],
'bio': bio.lower(),
'url': url.lower(),
}
def should_block(x):
if any(term in x['bio'] for term in BLOCK_BIO):
return True
if any(term in x['url'] for term in BLOCK_URL):
return True
if any(term in x['name'] for term in BLOCK_NAME):
return True
return False
try:
for tweet in api.GetStreamFilter(track=SEARCH_TERMS):
x = normalize(tweet)
print('User: ' + x['name'] + ' (' + x['handle'] + ')')
print('Bio: ' + x['bio'])
print('URL: ' + x['url'])
if should_block(x):
api.CreateBlock(user_id=x['id'])
print('\033[91mBLOCKED\033[0m')
sleep(0.2)
print('')
except KeyboardInterrupt:
print('')
sleep(0.5)
blocked = api.GetBlocksIDs()
print('Accounts blocked: %i' % len(blocked))
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment