Skip to content

Instantly share code, notes, and snippets.

@ronhornbaker
Last active May 25, 2017 00:21
Show Gist options
  • Save ronhornbaker/7817176 to your computer and use it in GitHub Desktop.
Save ronhornbaker/7817176 to your computer and use it in GitHub Desktop.
Get list of friends of a Twitter user, using the Twitter gem v5 (https://github.com/sferik/twitter), the Twitter APIv1.1, and Ruby.
require 'rubygems'
require 'twitter'
# see https://github.com/sferik/twitter
def twitter_client
Twitter::REST::Client.new do |config|
config.consumer_key = "XXXXXX"
config.consumer_secret = "XXXXXX"
config.access_token = "XXXXXX"
config.access_token_secret = "XXXXXX"
end
end
def fetch_all_friends(twitter_username, max_attempts = 100)
# in theory, one failed attempt will occur every 15 minutes, so this could be long-running
# with a long list of friends
num_attempts = 0
client = twitter_client
myfile = File.new("#{twitter_username}_friends_list.txt", "w")
running_count = 0
cursor = -1
while (cursor != 0) do
begin
num_attempts += 1
# 200 is max, see https://dev.twitter.com/docs/api/1.1/get/friends/list
friends = client.friends(twitter_username, {:cursor => cursor, :count => 200} )
friends.each do |f|
running_count += 1
myfile.puts "\"#{running_count}\",\"#{f.name.gsub('"','\"')}\",\"#{f.screen_name}\",\"#{f.url}\",\"#{f.followers_count}\",\"#{f.location.gsub('"','\"').gsub(/[\n\r]/," ")}\",\"#{f.created_at}\",\"#{f.description.gsub('"','\"').gsub(/[\n\r]/," ")}\",\"#{f.lang}\",\"#{f.time_zone}\",\"#{f.verified}\",\"#{f.profile_image_url}\",\"#{f.website}\",\"#{f.statuses_count}\",\"#{f.profile_background_image_url}\",\"#{f.profile_banner_url}\""
end
puts "#{running_count} done"
cursor = friends.next_cursor
break if cursor == 0
rescue Twitter::Error::TooManyRequests => error
if num_attempts <= max_attempts
cursor = friends.next_cursor if friends && friends.next_cursor
puts "#{running_count} done from rescue block..."
puts "Hit rate limit, sleeping for #{error.rate_limit.reset_in}..."
sleep error.rate_limit.reset_in
retry
else
raise
end
end
end
end
fetch_all_friends("screen_name_of_twitter_user")
@msdundar
Copy link

Thanks for it. I suggest using multiple-assignment, which looks better:

num_attempts, running_count, cursor = 0, 0, -1

@marlonmantilla
Copy link

Guys getting
private method `next_cursor' called for #Twitter::Cursor:0x0000000311d138 (NoMethodError)

It seems that this method is no longer public is this code still working for you ?

@echan00
Copy link

echan00 commented Apr 30, 2015

@marlonmantilla you should look at this script, its newer: https://gist.github.com/sferik/9630519

@mjrode
Copy link

mjrode commented May 4, 2016

@echan00 That script does not seem to address the rate limit error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment