Control of friends and followers on Twitter (API 1.1 update)

More than 2 years ago (that's a lot of time!) I published a simple Python script to monitor a Twitter account using Tweepy: basic account information, inactive friends and new/lost followers. But this script stopped working some time ago because Twitter updated its API to version 1.1. This update made obligatory using authentication to make any request and they also modified the request limits. Before the update, there was a limit of 150/350 requests per hour, depending on whether the request was authenticated or not, but now these limits are per request type and per 15 minutes. For example, to get a list of friends you can make a maximum of 15 requests per quarter of hour, but you can make other 15 to get a list of followers. If someone is late (like me) with the new API here you can find the full changelog.

Before starting to modify the code I had to update the Tweepy version too (2.1). The best and easiest way is using pip:
 

$ pip install tweepy

 
Now time to take a look at the code. The first important change was the authentication issue. So it is necessary creating a new application in our developer profile https://dev.twitter.com/apps/ to obtain the “Consumer key” and “Consumer secret” and the access token credentials, “Access token” and “Access token secret (follow the Step 1 in the link). Instead of a user authentication it is possible to perform an application-only authentication. However, there are more documentation and examples for user authentication, so no reason to complicate it more ;p

Due to the modification in the request limits we have a nice error if we execute the old code:
 

TweepError([{'message': 'Rate limit exceeded', 'code': 88}],)

 
With API 1.0 you were able to make a maximum of 150 requests per hour, and with each request it was possible to obtain a list of 100 friends/followers. Now we have a limit of 15 requests per 15 minutes, so with the same code we get a lower amount of users. However, the new API update also added some new request types, like the possibility to get a list of up to 5,000 follower/friend ids per request! So the new implementation of the script takes these ids, compares them with the local file and then makes some bulk requests to get the users' information, but just in the case of new followers, saving requests.

After these modifications we can use the script as before and without errors (if we don't reach the limits, of course). The annoying thing of this is that we have to create a Twitter application and copy the credentials within the script, or use an alternative option, like getting these values from environment variables. You can download the new code from here.

Is your code available on

Is your code available on github? I did a quick search but no results.

Hi Glen, It was not Github,

Hi Glen,

It was not Github, you are right. I just added a bunch of scripts in a new repo, so you can find it here:

https://github.com/jesparza/scripts/blob/master/twitCheck.py

Cheers!

Jose