Last active
August 29, 2015 14:14
-
-
Save binodpanta/d577b24bb776a99a43aa to your computer and use it in GitHub Desktop.
Track Tweets on your console in colored formatted display using Ruby, Rake and Awesome print
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
# If you use Rails, just add a lib/tasks/tweets.rb or something and paste this code there, then run rake tweets:tweet1 to run it | |
# This stops after 50 tweets but you don't have to! Now sit back and watch your custom twitter feed live on your console! | |
require 'tweetstream' | |
require 'awesome_print' | |
namespace :tweets do | |
desc "Tracks tweets with specified strings and stores them in db" | |
task tweet1: :environment do | |
TweetStream.configure do |config| | |
config.consumer_key = '' | |
config.consumer_secret = '' | |
config.oauth_token = '' | |
config.oauth_token_secret = '' | |
config.auth_method = :oauth | |
end | |
stream(["Weather", "Storm", "Juno", "Coursera", "Himalaya"]) | |
end | |
'''Each time there is a tweet on the specified items in tracked, perform an action''' | |
def stream(tracked) | |
tracked.each do |track| | |
tweets = 0 | |
TweetStream::Client.new.on_error do |message| | |
puts "Error #{message}" | |
end.track(track) do |t| | |
tweets += 1 | |
if tweets > 50 | |
break | |
else | |
# pretty print to console in color using awesome print | |
ap t | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment