Skip to content

Instantly share code, notes, and snippets.

@kofigumbs
Last active April 14, 2020 22:31
Show Gist options
  • Save kofigumbs/d1db0926226fb061fb241df47c88b373 to your computer and use it in GitHub Desktop.
Save kofigumbs/d1db0926226fb061fb241df47c88b373 to your computer and use it in GitHub Desktop.
Bulk unfollow inactive users
source 'https://rubygems.org'
gem 'twitter'
require 'twitter' # v6.2.0
# Setup the twitter api
#
THRESHOLD = (Date.today - 90).to_time
CLIENT = Twitter::REST::Client.new do |config|
config.consumer_key = "" # TODO
config.consumer_secret = "" # TODO
config.access_token = "" # TODO
config.access_token_secret = "" # TODO
end
# Guard against rate limiting
#
def api(&block)
CLIENT.instance_eval &block
rescue Twitter::Error::TooManyRequests => error
reset = error.rate_limit.reset_in
puts "TOO MANY REQUESTS ... retrying in #{reset} seconds"
sleep reset
retry
end
# Cache stuff for subsequent runs
#
CACHE = File.readlines(".cache").map(&:strip)
def append_line(file_name, line)
File.open(file_name, "a") { |file| file.write "#{line}\n" }
end
### MAIN
api { friend_ids.to_a }.each do |user_id|
next if CACHE.include? user_id.to_s
timeline = api { user_timeline(user_id, include_rts: false, count: 1, trim_user: true) }
unless timeline.first && timeline.first.created_at > THRESHOLD
handle = api { user(user_id).screen_name }
api { unfollow(user_id) }
append_line "unfollowed", "https://twitter.com/#{handle}"
end
append_line ".cache", user_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment