Last active
December 16, 2015 16:59
-
-
Save lfcipriani/5466999 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
# LINK CATCHER | |
# | |
# usage: set a QUERY of interest, the loop will run a search and | |
# retrieve links parsed from tweets to build a ranking of | |
# the most popular based on frequency, retweets and favorites | |
# at the moment of execution. | |
# | |
# Press CTRL+C to stop and show the report | |
# The more you wait, the more quality you get | |
# requiring twitter gem (https://github.com/sferik/twitter) | |
require "twitter" | |
Twitter.configure do |config| | |
config.consumer_key = "FILL_THIS" | |
config.consumer_secret = "FILL_THIS" | |
config.oauth_token = "FILL_THIS" | |
config.oauth_token_secret = "FILL_THIS" | |
end | |
QUERY = "EdMotta" #query string to do a search | |
REQUEST_INTERVAL = 5 #seconds | |
@since = "" | |
@report = {} | |
at_exit do | |
puts "\nReport for #{QUERY} - Greater the score, more popular is the link\n\n" | |
ranking = @report.sort_by { |link, data| -data[:score] } | |
rank = 1 | |
ranking.each do |link| | |
puts "#{rank} | #{link[0]} | frequency: #{link[1][:frequency]} | retweets: #{link[1][:retweets]} | favorites: #{link[1][:favorites]} | score: #{link[1][:score]}" | |
rank += 1 | |
end | |
puts "\n\nCheck the report above\n" | |
end | |
loop do | |
puts "Executing search '#{QUERY}'..." | |
tweets = Twitter.search(QUERY, :count => 100, :result_type => "recent", :since_id => @since).results | |
puts " #{tweets.size} tweet(s) retrieved." | |
@since = tweets.first.id if !tweets.empty? | |
tweets.each do |tweet| | |
unless tweet.urls.empty? | |
tweet.urls.each do |an_url| | |
link_to_catch = an_url.expanded_url || an_url.url | |
puts " " + link_to_catch | |
if @report[link_to_catch] | |
# existent link, let's sum up things | |
@report[link_to_catch][:frequency] += 1 | |
@report[link_to_catch][:retweets] += tweet.retweet_count | |
@report[link_to_catch][:favorites] += tweet.favorite_count | |
else | |
# new link, let's instantiate | |
@report[link_to_catch] = { | |
:frequency => 1, | |
:retweets => tweet.retweet_count, | |
:favorites => tweet.favorite_count, | |
:score => 0 | |
} | |
end | |
@report[link_to_catch][:score] = @report[link_to_catch][:frequency] + @report[link_to_catch][:retweets] + @report[link_to_catch][:favorites] | |
end | |
end | |
end | |
puts "Done! Now waiting for #{REQUEST_INTERVAL} seconds..." | |
# let's not abuse | |
sleep REQUEST_INTERVAL | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment