Intridea Company Blog
TweetStream: Ruby Access to the Twitter Streaming API
Twitter’s Streaming API is one of the most exciting developments in the Twitter API in some time. It gives you the ability to create a long-standing connection to Twitter that receives “push” updates when new tweets matching certain criteria arrive, obviating the need to constantly poll for updates. TweetStream is a Ruby library to access the new API.
Installation
Installation of TweetStream is simple, it’s available as a gem on GitHub and Gemcutter.org. To install it from GitHub:
gem sources -a http://gems.github.com gem install intridea-tweetstream
To install it from Gemcutter:
gem sources -a http://gemcutter.org gem install tweetstream
Usage
TweetStream creates a long-standing HTTP connection to Twitter, so unlike other Twitter libraries you don’t simply run it once and deal with the results. Instead, you provide a block that will be yielded to with each new status that arrives. The most basic example is:
require 'rubygems'
require 'tweetstream'
TweetStream::Client.new('user','pass').sample do |status|
puts "[#{status.user.screen_name}] #{status.text}"
end
This will provide you with a small sample snapshot of all of the updates being posted to Twitter at this moment and print them to the screen. There are also methods available to track single-word keywords as well as the updates of a specified list of user ids (integers, not screen names). You can do that like so:
# Track the terms 'keyword1' and 'keyword2'
TweetStream::Client.new('user','pass').track('keyword1', 'keyword2') do |status|
puts "[#{status.user.screen_name}] #{status.text}"
end
# Track users with IDs 123 and 456
TweetStream::Client.new('user','pass').follow(123, 456) do |status|
puts "[#{status.user.screen_name}] #{status.text}"
end
Handling Deletion/Limit Notices (Updated in 0.1.4)
Sometimes the Streaming API will send messages other than statuses. Specifically, it does so when a status is deleted or rate limitations have caused some tweets not to appear in the stream. To handle these, you can use the on_delete and on_limit methods. Example:
TweetStream::Client.new('user','pass').on_delete{ |status_id, user_id|
Tweet.delete(status_id)
}.on_limit { |skip_count|
# do something
}.track('intridea') do |status|
# do something with the status like normal
end
Daemonization
One of the most useful features of TweetStream is its built-in daemonization functionality. This allows you to create scripts that run in the background of your machine rather than taking up an active process. To create a daemon script, you simply use TweetStream::Daemon instead of TweetStream::Client. Here’s an example:
require 'rubygems'
require 'tweetstream'
# The third argument is an optional process name.
TweetStream::Daemon.new('user','pass','tracker').track('keyword1','keyword2') do |status|
# Do something like dump the status to ActiveRecord
# or anything else you want.
end
If you were to place the above code in a file called tracker.rb you could then run ruby tracker.rb to see a list of daemonization commands such as start, stop, or run.
TweetStream is a simple wrapper on the Streaming API, but with built-in daemonization provides powerfully flexible means of accessing the Twitter Streaming API using familiar Ruby tools. More complete code documentation is available at rdoc.info.
Update: I overlooked the deletion and rate limit notices when I wrote the initial version of the gem. As of version 0.1.4 these are handled properly.
The Intridea Blogs
Blog Archive
- January 2010 (5)
- November 2009 (4)
- October 2009 (4)
- September 2009 (2)
- August 2009 (5)
- July 2009 (3)
- June 2009 (7)
- May 2009 (4)
- April 2009 (6)
- March 2009 (15)
- February 2009 (18)
- January 2009 (4)
- December 2008 (2)
- November 2008 (3)
- October 2008 (7)
- September 2008 (9)
- August 2008 (2)
- July 2008 (7)
- June 2008 (14)
- May 2008 (5)
- April 2008 (14)
- March 2008 (6)
- February 2008 (5)
- January 2008 (6)
- December 2007 (5)
- November 2007 (5)
- October 2007 (7)
- September 2007 (2)
- August 2007 (7)
- July 2007 (4)
- June 2007 (3)
- April 2007 (1)
Michael Bleigh