Skip to content

Instantly share code, notes, and snippets.

@robinsloan
Created June 1, 2016 03:38
Show Gist options
  • Save robinsloan/383448232edd32be40dea80ca345e0bb to your computer and use it in GitHub Desktop.
Save robinsloan/383448232edd32be40dea80ca345e0bb to your computer and use it in GitHub Desktop.
Courage!
require "rubygems"
require "twitter"
# get these from apps.twitter.com
CONSUMER_KEY = "abcd"
CONSUMER_SECRET = "abcd"
OAUTH_TOKEN = "abcd"
OAUTH_TOKEN_SECRET = "abcd"
TWITTER_USER = "your_username" # needs to be the one associated with keys above
TIMEOUT = 5 # a good default value
client = Twitter::REST::Client.new do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.access_token = OAUTH_TOKEN
config.access_token_secret = OAUTH_TOKEN_SECRET
end
the_preserved = ["usernames", "you_do_not", "want_to_unfollow", "here"]
the_preserved = the_preserved.map do |username| username.downcase end
puts "Preserving:"
puts the_preserved
puts ""
following_ids = client.friend_ids(TWITTER_USER).to_a
save_file = File.open("followings-bkup.txt", "a") # open in append mode
following_ids.each do |id|
name = client.user(id).screen_name.downcase
if the_preserved.include?(name)
puts "...preserving #{name}"
else
begin
puts "UNFOLLOWING #{name}"
client.unfollow(id)
save_file.puts name
save_file.flush
rescue Twitter::Error::TooManyRequests => error
puts "Got an error, probably rate-limiting... waiting to try again"
sleep(error.rate_limit.reset_in)
end
end
sleep(TIMEOUT)
end
puts "done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment