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.



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

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.



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

def follower_bots
  @follower_bots
end

#followersObject (readonly)

Returns the value of attribute followers.



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

def followers
  @followers
end

#screen_nameObject (readonly)

Returns the value of attribute screen_name.



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

def screen_name
  @screen_name
end

Instance Method Details

#a_bot?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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



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

def client
  config.client.client
end

#count_followers_and_bots(response) ⇒ Object



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

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



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

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



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

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



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

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