Skip to content

Instantly share code, notes, and snippets.

@hectorperez
Forked from sferik/twitter-friends.rb
Last active August 11, 2016 20:43
Show Gist options
  • Save hectorperez/b34ae70ebc5c00f2561a to your computer and use it in GitHub Desktop.
Save hectorperez/b34ae70ebc5c00f2561a 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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment