-
-
Save joshuarrrr/7ff7a1fe05fabf9933cd to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# | |
# Register a Twitter application to get auth credentials: | |
# https://dev.twitter.com/apps | |
# | |
# To run: | |
# $ ruby counter.rb upworthy.com 500 | |
# | |
# Where 'upworthy.com' is the website and '500' is the number of tweets to analyze. | |
# | |
# Context: | |
# http://luigimontanez.com/2012/actually-social-media-buttons-work-really-well | |
# | |
require 'rubygems' | |
require 'twitter' | |
search_term = ARGV[0] | |
num_tweets = ARGV.fetch(1, 100).to_i | |
total_tweets = 0 | |
from_button = 0 | |
from_ios = 0 | |
from_android = 0 | |
from_web = 0 | |
from_facebook = 0 | |
from_hootsuite = 0 | |
from_ifttt = 0 | |
from_twitterfeed = 0 | |
from_dlvr = 0 | |
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_TOKEN_SECRET" | |
end | |
options = {:count => num_tweets, :result_type => 'recent'} | |
client.search(search_term, result_type: "recent").take(num_tweets).each do |tweet| | |
total_tweets += 1 | |
from_button += 1 if tweet.source =~ /Twitter for Websites/ | |
from_ios += 1 if tweet.source =~ /iOS/ | |
from_ios += 1 if tweet.source =~ /iPhone/ | |
from_ios += 1 if tweet.source =~ /iPad/ | |
from_android += 1 if tweet.source =~ /Android/ | |
from_web += 1 if tweet.source =~ /Web Client/ | |
from_facebook += 1 if tweet.source =~ /Facebook/ | |
from_hootsuite += 1 if tweet.source =~ /Hootsuite/ | |
from_ifttt += 1 if tweet.source =~ /IFTTT/ | |
from_twitterfeed += 1 if tweet.source =~ /twitterfeed/ | |
from_dlvr += 1 if tweet.source =~ /dlvr/ | |
max_id = tweet.id | |
end | |
puts "Search term: #{search_term}" | |
puts "Total tweets analyzed: #{total_tweets}" | |
puts "Tweets from button: #{from_button} (#{sprintf('%.1f', (from_button.to_f / total_tweets.to_f) * 100)}%)" | |
puts "Tweets from iOS: #{from_ios} (#{sprintf('%.1f', (from_ios.to_f / total_tweets.to_f) * 100)}%)" | |
puts "Tweets from Android: #{from_android} (#{sprintf('%.1f', (from_android.to_f / total_tweets.to_f) * 100)}%)" | |
puts "Tweets from Twitter.com: #{from_web} (#{sprintf('%.1f', (from_web.to_f / total_tweets.to_f) * 100)}%)" | |
puts "Tweets from Facebook: #{from_facebook} (#{sprintf('%.1f', (from_facebook.to_f / total_tweets.to_f) * 100)}%)" | |
puts "Tweets from Hootsuite: #{from_hootsuite} (#{sprintf('%.1f', (from_hootsuite.to_f / total_tweets.to_f) * 100)}%)" | |
puts "Tweets from IFTTT: #{from_ifttt} (#{sprintf('%.1f', (from_ifttt.to_f / total_tweets.to_f) * 100)}%)" | |
puts "Tweets from twitterfeed: #{from_twitterfeed} (#{sprintf('%.1f', (from_twitterfeed.to_f / total_tweets.to_f) * 100)}%)" | |
puts "Tweets from dlvr.it: #{from_dlvr} (#{sprintf('%.1f', (from_dlvr.to_f / total_tweets.to_f) * 100)}%)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment