i've noticed..

2011, Aug 05

Social Nerds Track Twitter Followers with github

Update: Be sure to check the updated source code. I wasn’t accounting for the paging mechanism in the Twitter API for followers. The old code won’t work with over 100 followers.

Recently, I logged into Github to find a notification for a new mention. When I went to check it out, I found that I was mentioned because someone was logging all of their tweets with Github.

Being as obsessive about my low follower count as I am, I thought it would be really cool to apply a similar tactic to Twitter followers. I was able to create a script to do everything I need to track my Twitter followers with Git (and optionally Github) in just a little over an hour.

Before I began coding, I had to register a new application with Twitter and throw all of my keys/secrets into a config.yml. I also set up a git repository in directory with the script. This way, I didn’t have to worry about checking for a repository - my script can just assume it is there.

Then the code… it’s pretty simple, so I’ll just paste it.

require "yaml"
require "twitter"
require "git"

key_file = YAML.load_file('config.yml')
g = Git.init('.')

Twitter.configure do |config|
  config.consumer_key = key_file['consumer_key']
  config.consumer_secret = key_file['consumer_secret']
  config.oauth_token = key_file['oauth_token']
  config.oauth_token_secret = key_file['oauth_token_secret']
end

follower_file = File.open('followers','w')

cursor = -1 
followers = [] 
begin 
  response = Twitter.followers :cursor => cursor 
  followers += response.users 
  cursor = response.next_cursor 
end while cursor > 0

followers = followers.sort{|x,y| x.id <=> y.id}.collect{|x| x.screen_name}
followers.each do |f|
  follower_file.puts f
end
follower_file.close

g.commit_all('Followers for '+Time.now.to_s)
g.push

That’s it. I set this up to run in a cron job twice a day, and now I have all kinds of green +’s and red -’s next to my follower list. It will fail if there hasn’t been any changes to your follower list, but that’s fine because there’s nothing worth writing home about.

I may stop pushing to github, just to limit annoyances to my followers there, but I don’t think I need to worry about the list being up there because it’s not any thing that you can’t get by visiting (scraping if you’re a bot) my already public profile. One thing I really like about it being on GitHub is that I can use the RSS feed to passively keep tabs on when things change. I never have to visit that repository just to find out there are no changes.

You can find the full project here: https://github.com/justinxreese/justinxreese-followers

  1. laphotos reblogged this from justinreese
  2. romanticshop reblogged this from justinreese
  3. euvreau reblogged this from justinreese
  4. justinreese posted this