We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

flyingmachine on 05/20/09


Tagged

ruby twitter


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

webstic
dbald
jjuarez


Follow your followers in Ruby


Published in: Ruby 


URL: http://www.flyingmachinestudios.com/2009/05/20/follow-your-followers-using-ruby/

  1. require 'twitter'
  2. # Check out the twitter gem docs for using oauth
  3. httpauth = Twitter::HTTPAuth.new("username", "password")
  4. base = Twitter::Base.new(httpauth)
  5. to_follow_ids = base.follower_ids - base.friend_ids
  6. unavailable_count = 0
  7. to_follow_ids.each do |tfid|
  8. begin
  9. base.friendship_create(tfid, true)
  10. rescue Twitter::General
  11. # Twitter::General is raised for 403 errors
  12. # Which occur when you're trying to follow someone who's been banned by twitter
  13. base.block(tfid)
  14. rescue Twitter::Unavailable
  15. # Wait and try again if twitter's telling you to wait
  16. sleep 5
  17. if unavailable_count < 3
  18. retry
  19. unavailable_count += 1
  20. end
  21. end
  22. end

Report this snippet 

You need to login to post a comment.