Skip to content

Instantly share code, notes, and snippets.

@kent
Created June 24, 2010 12:57
Show Gist options
  • Save kent/451413 to your computer and use it in GitHub Desktop.
Save kent/451413 to your computer and use it in GitHub Desktop.
Find all Twitter Friends using recursion
# Here is a little recursive method that pulls in all Twitter friends using recursion
require 'rubygems'
require 'twitter'
def get_twitter_friends_with_cursor(cursor, list, client)
# Base case
if cursor == 0
return list
else
hashie = client.friends(:cursor => cursor)
hashie.users.each {|u| list << u } # Concat users to list
get_twitter_friends_with_cursor(hashie.next_cursor,list,client) # Recursive step using the next cursor
end
end
# Generate Oauth
user = User.find.first
oauth = Twitter::OAuth.new(TOKEN,SECRET)
oauth.authorize_from_access(user.twitter_token,user.twitter_secret)
client = Twitter::Base.new(oauth)
# recursively get all Twitter friends
# pass the default -1 cursor, an empty list and the Oauth client
all_friends = get_twitter_friends_with_cursor(-1,[],client)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment