Skip to content

Instantly share code, notes, and snippets.

@DessertArbiter
Forked from jpmckinney/twitter_list_members.rb
Last active August 22, 2019 00:30
Show Gist options
  • Save DessertArbiter/ef25f96cb376b03b06912f3990906a0a to your computer and use it in GitHub Desktop.
Save DessertArbiter/ef25f96cb376b03b06912f3990906a0a to your computer and use it in GitHub Desktop.
Download all Twitter list members to CSV
require 'csv'
require 'twitter'
@client = Twitter::REST::Client.new do |config|
config.consumer_key = ''
config.consumer_secret = ''
config.oauth_token = ''
config.oauth_token_secret = ''
end
def all_list_members(list_owner_username, slug)
users = []
cursor = -1
while cursor.nonzero?
response = @client.list_members(list_owner_username, slug, :cursor => cursor)
users += response.users
cursor = response.next_cursor
end
users
end
def all_friends(user)
users = []
cursor = -1
while cursor.nonzero?
response = @client.friends(user, :cursor => cursor)
users += response.users
cursor = response.next_cursor
end
users
end
CSV.open('list.csv', 'w') do |csv|
csv << %w(ID Name ScreenName Location Description URL)
all_list_members('verified', 'politics').each do |user|
url = user.attrs[:entities][:url]
csv << [
user.id,
user.screen_name,
user.name,
user.location,
user.description,
url && url[:urls][0][:expanded_url],
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment