Skip to content

Instantly share code, notes, and snippets.

@mamantoha
Created December 28, 2015 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamantoha/753d8a2f59e80eabb600 to your computer and use it in GitHub Desktop.
Save mamantoha/753d8a2f59e80eabb600 to your computer and use it in GitHub Desktop.
Twitter Stats
require 'twitter'
require 'unicode_utils'
client = Twitter::Streaming::Client.new do |config|
config.consumer_key = "YOUR_CONSUMER_KEY"
config.consumer_secret = "YOUR_CONSUMER_SECRET"
config.access_token = "YOUR_ACCESS_TOKEN"
config.access_token_secret = "YOUR_ACCESS_SECRET"
end
# You can fetch up to 3,200 tweets for a user, 200 at a time.
puts statuses_count = client.user.statuses_count
current_user_id = client.current_user.id
collection = []
max_id = nil
options = { count: 200, include_rts: true }
while true do
options[:max_id] = max_id unless max_id.nil?
response = client.user_timeline(current_user_id, options)
collection += response
if response.empty?
collection.flatten
break
else
max_id = response.last.id - 1
next
end
end
profanity_regexp = /\s* ( \S* (ху[йяюєї]|п[иі]зд|[єї]б[ауе]|бл[ьєя]|сук[аиоу]|йоб|курв)+ \S* ) \s*/ix
bad_words_collection = collection.select { |tweet| tweet.text =~ profanity_regexp }
words = Hash.new { |h,k| h[k] = 0 }
bad_words_collection.each do |tweet|
words_arry = tweet.text.scan(profanity_regexp)
word = words_arry.dig(0, 0)
word = UnicodeUtils.downcase(word)
word.gsub!(/[^[:alnum:]]/, '')
words[word] += 1
end
(bad_words_collection.size.to_f / collection.size)*100
words_ordered = words.sort_by{|k,v| v}.reverse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment