Last active
May 25, 2017 00:21
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@echan00 That script does not seem to address the rate limit error.