Skip to content

Instantly share code, notes, and snippets.

@teleological
Created February 14, 2012 19:36
Show Gist options
  • Save teleological/1829529 to your computer and use it in GitHub Desktop.
Save teleological/1829529 to your computer and use it in GitHub Desktop.
How to make a Twitter list from a list of tweeps someone is following using ruby and twitter gem
require 'rubygems'
require 'twitter'
follower = ARGV[0] || raise("USAGE: #$0 <follower_name> [list_name]")
list_name = ARGV[1] || ("FollowedBy" + follower.upcase)
# Requires read-write token
CONSUMER_KEY = YOUR_CONFIG_HERE
CONSUMER_SECRET = YOUR_CONFIG_HERE
OAUTH_TOKEN = YOUR_CONFIG_HERE
OAUTH_SECRET = YOUR_CONFIG_HERE
MAXLEN_LIST_NAME = 25
# Be gentle with Twitter API
# (your mileage may vary)
API_CHUNK_SIZE = 10
API_LOOP_DELAY = 3
MAX_RETRY = 3
Twitter.configure do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.oauth_token = OAUTH_TOKEN
config.oauth_token_secret = OAUTH_SECRET
end
Twitter.list_create(list_name.slice(0,MAXLEN_LIST_NAME))
tweeps = Twitter.friend_ids(follower).collection
puts "Adding #{tweeps.size} tweeps"
added = 0
until tweeps.empty?
chunked_tweeps = tweeps.slice!(0,API_CHUNK_SIZE)
tries = 0
begin
tries += 1
Twitter.list_add_members(list_name,chunked_tweeps)
added += 1
print '.'
STDOUT.flush
sleep API_LOOP_DELAY
rescue
print 'x'
STDOUT.flush
sleep API_LOOP_DELAY
retry if tries < MAX_RETRY
end
end
puts "\nAdded #{added} tweeps"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment