Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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")
@yazinsai

This comment has been minimized.

Copy link

@yazinsai yazinsai commented Sep 6, 2014

Thanks for sharing Ron. This helped me out a ton!

@msdundar

This comment has been minimized.

Copy link

@msdundar msdundar commented Nov 22, 2014

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

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

This comment has been minimized.

Copy link

@marlonmantilla marlonmantilla commented Jan 15, 2015

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

This comment has been minimized.

Copy link

@echan00 echan00 commented Apr 30, 2015

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

@mjrode

This comment has been minimized.

Copy link

@mjrode 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