Created
February 29, 2012 07:05
-
-
Save beatinaniwa/1938711 to your computer and use it in GitHub Desktop.
Store tweets in MongoDB using twitter streaming api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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