Figuring Out Who to Unfollow – Using Python and the Twitter API



Twitter is excellent for following people who have interesting things to say.  This is not news.  For me, though, the issue has become that once I get above 300 people to follow, I start to lose the real usefulness of the platform, so I’ve tried to keep my following list at or below that number.  As it recently got above 320, I decided it was time to prune the list.
There are two main ways to prune in my opinion.  The first is to unfollow people who really aren’t that interesting to me, or whose relevance has decreased because of changes in my life or theirs.  The second is much easier, and that’s to check who hasn’t tweeted in a while and unfollow them.  A chose the value of “a month” for “a while”, given that that seems like a reasonable time in which people should at least tweet once if they’re serious about it.
Checking to see who hadn’t tweeted in a month was something of an interesting challenge, because it meant having to go to the website, click on each person, and see when they last tweeted.  This got tedious very quickly, so I decided to use Python and the Twitter API to do it.
I installed the Python Twitter API wrapper here:
following the instructions to first install the dependencies and then install the wrapper itself.  The install process was straightforward.
I set up an access token on the twitter website ( so that I could authenticate in my script, as some of the functions in question require authentication.
Once I completed that, I wrote a python script that first authenticates me, then requests the list of my followers.  Then, for each friend, it gets their last public status, and then checks when it was created against a month ago.  If the person hasn’t tweeted in a month, it stores their username in a list that gets printed at the end of the script run.
I put in a sleep call so that it only queries for the last status every fifteen seconds – this is to prevent me from going over the 300 requests per hour that is imposed on Twitter API requests.  This could be a lower amount of time but I don’t really care how quickly it finishes as I only run it every so often anyway.
Here’s the script, which I called “”:
#!/usr/bin/env python
import time, twitter
api = twitter.Api(consumer_key='insertyourshere', consumer_secret='insertyourshere, access_token_key='insertyourshere', access_token_secret='insertyourshere')
usersToUnfollow = []
oneMonthInSeconds = 2629743
twitterUN = 'thomas_quinlan'
twitterID = 14639500# You can get your Twitter ID at

# We need the time in seconds since the epoch.
now = time.time()

# Let's make sure we can successfully log in.
if api.VerifyCredentials():        
    print "Twitter credentials verified.\n"
    print "Error verifying twitter credentials.\n"

#First get the user IDs of my friends (the people I'm following).
friends = api.GetFriends(user=twitterUN)

# For each of those friends, we need to get their last tweet, and whether it was more than a month ago.
for friend in friends:
    lastStatus = api.GetUserTimeline(screen_name=friend.screen_name,count=1)
    for status in lastStatus:
        if status.created_at_in_seconds < (now-oneMonthInSeconds):

# Now that we have a list of people whose last tweet is more than a \\
  month ago, we can print them out as a list of people to unfollow.
print "Unfollow these folks: "
    for user in usersToUnfollow:
        print user
I’m thinking that this could likely be improved for efficiency in some way, but realistically I don’t need it to be.  It could also be extended to automatically unfollow the particular people in question, but I’d prefer that it just tell me, as some people (such as family members) don’t tweet too often but I still want to follow them.
If you can think of any immediate improvements I’m always happy to hear them!  Leave me a comment if you do.