Last active
February 25, 2020 17:13
-
-
Save wybiral/7f4ba15ae663148ccd55d16b51cf7275 to your computer and use it in GitHub Desktop.
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
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 = [ | |
'#Bernie2020', | |
'#NotMeUs', | |
'#BernieWon', | |
'#M4A', | |
'#Medicare4All', | |
'#MedicareForAll', | |
'#BernieBeatsTrump', | |
'#FeelTheBern', | |
'#BernieOrBust', | |
'#BernieSanders2020', | |
] | |
BLOCK_BIO = [ | |
'socialist', | |
'socialism', | |
'marxist', | |
'marxism', | |
'🌹', | |
'#dsa', | |
'jacobin', | |
'bernie2020', | |
'sanders2020', | |
'notmeus', | |
'feelthebern', | |
'bernieorbust', | |
'sanders2020', | |
] | |
BLOCK_NAME = [ | |
'🌹', | |
'bernie2020', | |
'sanders2020', | |
'the bern', | |
] | |
def normalize(tweet): | |
user = tweet['user'] | |
name = user.get('name', user['screen_name']) | |
name = name.lower() | |
bio = user['description'] | |
if bio is None: | |
bio = '' | |
bio = ' '.join(bio.replace('\n', ' ').lower().split()) | |
text = tweet.get('text', '') | |
if 'extended_tweet' in tweet: | |
text = tweet['extended_tweet'].get('full_text', '') | |
text = ' '.join(text.replace('\n', ' ').lower().split()) | |
return { | |
'id': user['id'], | |
'name': name, | |
'handle': user['screen_name'], | |
'bio': bio, | |
'text': text, | |
} | |
def should_block(x): | |
if any(term in x['bio'] for term in BLOCK_BIO): | |
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('Text: ' + x['text']) | |
if should_block(x): | |
api.CreateBlock(user_id=x['id']) | |
print('BLOCKED') | |
print('') | |
except KeyboardInterrupt: | |
print('') | |
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