Skip to content

Instantly share code, notes, and snippets.

@bgreenlee
Created July 28, 2009 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bgreenlee/157753 to your computer and use it in GitHub Desktop.
Save bgreenlee/157753 to your computer and use it in GitHub Desktop.
Script for unfollowing Twitter users who aren't following you, # or following users who are following you...or both
#!/usr/bin/env ruby
# twitdiff.rb
# Quickie script for unfollowing users who aren't following you,
# or following users who are following you...or both
# Requires Grackle (http://github.com/hayesdavis/grackle/tree/master):
# sudo gem sources -a http://gems.github.com
# sudo gem install hayesdavis-grackle
require 'rubygems'
require 'grackle'
require 'net/https'
require 'optparse'
# defaults
options = {
:username => "username",
:password => "password",
:quiet => false,
:follow => false,
:unfollow => false
}
OptionParser.new do |opts|
opts.banner = "Usage: twitdiff.rb [options]"
opts.on("-q", "--quiet", "Shhhh!") { |q| options[:quiet] = true }
opts.on("--follow", "Follow anyone who is following you.") { |f| options[:follow] = true }
opts.on("--unfollow", "Unfollow anyone who isn't following you.") { |f| options[:unfollow] = true }
opts.on("--sync", "Same as --follow --unfollow") do |s|
options[:follow] = true
options[:unfollow] = true
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
# Grackle supports OAuth, if someone wants to implement that option.
auth = {:type => :basic,
:username => options[:username],
:password => options[:password]}
client = Grackle::Client.new(:auth => auth)
followers = client.followers.ids?
friends = client.friends.ids?
friends_not_following = friends - followers
followers_not_friends = followers - friends
puts "You have #{followers.length} followers and #{friends.length} friends." unless options[:quiet]
puts "Friends who are not followers: #{friends_not_following.inspect}" unless options[:quiet]
puts "Followers who are not friends: #{followers_not_friends.inspect}" unless options[:quiet]
if options[:unfollow]
friends_not_following.each do |id|
begin
response = client.friendships.destroy!(:id => id)
puts "Unfollowed: #{response.screen_name} (#{response.id})" unless options[:quiet]
rescue Grackle::TwitterError => e
puts e
end
end
end
if options[:follow]
followers_not_friends.each do |id|
begin
response = client.friendships.create!(:id => id)
puts "Followed: #{response.screen_name} (#{response.id})" unless options[:quiet]
rescue Grackle::TwitterError => e
puts e
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment