Skip to content

Instantly share code, notes, and snippets.

@celesteh
Last active January 24, 2016 20:37
Show Gist options
  • Save celesteh/7e0e23dcebd9095e0e13 to your computer and use it in GitHub Desktop.
Save celesteh/7e0e23dcebd9095e0e13 to your computer and use it in GitHub Desktop.
'''
This is a python script to block retweeters of particular tweets.
The twitter API will only let you access the most recent 100 tweeters.
Also, people may continue to retweet the original, so you may wish to keep old evil tweets in your script and
re-run it every so often while the event is ongoing.
Your blocking will not prevent more people from retweeting it. As retweeters are effectively encouraging people
to harass you, you may wish to block all the followers of the retweeters, or at least the most prominent ones.
Here's what you have to do:
# Twitter Authorization
1. Go to apps.twitter.com
2. Click on Create New App
3. Fill in the stuff. None of it matters
4. Go to Keys and Access Tokens
5. Click on Create my access token
6. Copy the consumer key, consumer secret, access token, and access token secret
7. Paste them in the script (those first lines after the `import`s)
# Detective work stuff (not really)
For every tweet you want to block RTers of:
1. Find the evil tweet
2. Click the 3 dots on the bottom of it to get more actions
3. Select 'Copy Link to Tweet'
4. The ID of the tweet is the end part of the URL. If the URL is https://twitter.com/example/status/123456789,
the ID is 123456789
5. Paste the IDs into the script in the evil_tweets
# Python stuff
1. Open Terminal.app
2. Enter `pip install tweepy` and give it a few seconds
3. If it produces an error (a bunch of red text) use `sudo pip install tweepy`
4. Enter `python ` (don't forget the space!) (and don't hit return yet)
5. Drag this file to the Terminal window. Its path should be filled in
6. Hit return
It will take a while...
Every account that follows these suspicious accounts will be blocked by the user
who issued the API tokens. You can then export the list of blocked accounts
(on twitter.com) and give it to someone else to import.
# Notes
If you have any problems you can tweet @celesteh
And if it's because of an error you should probably take a screenshot of the
Terminal window.
If you want to stop the script just hit Ctrl+C
#Credits
These notes cribbed from @tennabey. See https://gist.github.com/danfishgold/5e39d65c03e9aa5a379e
'''
import tweepy
import re
from time import time, sleep
from datetime import datetime
auth = tweepy.OAuthHandler('CONSUMERKEY',
'CONSUMERSECRET')
auth.set_access_token('ACCESSTOKEN',
'ACCESSTOKENSECRET')
evil_tweets=['id numbers of tweets', 'get them from the last part of the url']
api = tweepy.API(auth)
me = api.me()
number_of_blocked = 0
friendship_limit = api.rate_limit_status()['resources']['friendships']['/friendships/show']
number_of_friendship_requests = friendship_limit['limit'] - friendship_limit['remaining']
i = 0;
for tweet in evil_tweets:
for retweet in api.retweets(tweet, 500):
if number_of_friendship_requests >= 175: # rate limit is 180 per 15 minutes
reset = api.rate_limit_status()['resources']['friendships']['/friendships/show']['reset']
continue_time = datetime.fromtimestamp(reset).strftime('%H:%M:%S')
print 'waiting for rate limit... (will continue at {})'.format(continue_time)
sleep(reset - time() + 1)
number_of_friendship_requests = 0
twerp = retweet.user.screen_name
print 'RTer #{}: {}'.format(i+1, twerp)
i = i+1
friendship = api.show_friendship(source_screen_name=twerp,
target_screen_name=me.screen_name)
if friendship[0].followed_by:
print '------Spared!'
else:
number_of_blocked += 1
#number_of_blocked += 1
#print 'blocked ({} already)'.format(number_of_blocked)
api.create_block(screen_name=twerp)
number_of_friendship_requests += 1
print 'blocked {} accounts'.format(number_of_blocked)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment