I started a draft of this post over 3 years ago before I abandoned my blog, however I think it is still relevant/useful today. There is still no easy way of finding out those users who unfollow you on Twitter (“Losers” as I like to call them). Yes, there are several third party services available but they tend to spam your timeline, and require you to manually check on a regular basis to get a true picture.

I wrote a little PHP script to keep track of my new followers and losers. It’s a bit of a botch job, so I’m not going to upload all of the code. I will go through the theory behind it, and post little snippets. Hopefully, this will help you to develop a similar script, or more likely, ridicule me.

I used TwitterOAuth to connect to the Twitter API. Even 3 years later, I think this is still the best library available.

To start, you need an initial snapshot of your followers. The below code will connect to Twitter using defined keys, and grab the followers for a specified user. Then you just need to save this list. For ease, I store mine in a MySql database, however you could easily store it in a NoSQL database or even just a flat file. I save the ID of each follower along with the timestamp of when they were added to the database (not shown in the code).

$connection = new TwitterOAuth( CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret'] );
$followers_list = $connection->get( 'followers/ids', [ 'user_id' => $access_token['user_id'] ] );
$followers = $followers_list->ids;

Now, you have your initial snapshot, you can use this for a comparison in the future, and thus find out if you have gained or lost any followers. I had to spend a little bit of time to think about the best approach. Obviously, a simple count won’t work as you may have lost and gained the same number of followers. Twitter returns the IDs in a random order, so you can’t even compare the last ID in the list. In the end, I decided to sort the list arrays, implode them into strings, and then compare the checksums of each.

$checksum1 = crc32( implode( asort( $followers ) ) );
$checksum2 = crc32( implode( asort( $current ) ) );
if ( $checksum1 != $checksum2 ) {
  update_lists( $followers );
}

I have to admit that I can’t remember why I used the crc32 function to create the checksums. I’m sure there was a good reason at the time. If the two checksums don’t match then we know that I have gained and/or lost some followers, but not which users. This aspect is the biggest hack and could definitely do with some improving.. but it works so ho hum:

  1. I copy the “followers” table into a “temp” table
  2. I then loop through the latest followers array, and delete any user that appears in it from the temp table. If the ID does not exist in the temp table, then this is a new follower and so I insert them into the followers table.
  3. Any users left in the temp table are inserted into a “losers” table, and removed from followers (boo!).
  4. Truncate the temp table.

Simple! I also add a couple of safety checks just incase something has gone wrong and everyone is marked as a loser. Let me know if you need any help.

The final step is to create a cron job, and a little web page to display the results. Hope this helps, and that I see you in my followers list soon.

To finish, here are a few pages that I have found interesting over the last week:
https://css-tricks.com/intro-bedrock-wordpress/
https://learnredux.com/
http://youmightnotneedjquery.com/