-
-
Save bertomartin/4741641 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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