Twitter Mass Unfollow With Python

I decided to revive my old twitter account and wanted to unfollow all my old friends list,  the majority were gained from an auto-follow script a few years ago and are nothing but spam.   Given I want to start using twitter I only want to follow people I am interested in.   It was more difficult than I expected to simply purge your followers.

It appears twitter don’t allow most of the apps (and there are lots of them) to mass unfollow as part of their TOS.   I found this at http://www.hostnexus.com/blog/how-to-bulk-unfollow-on-twitter and decided to give the steps in there a shot.   I tried a few of the apps with the chrome check all plugin but all of them have daily limits and given up here. This is when I decided instead to just write a quick python script using the API.

It was easy enough to get started with the twitter API.
Simply visit https://dev.twitter.com and login with your account.  
Create a new application,   get your key.
Install the twitter python API at http://code.google.com/p/python-twitter/

You can get the latest version of the script at: https://github.com/Scott-Mc/python/blob/master/twitter-mass-unfollow.py and the version at the time of writing at the bottom of this post.

This was my profile before running the script
Twitter Friends Before Mass Purge

This is my profile now after running the script,

Twitter Friends After Mass Purge

This is the code at the time of writing,

#!/usr/bin/env python

#
#Mass unfollow friends on twitter
#

# Author
# – Scott Mcintyre
#
# Usage
#
# Get your keys from https://dev.twitter.com/
# Ensure it has write access
# Change the api key values marked ##### below with the twitter keys
# Install http://code.google.com/p/python-twitter/

import twitter
import time

api = twitter.Api(consumer_key=’#####’, consumer_secret=’#####’, access_token_key=’#####’, access_token_secret=’#####’)

start = time.time()
i = 0
friends=api.GetFriends()

while (1):
try:
for u in friends:
print “Deleting %s” % (u.name)
api.DestroyFriendship(u.id);
i = i + 1

except:
print “Possibly Rate limited? sleeping 60 seconds”
time.sleep(60)

elapsed = (time.time() – start)
print “Deleted %s friends in %0.2f seconds” % (str(i), elapsed)

Leave a Reply

Your email address will not be published. Required fields are marked *