Skip to content

Instantly share code, notes, and snippets.

@binodpanta
Last active August 29, 2015 14:14
Show Gist options
  • Save binodpanta/d577b24bb776a99a43aa to your computer and use it in GitHub Desktop.
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
# 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