Skip to content

Commit

Permalink
Collect user timeline status and store them in mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmoreno committed Sep 4, 2012
1 parent 8336ee7 commit 7d6e7ba
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source "http://rubygems.org"

gem "tweetstream"
gem "mongo"
gem "bson_ext"

group :development do
gem "awesome_print"
end
31 changes: 31 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
GEM
remote: http://rubygems.org/
specs:
awesome_print (1.0.2)
bson (1.6.4)
bson_ext (1.6.4)
bson (~> 1.6.4)
daemons (1.1.8)
eventmachine (0.12.10)
http_parser.rb (0.5.3)
mongo (1.6.2)
bson (~> 1.6.2)
multi_json (1.3.6)
simple_oauth (0.1.5)
tweetstream (1.1.4)
daemons (~> 1.1)
multi_json (>= 1.0)
twitter-stream (= 0.1.14)
twitter-stream (0.1.14)
eventmachine (>= 0.12.8)
http_parser.rb (~> 0.5.1)
simple_oauth (~> 0.1.4)

PLATFORMS
ruby

DEPENDENCIES
awesome_print
bson_ext
mongo
tweetstream
35 changes: 35 additions & 0 deletions collect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Collects user tweets and saves them to a mongodb

require "bundler"
require File.dirname(__FILE__) + "/tweetminer"

Bundler.require

# We use the TweetStream gem to access Twitter's Streaming API.
# https://github.com/intridea/tweetstream

TweetStream.configure do |config|
settings = YAML.load_file File.dirname(__FILE__) + '/twitter.yml'

config.consumer_key = settings['consumer_key']
config.consumer_secret = settings['consumer_secret']
config.oauth_token = settings['oauth_token']
config.oauth_token_secret = settings['oauth_token_secret']
end

settings = YAML.load_file File.dirname(__FILE__) + '/mongo.yml'
miner = TweetMiner.new(settings)

stream = TweetStream::Client.new

stream.on_error do |msg|
puts msg
end

stream.on_timeline_status do |status|
miner.insert_status status
print "."
end

# Do not forget this to trigger the collection of tweets
stream.userstream
3 changes: 3 additions & 0 deletions mongo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
host: localhost
port: 27017
database: twitter
33 changes: 33 additions & 0 deletions tweetminer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "mongo"

class TweetMiner
attr_writer :db_connector
attr_reader :options

def initialize(options)
@options = options
end

def db
@db ||= connect_to_db
end

def insert_status(status)
statuses.insert status
end

def statuses
@statuses ||= db["statuses"]
end

private

def connect_to_db
db_connector.call(options["host"], options["port"]).db(options["database"])
end

def db_connector
@db_connector ||= Mongo::Connection.public_method :new
end

end
9 changes: 9 additions & 0 deletions twitter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Vist http://dev.twitter.com to register your application
# and get oauth settings.

consumer_key: 'KEY'
consumer_secret: 'SECRET'

oauth_token: 'TOKEN'
oauth_token_secret: 'TOKEN SECRET'

1 comment on commit 7d6e7ba

@shamshul2007
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which code we have to put in model to get the confirmation on webpage that it is streaming

Please sign in to comment.