Skip to content

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Copy link

commented May 9, 2015

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

@RonnyValdivieso

This comment has been minimized.

Copy link

commented Mar 15, 2016

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

@imlordeyaya

This comment has been minimized.

Copy link

commented Sep 22, 2016

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
You can’t perform that action at this time.