Skip to content

Instantly share code, notes, and snippets.

@willf
Created November 23, 2011 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willf/1387532 to your computer and use it in GitHub Desktop.
Save willf/1387532 to your computer and use it in GitHub Desktop.
Track tweets on some topic
require 'rubygems'
require 'tweetstream'
require 'date'
TweetStream.configure do |config|
config.consumer_key = '<your key>'
config.consumer_secret = '<your secret>'
config.oauth_token = '<your token>'
config.oauth_token_secret = '<your token secret>'
config.auth_method = :oauth
config.parser = :json_gem
end
# Change the words you want to track
TweetStream::Client.new.track('football', 'baseball', 'soccer', 'cricket') do |status|
begin
# The Tweet id
id = status.id
# The text of the tweet, with new lines (returns) replaced by spaces
txt = status.text.gsub(/\n/," ")
# The date of the tweet, printed out in a slightly more useful form for
# our purposes
d = DateTime.parse(status.created_at).strftime("%Y-%m-%d\t%H:%M:%S")
puts [id,txt,d].join("\t")
rescue Exception => e
puts "!!! Error: #{e.to_s}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment