Created
November 5, 2013 16:44
-
-
Save luigi/7321962 to your computer and use it in GitHub Desktop.
An updated Tweet button counter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Before running: | |
# $ gem install twitter -v 4.8.1 | |
# | |
# Register a Twitter application to get auth credentials: | |
# https://dev.twitter.com/apps | |
# | |
# To run: | |
# $ ruby counter.rb upworthy.com | |
# | |
# Context: | |
# http://luigimontanez.com/2012/actually-social-media-buttons-work-really-well | |
# | |
require 'rubygems' | |
require 'twitter' | |
search_term = ARGV[0] | |
total_tweets = 0 | |
from_button = 0 | |
max_id = nil | |
pages = 5 | |
options = {:count => 100, :result_type => 'recent'} | |
Twitter.configure do |config| | |
config.consumer_key = "YOUR_CONSUMER_KEY" | |
config.consumer_secret = "YOUR_CONSUMER_SECRET" | |
config.oauth_token = "YOUR_ACCESS_TOKEN" | |
config.oauth_token_secret = "YOUR_ACCESS_TOKEN_SECRET" | |
end | |
(1..pages).each do |page| | |
# Explanation of pagination method: | |
# https://dev.twitter.com/docs/working-with-timelines | |
options[:max_id] = max_id unless max_id.nil? | |
# Docs: http://rdoc.info/gems/twitter/Twitter/API/Search#search-instance_method | |
Twitter.search(search_term, options).results.each do |tweet| | |
total_tweets += 1 | |
from_button += 1 if tweet.source =~ /Tweet Button/ | |
max_id = tweet.id | |
end | |
end | |
percentage = (from_button.to_f / total_tweets.to_f) * 100 | |
puts "Search term: #{search_term}" | |
puts "Total tweets analyzed: #{total_tweets}" | |
puts "Tweets from button: #{from_button} (#{sprintf('%.1f', percentage)}%)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment