Skip to content

Instantly share code, notes, and snippets.

@beatinaniwa
Created February 29, 2012 07:05
Show Gist options
  • Save beatinaniwa/1938711 to your computer and use it in GitHub Desktop.
Save beatinaniwa/1938711 to your computer and use it in GitHub Desktop.
Store tweets in MongoDB using twitter streaming api
#encoding: utf-8
require 'rubygems'
require 'tweetstream'
require 'mongo'
require 'eventmachine'
db = Mongo::Connection.from_uri('mongodb://username:password@ds031087.mongolab.com:31087/dbname').db('dbname')
@items = db.collection('items')
TweetStream.configure do |config|
config.consumer_key = 'your_consumer_key'
config.consumer_secret = 'your_consumer_secret'
config.oauth_token = 'your_access_token'
config.oauth_token_secret = 'your_access_token_secret'
config.auth_method = :oauth
config.parser = :json_pure
end
EM.run do
client = TweetStream::Client.new
def write_to_mongolab(status)
EM.defer do
item = {
:id_str => status.id_str,
:screen_name => status.user.screen_name,
:profile_image_url => status.user.profile_image_url,
:text => status.text,
:created_at => status.created_at
}
@items.insert(item)
end
end
client.track('gumroad') do |status|
write_to_mongolab(status)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment