twords 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 201fe822e2fdd8c1ec5168fd73deb7cfb8dd29db
4
- data.tar.gz: e3b057e402b2b3f1c5f47235de4f687836322c55
3
+ metadata.gz: 5c34c3b4415dc5107328e127712381efc0a84562
4
+ data.tar.gz: d90029ae4175a52dd886ac459d7023ac5aa1cd9a
5
5
  SHA512:
6
- metadata.gz: 0b55531d7030c0fce27b3cd0e54c82f739c7cc3ff5b90596d087a7a8cf73d2ec3b5434c21daed60423b3fb3fcf55f724f6a3bf0dd65f8460348eab92f0a0f8d9
7
- data.tar.gz: 85ebd69ab5677bf649cb618ae491f9bba4b3868c2030dd6254891bde8939f5509a3ac0e9d6ea2e3530a1cc0834e833d4a4e448d17dd6788cec442b30fd1bc506
6
+ metadata.gz: f7eada355aa2b6c1aaf30fcff26ca51bab3626ef05ed6dea0d40ca5431426787308cc04050290564f0efc29b31b740eb348edd212e04c71d0f2f5201b83b3480
7
+ data.tar.gz: 6b42cffee275451b7cf64d73003ca4d418b7bb9ba4a970de833407450c6f9076bceefb1d264990420054e3fd9cffd44737872e002e6ee256247dfb66fa4aedde
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ![twords](http://msimonborg.com/twords/twords.png)
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/twords.svg)](https://badge.fury.io/rb/twords)
4
+ [![Code Climate](https://codeclimate.com/github/msimonborg/twords/badges/gpa.svg)](https://codeclimate.com/github/msimonborg/twords)
5
+ [![Inline docs](http://inch-ci.org/github/msimonborg/twords.svg?branch=master)](http://inch-ci.org/github/msimonborg/twords)
6
+
3
7
  ## Twitter word clouds
4
8
 
5
9
  Count the occurrences of words in a tweeter's tweets.
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'twords/follower_bot_cop'
3
4
  require 'twords/configuration'
4
5
  require 'twords/instance_methods'
5
6
  require 'twords/version'
@@ -121,7 +121,8 @@ class Twords
121
121
  end
122
122
  alias include_urls= include_uris=
123
123
 
124
- # Set whether @-mentions should be counted. If true, any word beginning with "@" will be ignored.
124
+ # Set whether @-mentions should be counted.
125
+ # If true, any word beginning with "@" will be ignored.
125
126
  #
126
127
  # @param boolean [true, false] will raise an error if the value is not a Boolean value
127
128
  def include_mentions=(boolean)
@@ -129,16 +130,17 @@ class Twords
129
130
  @include_mentions = boolean
130
131
  end
131
132
 
132
- # Takes a block and stores for lazy evaluation to define the end of the time range being checked.
133
- # The return value of the block must respond to #to_time and return a Time object when called.
133
+ # Takes a block and stores for lazy evaluation to define the end of the
134
+ # time range being checked. The return value of the block must respond
135
+ # to #to_time and return a Time object when called.
134
136
  #
135
137
  # @return [Proc]
136
138
  def up_to(&time_block)
137
139
  @up_to_block = time_block
138
140
  end
139
141
 
140
- # Calls the Proc value of #up_to_block and calls #to_time on the return value. Expects a Time object
141
- # to be returned.
142
+ # Calls the Proc value of #up_to_block and calls #to_time on the return value.
143
+ # Expects a Time object to be returned.
142
144
  #
143
145
  # @return [Time]
144
146
  def up_to_time
@@ -0,0 +1,67 @@
1
+ require 'csv'
2
+ require 'pry'
3
+ require 'twords/config_accessible'
4
+
5
+ class Twords
6
+ class FollowerBotCop
7
+ include ConfigAccessible
8
+
9
+ attr_reader :screen_name, :followers, :follower_bots
10
+
11
+ def initialize(screen_name)
12
+ @screen_name = screen_name
13
+ @followers = []
14
+ @follower_bots = []
15
+ end
16
+
17
+ def client
18
+ config.client.client
19
+ end
20
+
21
+ def detect_bots(cursor: nil)
22
+ response = client.followers(screen_name, count: 200, cursor: cursor)
23
+ count_followers_and_bots(response)
24
+ next_cursor = response.attrs[:next_cursor]
25
+ return if next_cursor == 0
26
+ detect_bots(cursor: next_cursor)
27
+ rescue Twitter::Error::TooManyRequests => error
28
+ wait_out_rate_limit_error(error)
29
+ retry
30
+ end
31
+
32
+ def count_followers_and_bots(response)
33
+ response.attrs[:users].each do |user|
34
+ @followers << user
35
+ @follower_bots << user if a_bot?(user)
36
+ end
37
+ end
38
+
39
+ def wait_out_rate_limit_error(error)
40
+ reset_time = error.rate_limit.reset_in + 1
41
+ puts "Out of #{followers.count} followers, #{follower_bots.count} bots detected."
42
+ puts "That's a rate of #{percentage} out of 100."
43
+ puts "Hit rate limit, waiting #{reset_time} seconds. "
44
+ sleep reset_time
45
+ end
46
+
47
+ def a_bot?(user)
48
+ if user[:statuses_count] <= 10
49
+ user[:default_profile_image] == true ||
50
+ user[:followers_count].zero? ||
51
+ bot_like_timeline?(user)
52
+ end
53
+ end
54
+
55
+ def bot_like_timeline?(user)
56
+ return true if user[:statuses_count].zero?
57
+ return false if user[:protected] == true
58
+ timeline = client.user_timeline(user[:screen_name])
59
+ timeline.all? { |tweet| tweet.reply? || tweet.retweet? }
60
+ end
61
+
62
+ def percentage
63
+ return 0.0 if followers.count.zero?
64
+ (follower_bots.count / followers.count.to_f * 100).round(2)
65
+ end
66
+ end
67
+ end
@@ -71,6 +71,7 @@ class Twords
71
71
  # @api public
72
72
  # @return [Array<Array<String, Integer>>]
73
73
  def sort_words
74
+ audit
74
75
  @_sort_words ||= words.sort { |a, b| b.last <=> a.last }
75
76
  end
76
77
  alias words_forward sort_words
@@ -2,5 +2,5 @@
2
2
 
3
3
  class Twords
4
4
  # The current gem version
5
- VERSION = '0.2.2'.freeze
5
+ VERSION = '0.2.3'.freeze
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twords
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - M. Simon Borg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-23 00:00:00.000000000 Z
11
+ date: 2017-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: twitter
@@ -69,6 +69,7 @@ files:
69
69
  - lib/twords.rb
70
70
  - lib/twords/config_accessible.rb
71
71
  - lib/twords/configuration.rb
72
+ - lib/twords/follower_bot_cop.rb
72
73
  - lib/twords/instance_methods.rb
73
74
  - lib/twords/twitter_client.rb
74
75
  - lib/twords/version.rb