Class: Twords::FollowerBotCop

Inherits:
Object
  • Object
show all
Includes:
ConfigAccessible
Defined in:
lib/twords/follower_bot_cop.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ConfigAccessible

config

Constructor Details

#initialize(screen_name) ⇒ FollowerBotCop

Returns a new instance of FollowerBotCop.



10
11
12
13
14
# File 'lib/twords/follower_bot_cop.rb', line 10

def initialize(screen_name)
  @screen_name   = screen_name
  @followers     = []
  @follower_bots = []
end

Instance Attribute Details

#follower_botsObject (readonly)

Returns the value of attribute follower_bots.



8
9
10
# File 'lib/twords/follower_bot_cop.rb', line 8

def follower_bots
  @follower_bots
end

#followersObject (readonly)

Returns the value of attribute followers.



8
9
10
# File 'lib/twords/follower_bot_cop.rb', line 8

def followers
  @followers
end

#screen_nameObject (readonly)

Returns the value of attribute screen_name.



8
9
10
# File 'lib/twords/follower_bot_cop.rb', line 8

def screen_name
  @screen_name
end

Instance Method Details

#a_bot?(user) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/twords/follower_bot_cop.rb', line 46

def a_bot?(user)
  if user[:statuses_count] <= 10
    user[:default_profile_image] == true ||
    user[:followers_count].zero? ||
    bot_like_timeline?(user)
  end
end

#bot_like_timeline?(user) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/twords/follower_bot_cop.rb', line 54

def bot_like_timeline?(user)
  return true if user[:statuses_count].zero?
  return false if user[:protected] == true
  timeline = client.user_timeline(user[:screen_name])
  timeline.all? { |tweet| tweet.reply? || tweet.retweet? }
end

#clientObject



16
17
18
# File 'lib/twords/follower_bot_cop.rb', line 16

def client
  config.client.client
end

#count_followers_and_bots(response) ⇒ Object



31
32
33
34
35
36
# File 'lib/twords/follower_bot_cop.rb', line 31

def count_followers_and_bots(response)
  response.attrs[:users].each do |user|
    @followers << user
    @follower_bots << user if a_bot?(user)
  end
end

#detect_bots(cursor: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/twords/follower_bot_cop.rb', line 20

def detect_bots(cursor: nil)
  response = client.followers(screen_name, count: 200, cursor: cursor)
  count_followers_and_bots(response)
  next_cursor = response.attrs[:next_cursor]
  return if next_cursor == 0
  detect_bots(cursor: next_cursor)
rescue Twitter::Error::TooManyRequests => error
  wait_out_rate_limit_error(error)
  retry
end

#percentageObject



61
62
63
64
# File 'lib/twords/follower_bot_cop.rb', line 61

def percentage
  return 0.0 if followers.count.zero?
  (follower_bots.count / followers.count.to_f * 100).round(2)
end

#wait_out_rate_limit_error(error) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/twords/follower_bot_cop.rb', line 38

def wait_out_rate_limit_error(error)
  reset_time = error.rate_limit.reset_in + 1
  puts "Out of #{followers.count} followers, #{follower_bots.count} bots detected."
  puts "That's a rate of #{percentage} out of 100."
  puts "Hit rate limit, waiting #{reset_time} seconds.  "
  sleep reset_time
end