Skip to content

Instantly share code, notes, and snippets.

@postmodern
Created September 27, 2009 03:04
Show Gist options
  • Save postmodern/194562 to your computer and use it in GitHub Desktop.
Save postmodern/194562 to your computer and use it in GitHub Desktop.
Watches tweets in real-time using TweetStream, and can save them using TokyoCabinet
require 'tweetstream'
require 'rufus/tokyo'
module SophSec
module Twitter
#
# Watches tweets using the TweetStream library.
#
# @param [Hash] options
#
# @option options [String] :user
# The twitter user to authenticate as.
#
# @option options [String] :password
# The twitter password to authenticate with.
#
# @option options [Array<String>, String] :keywords
# The keywords to track.
#
# @option options [Array<Integer>, Integer] :follow
# The Twitter User IDs to follow.
#
# @yield [status]
# A given block will receive the desired tweets in real-time.
#
# @yieldparam [TweetStream::Status] status
# The Hash of the tweet.
#
def Twitter.watch(options={},&block)
unless options[:user]
raise(RuntimeError,"a :user option must be specified",caller)
end
unless options[:password]
raise(RuntimeError,"a :password option must be specified",caller)
end
client = TweetStream::Client.new(options[:user],options[:password])
if options[:keywords]
keywords = [options[:keywords]].flatten
client.track(keywords,&block)
elsif options[:follow]
ids = [options[:follow]].flatten
client.follow(ids,&block)
end
end
#
# Collects tweets and stores them in a Tokyo Table DB.
#
# @param [String] db
# Path to the DB file.
#
# @see Twitter.watch
#
def Twitter.collect(db,options={})
db = Rufus::Tokyo::Table.new(db)
index = db.size
begin
Twitter.watch(options) do |status|
user = status.delete(:user)
puts "\n@#{user.screen_name}: #{status.text}"
db.transaction do
db["user#{user[:id]}"] = user
db["tweet#{index}"] = status
index += 1
end
end
rescue Interrupt
ensure
db.close
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment