Control of friends and followers on Twitter

I'm going to lay aside PDF files and malware to write a simple script to control friends and followers on Twitter. We use to have a lot of them and it's difficult to know if our friends haven't written some time ago or our followers have left. But we can use one of the multiple modules (talking about Python) to communicate with the Twitter API and solve this task. I've chosen Tweepy because I think it's very simple and well documented. What we want to obtain from Twitter is:

It's important to highlight that we cannot obtain all the friends/followers with one API request  but  only 100 each time. We can use the Cursors object from Tweepy to solve this very easily:

followersCursor = tweepy.Cursor(tweepy.api.followers,id=user)
for follower in followersCursor.items():
print follower.name

When we use the Twitter API we also have to take into account the API requests limits, because if we are not authenticated (it's the case) we can only perform 150 requests per hour and if we use the OAuth authentication the limit is 350 requests per hour. The idea is to execute this script two times a month to check our Twitter account so we shouldn't have any problems with that, except if we have a lot of friends/followers...

In order to obtain the activity of friends we should check the Status.created_at member of the User object or, if it does not exist, ask for the timeline of the user and get the last Status object to obtain the date of the last tweet. We should have in mind that, in case of protected tweets, we won't be able to access to this information.

The output of the script will show:

  • Information of the given Twitter account
  • Inactive friends: accounts not updated for X days
  • New followers
  • Lost followers

$ ./twitCheck.py eternaltodo
####################################################################################
User: eternaltodo
Name: Jose Miguel Esparza
Location: Pamplona
Language: es
Time Zone: Madrid
Creation: 2010-02-02 20:58:00
Last tweet: 2011-08-14 08:09:33
####################################################################################

####################################################################################
Friends: 174
Inactive Friends (12):
[2009-04-26 02:21:29] chris astacio (castacio)
[2010-12-09 16:05:01] XSS Hacker (hackersorg)
[2011-03-28 10:51:52] themoux (them0ux)
[2011-04-06 21:20:07] Felipe Manzano (feliam)
[2011-04-18 11:00:25] TROOPERS Conference (WEareTROOPERS)
[2011-04-22 05:17:22] Yuange (yuange1975)
[2011-04-28 12:46:02] CARO Workshop (caroworkshop)
[2011-05-14 11:01:45] Adrian P. (pagvac)
[2011-05-26 21:01:42] Ero (erocarrera)
[2011-05-30 17:21:10] Alvaro Ramon (alramonl)
[2011-06-02 20:59:48] Pablo Catalina (xkill)
[2011-06-18 15:37:34] TyMoPHoNO (TyMoPHoNO)
####################################################################################

####################################################################################
Followers: 306
New Followers (3):
Joseph McCray (j0emccray)
Max Nash (MaxNash1)
dorel (d0re1)
Lost Followers (1):
Santiago Vicente (smvicente)
####################################################################################

 

With these tips I've created a simple but useful script to check who of my followers are inactive and who has left because I'm too boring ;)

 

Update: Due to the new Twitter API release, this code is not longer valid. You can take a look at the new script here.

Thanks! This is very useful

Thanks! This is very useful stuff about Twitter.

Thanks! Multiple modules

Thanks! Multiple modules really helped me to solve this task. Twitter API is useful, but a beast to tame at times! :) Cheers!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.