Skip to content

Instantly share code, notes, and snippets.

@sferik
Forked from ronhornbaker/twitter-friends.rb
Last active May 4, 2017 16:44
Show Gist options
  • Save sferik/9630519 to your computer and use it in GitHub Desktop.
Save sferik/9630519 to your computer and use it in GitHub Desktop.
require 'csv'
require 'twitter'
def twitter_client
@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
SLICE_SIZE = 100
def fetch_all_friends(twitter_username)
CSV.open("#{twitter_username}_friends_list.txt", 'w') do |csv|
twitter_client.friend_ids(twitter_username).each_slice(SLICE_SIZE).with_index do |slice, i|
twitter_client.users(slice).each_with_index do |f, j|
csv << [i * SLICE_SIZE + j + 1, f.name, f.screen_name, f.url, f.followers_count, f.location.gsub(/\n+/, ' '), f.created_at, f.description.gsub(/\n+/, ' '), 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
end
end
end
fetch_all_friends("screen_name_of_twitter_user")
@siruguri
Copy link

siruguri commented May 9, 2015

Update: replace f.verified with f.verified? - the former is a deprecated method in 5.1+

@RonnyValdivieso
Copy link

Hi! What function should I use for fetch all tweets instead of friend_ids() and users()?

@imlordeyaya
Copy link

Hello, I have changed this code a little to scrape follower_ids, set the slice to 5000, but still hitting api rate limit, any idea how to avoid that?

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