Skip to content

Instantly share code, notes, and snippets.

@00101010b
Last active November 11, 2015 01:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 00101010b/0f47c4bd353b7133b846 to your computer and use it in GitHub Desktop.
Save 00101010b/0f47c4bd353b7133b846 to your computer and use it in GitHub Desktop.
Simple script to block annoying twitter spammers. Requires twitter API codes.
#!/usr/bin/env python
# Import TwitterAPI
# you can install this library via the commandline:
# $ pip install --user TwitterAPI
from TwitterAPI import TwitterAPI
# setup TwitterAPI
# you will have to get an API key set from: https://apps.twitter.com/ before you can use this script.
api = TwitterAPI( \
consumer_key='redacted', \
consumer_secret='redacted', \
access_token_key='redacted', \
access_token_secret='redacted' \
)
# get a list of already blocked users.
blockedUsers = list(set(user['screen_name'] for user in api.request('blocks/list', {'include_entities': 'false', 'skip_status':'true'})))
# perform our search for iphone spam and get the list of users.
for user in list(set(post['user']['screen_name'] for post in api.request('search/tweets', {'q': 'iPh0ne 6 AND bi0', 'count':'100', 'include_entities':'false'}))):
# check if our current user is already in our block list
# if they are let us know about it.
if user in blockedUsers:
print "User '%s' already blocked." % user
continue
# if they aren't, block them.
else:
blocked = api.request('blocks/create', {'screen_name': user})
if blocked:
print "Blocked user: [ @%s ]." % user
@00101010b
Copy link
Author

You will need to get an Twitter API key from https://apps.twitter.com/ before using this script. Make sure you review any blocked twitter accounts on twitter after this script has been run, this is so you don't block the wrong people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment