Class: FollowersForUsers

Inherits:
Object
  • Object
show all
Defined in:
lib/oii_twitter_goodies/lib/followers_for_users.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ FollowersForUsers

Returns a new instance of FollowersForUsers.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 5

def initialize(opts={})
  opts = Hashie::Mash[opts]
  @screen_names = opts[:screen_names] || []
  @oauth_token = opts[:oauth_token]
  @oauth_token_secret = opts[:oauth_token_secret]
  @consumer_key = opts[:consumer_key]
  @consumer_secret = opts[:consumer_secret]
  @screen_names = [@screen_names].flatten
  Twitter.configure do |config|
    config.consumer_key = @consumer_key
    config.consumer_secret = @consumer_secret
    config.oauth_token = @oauth_token
    config.oauth_token_secret = @oauth_token_secret
  end
  @client = Twitter::Client.new
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 3

def client
  @client
end

#consumer_keyObject

Returns the value of attribute consumer_key.



3
4
5
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 3

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



3
4
5
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 3

def consumer_secret
  @consumer_secret
end

#oauth_tokenObject

Returns the value of attribute oauth_token.



3
4
5
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 3

def oauth_token
  @oauth_token
end

#oauth_token_secretObject

Returns the value of attribute oauth_token_secret.



3
4
5
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 3

def oauth_token_secret
  @oauth_token_secret
end

#screen_namesObject

Returns the value of attribute screen_names.



3
4
5
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 3

def screen_names
  @screen_names
end

Instance Method Details

#direction_ids_for(screen_name, direction) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 44

def direction_ids_for(screen_name, direction)
  cursor = -1
  ids = []
  while cursor != 0
    puts cursor
    begin
      data = Hashie::Mash[@client.send(direction+"_ids", screen_name, :cursor => cursor).attrs]
    rescue Twitter::Error::BadGateway
      puts "Got Twitter::Error::BadGateway error - usually this is just a missed connect from Twitter."
      sleep(10)
      retry
    rescue Twitter::Error::TooManyRequests
      puts "Got Twitter::Error::TooManyRequests error - we are rate limited and will sleep for 15 minutes. See you in a bit."
      sleep(15*60)
      retry
    end
    ids = ids|data.ids
    cursor = data["next_cursor"]
  end
  return ids
end

#follower_ids_for(screen_name) ⇒ Object



36
37
38
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 36

def follower_ids_for(screen_name)
  return direction_ids_for screen_name, "follower"
end

#friend_ids_for(screen_name) ⇒ Object



40
41
42
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 40

def friend_ids_for(screen_name)
  return direction_ids_for screen_name, "friend"
end

#grab_followersObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 22

def grab_followers
  results = {}
  @screen_names.each do |screen_name|
    puts "Current progress: #{@screen_names.index(screen_name)}/#{@screen_names.length} started"
    user = user_data_for(screen_name)
    follower_ids = follower_ids_for(screen_name)
    user_data = user_data_for(follower_ids)
    results[user.first.attrs] = user_data
    puts "Current progress: ##{@screen_names.index(screen_name)}/#{@screen_names.length} complete"      
  end
  puts "Done!"
  return results
end

#user_data_for(screen_names) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/oii_twitter_goodies/lib/followers_for_users.rb', line 66

def user_data_for(screen_names)
  screen_names = [screen_names].flatten
  user_data = []
  screen_names.each_slice(100) do |screen_name_set|
    begin
      user_data = [user_data|@client.users(screen_name_set)].flatten
    rescue Twitter::Error::BadGateway
      puts "Got Twitter::Error::BadGateway error - usually this is just a missed connect from Twitter."
      sleep(10)
      retry
    rescue Twitter::Error::TooManyRequests
      puts "Got Twitter::Error::TooManyRequests error - we are rate limited and will sleep for 15 minutes. See you in a bit."
      sleep(15*60)
      retry
    end
  end
  user_data
end