Skip to content

Instantly share code, notes, and snippets.

@robinsloan
Created July 20, 2021 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinsloan/bee1a32e550c00d3501813d4c14fbb17 to your computer and use it in GitHub Desktop.
Save robinsloan/bee1a32e550c00d3501813d4c14fbb17 to your computer and use it in GitHub Desktop.
This script will sync your current followings to one of your lists. It will add users to the list who you are following, and remove users from the list who you are not following.
require "rubygems"
require "twitter"
require "date"
# get these from apps.twitter.com
CONSUMER_KEY = "foo"
CONSUMER_SECRET = "bar"
OAUTH_TOKEN = "blee"
OAUTH_TOKEN_SECRET = "baz"
TWITTER_USER = "foobar" # needs to be the username associated with keys above
LIST_ID = 1234567890 # this will be a long numeric ID; get it from the list URL
TIMEOUT = 1 # 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
following_ids = client.friend_ids(TWITTER_USER).to_a
list_members = client.list_members(LIST_ID).to_a
list_members_ids = list_members.map do |member| member.id end
following_ids.each do |id|
unless list_members_ids.include?(id)
"Id #{id} not in list, adding"
client.add_list_member(LIST_ID, id)
end
end
list_members_ids.each do |list_member_id|
begin
unless following_ids.include?(list_member_id)
puts "No longer following #{list_member_id}, so, removing from list"
client.remove_list_member(LIST_ID, list_member_id)
end
rescue Twitter::Error::TooManyRequests => error
puts "Got an error, probably rate-limiting... waiting #{error.rate_limit.reset_in} seconds to try again"
sleep(error.rate_limit.reset_in)
retry
end
end
puts "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment