Make your own followers rankings with twitter API

0 Flares Twitter 0 Facebook 0 LinkedIn 0 Email -- 0 Flares ×

I’ve created a quick script a few months ago that reads and sorts the number of followers of different twitter users. A funny way to learn a bit how to use OAuth and Twitter API.

Who has more followers?

I know, if you ask to many twitter users, anyone will say that they don’t look at it, they never check if a friend or a colleague has more or less followers,  and so on. But I’m pretty sure, from time to time, everyone does it. There’s nothing wrong with it, as part of Twitter itself is both having good conversation, but also gaining followers to spread around our content.

As I’m a lazy guy, I was tired to go and do these checks manually, by loading each time the specific URL of the people I was looking upon. It was time to do some scripting!

NOTE: this script works in the Mac OS X Shell, I’ve never checked if it works as it is on a Linux bash, or any other shell. Adjust it as needed.

The first part to learn, in order to work with Twitter API, is to authenticate your script (like any other third party application) using OAuth. By going to , you can register your new app and obtain the two fundamental keys: the consumer_key and the consumer_secret. As the script/app has no interactive login, the two keys are used to generate the “bearer token”: the OAuth 2 Bearer Token can be used to make API requests on an application’s own behalf, without a user context. What’s also called Application-only authentication.

WARNING: the two keys are written in clear text in the script, so be careful to share it with others, as you are going to share also your private application keys!

This is the first part of the code:



I had to try a few times to properly trim the received string, but I finally managed to clean it by using a couple of reverse (the rev command) and a cut.

Once we have the bearer token, it can be used to run additional interrogations against Twitter API. What we’d like to know, is the number of followers of a given user. The output of the API are in json format, and this is the time that I learned about the awesome JQ tool: it’s basically like the sed command for json content. With it, you can parse any json output, and extract the desired field. In my case, the field is “followers_count”:


This one-liner extracts the number of followers of a single user, but what if I want to know this information for multiple users, and even sort them from highest to lowest number of followers? This is the last part of my script, that you can see below. The for cycle reads the information for every user in the “users” variable, writes them all in a temporary text file, sorts them, print it on screen, and deletes the file.



By simply firing the script and waiting a few seconds, you can have nice results like this one 🙂

Followers rank