Skip to content

Instantly share code, notes, and snippets.

@ws
Created October 9, 2015 15:52
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 ws/37edec1cb592edfd2ad2 to your computer and use it in GitHub Desktop.
Save ws/37edec1cb592edfd2ad2 to your computer and use it in GitHub Desktop.
Calculate what % of your tweets are replies
require 'twitter'
client = Twitter::REST::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
# https://github.com/sferik/twitter/blob/master/examples/AllTweets.md
def collect_with_max_id(collection=[], max_id=nil, &block)
response = yield(max_id)
collection += response
response.empty? ? collection.flatten : collect_with_max_id(collection, response.last.id - 1, &block)
end
# https://github.com/sferik/twitter/blob/master/examples/AllTweets.md
def client.get_all_tweets(user)
collect_with_max_id do |max_id|
options = {count: 200, include_rts: true}
options[:max_id] = max_id unless max_id.nil?
user_timeline(user, options)
end
end
user = client.verify_credentials
username = user.screen_name
tweets = client.get_all_tweets(username)
reply_count = 0
tweets.each do |t|
if !t.in_reply_to_status_id.nil?
reply_count += 1
end
end
percentage_replies = ((reply_count.to_f / tweets.count.to_f) * 100).round(2)
puts "Tweets: #{tweets.count}"
puts "Replies: #{reply_count}"
puts "Replies make up #{percentage_replies}% of your tweets"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment