|\| | |-| € [)

How to retrieve Twitter account followers

Follow nihed on Twitter

With Ruby, Imagination is your limit. Our example is a way to retrieve a list of followers for an account.
For this sample, I’m using the twitter Gem.

  1. $ sudo gem install twitter

Now, we can explore our code.
First we must call our libraries (Gem).

  1. require "rubygems"
  2. require "twitter"

Next, we should configure an OAuth connexion.

  1. Twitter.configure do |config|
  2.   config.consumer_key = "AAAA"
  3.   config.consumer_secret = "BBBB"
  4.   config.oauth_token = "CCCC"
  5.   config.oauth_token_secret = "DDDD"
  6. end

(AAAA,BBBB,CCCC and DDDD) values can be retrieved on http://dev.twitter.com/apps/new when you create a new application.
After, We must initialize some variables

  1. myfile = File.new("write.txt", "w")
  2. client = Twitter::Client.new
  3. count=0
  4. cursor1=-1
  • myfile: file that be used to create a cvs file
  • client: our connexion with twitter service
  • count: number of count retrieved
  • cursor1: twitter send 100 per 100 account, this variable is used to follow this list

Finally, We can launch our code

  1. while (cursor1 != 0) do
  2. result =client.followers("Account","cursor"=>"#{cursor1}")
  3. cursor1=result.next_cursor
  4. users=result.users
  5. puts count
  6.  users.each do |follower|
  7.    count+=1
  8.     myfile.puts "\"#{count}\",\"#{follower.name}\",\"#{follower.screen_name}\",\"#{follower.url}\",\"#{follower.followers_count}\",\"#{follower.location}\",\"#{follower.created_at}\",\"#{follower.description}\",\"#{follower.friends_count}\",\"#{follower.lang}\",\"#{follower.time_zone}\",\"#{follower.verified}\""
  9.  end
  10.  sleep(5)
  11. end

While, our cursor is not equal 0, we retrieve accounts information using the request followers parameters the name of the account and the position of the cursor. We update our variables and we store the result in the our CVS file.

Categories: OpenSource, programmation, Ruby

RFID in shoes » « Facebook Graph with Ruby

2 Comments

  1. Thanks. I’m updating a dormant Rails project and needed to see if its Twitter account was still active and accessible. This worked well.

  2. I am getting error
    “undefined method `next_cursor’ for #”

Leave a Reply

Your email address will not be published.

*

Copyright © 2017 |\| | |-| € [)

Theme by Anders NorenUp ↑