Skip to content

Instantly share code, notes, and snippets.

@benjaminjackson
Created August 1, 2012 16:07
Show Gist options
  • Save benjaminjackson/3228260 to your computer and use it in GitHub Desktop.
Save benjaminjackson/3228260 to your computer and use it in GitHub Desktop.
Calculate how spammy your Twitter followers are
# Author: @benjaminjackson
require 'rubygems'
require 'uri'
require 'net/http'
require 'json'
SCREEN_NAME = 'benjaminjackson'
class Float
def round_decimal
(self * 100).round.to_f / 100
end
end
class BotScore
def initialize
@ids = []
@follower_counts = []
end
def run
slice_number = 0
cursor = -1
while cursor != 0
# Grab the authorized user's followers
uri = URI.parse("http://api.twitter.com/1/followers/ids.json?screen_name=#{SCREEN_NAME}&cursor=#{cursor}")
response = Net::HTTP.get_response(uri)
response_dict = JSON::parse(response.body)
@ids += response_dict["ids"]
cursor = response_dict["next_cursor"].to_i
end
# Get user details in batches of 100, the largest that Twitter will allow
@ids.each_slice(100) do |slice|
puts "checking #{slice_number +=1} of #{@ids.length/100 + 1} pages"
uri = URI.parse("http://api.twitter.com/1/users/lookup.json")
response = Net::HTTP.post_form(uri, { 'user_id' => slice.join(',') })
users = JSON::parse(response.body)
@follower_counts += users.map { |u| u['followers_count'].to_i }
end
# Run the numbers (from http://bit.ly/PsQBIC)
sorted = @follower_counts.sort
length = @follower_counts.length
median = length % 2 == 1 ? sorted[length/2] : (sorted[length/2 - 1] + sorted[length/2]).to_f / 2
percent = @follower_counts.reject { |count| count > 2 }.length / @follower_counts.length.to_f * 100
puts "Your followers have a median of #{median} followers themselves"
puts "#{percent.round_decimal}% of your followers have 2 or less followers"
end
end
bot_score = BotScore.new
bot_score.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment