A Python script to auto-follow all Twitter followers

In my recent fiddling around with Twitter I came across the Twitter API, which is surprisingly feature-complete. Since programming is one of my hobbies (as well as my occupation), I inevitably started fooling around with it and have already come up with something useful. I’m posting it here, so if you need to do the same thing that I am, you won’t have to reinvent the wheel.

One common thing that people do on Twitter is they follow everyone that follows them. This is good for social networking (or just bald self-promotion), as inbound links to your Twitter page show in the followers list of everyone that you’re following. You’d think Twitter itself would have a way to do this, but alas, it does not. So what I wanted to do is use a program to automatically follow everyone following me instead of having to manually follow each person.

Other sites that interface with Twitter will do it for you (such as TweetLater), but I’m not interested in signing up for another service, and I’m especially not interested in giving out my Twitter login credentials to anyone else. So I needed software that ran locally. A Google search turned up an auto-follow script written in Perl, but the download link requires registration with yet another site. I didn’t want to do that so I decided to program it for myself, which ended up being surprisingly simple.

My Auto-Follow script is written in Python. I decided to use Python because of the excellent Python Twitter library. It provides an all-Python interface to the Twitter API. You’ll need to download and install Python-Twitter (and its dependency, python-simplejson, if you don’t have it already; sudo apt-get install python-simplejson does the trick on Ubuntu GNU/Linux). Just follow the instructions on the Python-Twitter page; it’s really simple.

Now, create a new Python script named auto_follow.py and copy the following code into it:

# -*- coding: utf-8 -*-
#(c) 2009 Ben McIlwain, released under the terms of the GNU GPL v3.
import twitter
from sets import Set

username = 'your_username'
password = 'your_password'
api = twitter.Api(username=username, password=password)

following = api.GetFriends()
friendNames = Set()
for friend in following:

followers = api.GetFollowers()
for follower in followers:
    if (not follower.screen_name in friendNames):

Yes, it really is that simple. I’d comment it, but what’s the point? I can summarize its operation in one sentence: It gets all of your friends and all of your followers, and then finds every follower that isn’t a friend and makes them a friend. Just make sure to edit the script to give it your actual username and password so that it can sign in.

Run the script and you will now be following all of your followers. Pretty simple, right? But you probably don’t want to have to keep running this program manually. Also, I’ve heard rumors that the Twitter API limits you to following 70 users per hour (as an anti-spam measure, I’m guessing), so if you have more than 70 followers you’re not following, you won’t be able to do it all at once. Luckily, there’s a solution for both problems: add the script as an hourly cronjob. This will keep who you follow synced with your followers over time, and if you have a large deficit in who you follow at the start (lucky bastard), it’ll slowly chip away at it each hour until they do get in sync. In Ubuntu GNU/Linux, adding the following line to a text file in /etc/cron.d/ (as root) should do it:

0 * * * * username python /path/to/auto_follow.py >/dev/null 2>&1

This will run the auto_follow script at the top of each hour. You’ll need to set the username to the user account you want the job to run under — your own user account is fine — and set the path to wherever you saved the auto_follow script. Depending on your GNU/Linux distribution and which cron scheduler you have installed, you may not need the username field, and this line might go in a different file (such as /etc/crontab). Refer to your distro’s documentation for more information.

So that’s it. That’s all it takes to automatically auto-follow everyone who’s following you — a dozen or so lines of Python, one crontab entry, and one excellent library and API. Enjoy.

10 Responses to “A Python script to auto-follow all Twitter followers”

  1. Peter MacRobert Says:

    This is handy, but won’t work if you have more than 100 followers or friends. Both the twitter API methods to return friends and followers only return 100 users at a time. The twitter.py library takes a page variable to accomodate this, i.e.:

    following = []
    i = 1
    while True:
    f = api.GetFriends(page=i)
    if f.__len__() == 0: break
    i += 1
    following += f

    Bearing in mind this will hammer the twitter API, and will use up your hourly API call allocation before the script finishes if you have a large number of friends or followers. So you’ll need to batch this process up and execute it over a period of time.

    Hope it helps,


  2. Cyde Weys Says:

    Thanks Peter. You’re absolutely right about the limit. I found out about it while playing around with Python-Twitter probably a day after I posted this. Fortunately, so long as you are already kept up on who you’re following, this script should work as written. As I understand it (and please correct me if I’m wrong), when you call GetFollowers without a page parameter, it should return the 100 most recent followers. I’ve been running this auto-follow script as written on one of my accounts and it works. So long as you aren’t getting more than 100 followers an hour (you should only be so lucky!), it’ll at least keep up with all of the new people to auto-follow.

  3. Manish Says:

    Thanks man

    This helped me a lot …


  4. Steve Driscoll Says:

    Thanks for the tips. I have a question (bug) that I’m trying to resolve…

    I use the following code to get my list of friends which constitutes over 3k at the moment.

    c_time = time.time()

    friends = []
    i = 1
    while True:
    f = api.GetFriends(page=i)
    if f.__len__() == 0: break
    i += 1
    friends += f

    I’m then trying to determine each friends last update with this code…

    for friend in friends:
    last_u = (c_time – friend.status.created_at_in_seconds) / (60*60*24)
    if (last_u >= 10):
    print “%s last posted %.2f day(s) ago” % (friend.screen_name, last_u)

    This works for small lists of friends but not larger lists as I receive this error…

    Traceback (most recent call last):
    File “./twitter_api_poc.py”, line 77, in
    last_u = (c_time – friend.status.created_at_in_seconds) / (60*60*24)
    AttributeError: ‘NoneType’ object has no attribute ‘created_at_in_seconds’

    Any thoughts?

  5. Steve Driscoll Says:

    Ah…I figure out the problem. The person has never made an update! LOL.

  6. exinco Says:

    i’m interested in this script but i’m totally blur, how to implement it. i have no time to understand the program. btw you have a good sharing and thanks for that

  7. Get Me Followers Says:

    I’m sure it would be just as easy for Twitter to set it so that you could automatically follow everyone that follows you, but just think how that would encourage even more spam than there already is on Twitter. People would follow anyone and everyone. Hundreds of thousands of followers would not be uncommon and you would also be following hundreds of thousands. Someone like aplusk has over 2 million followers, but only follows, probably, a couple of dozen, which is manageable.

    I use programs that get me followers, like Get Me Followers (click above..heeheh..blatant self-promotion), but it takes work to keep my following list manageable as my Followers list grows. What would be good is being able to have separate lists (maybe in a user control panel) that shows who is both following you AND being followed by you as opposed to those that you follow and want nothing to do with following you.

  8. codewolf Says:

    This article inspired me to build my own. So I’ve made a more complete example in PHP if anyone needs a copy without having to pound the Twitter API with thousands of requests. My PHP version should only hit the API about 4 API uses an hour and it runs on a cron.

    Please feel free to port it to python if you want. The script Logs all your followers in a mysql database and welcomes them with an optional msg when it auto follows them.


    I hope it helps! Thanks again!

  9. istacks Says:

    hello, can you please email me at millistacks@gmail.com for more information about this script please. i need help with it.

  10. reverse phone cell lookup Says:

    By making use of this support, it is viable to successfully make your web site significantly
    more interactive and social. That the support of Blog
    site comments is hottest service that hand over again hyperlinks on your own weblog.
    The support features two chief plans that include prompt site visitors likewise as quite a lot of back
    links. You also ought to consider treatment on that the fine
    quality which can be certainly more chosen than amount in the time of submitting responses.