Created
February 28, 2013 20:19
-
-
Save rashidkpc/5059781 to your computer and use it in GitHub Desktop.
Indexed throttled tweets into elasticsearch and resets the id counter after a definable number of events
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
require 'rubygems' | |
require 'tire' | |
require 'tweetstream' | |
bulk_limit = 50 | |
throttle = 10 | |
total_limit = 4320000 | |
TweetStream.configure do |config| | |
config.consumer_key = 'foo' | |
config.consumer_secret = 'bar' | |
config.oauth_token = 'baz' | |
config.oauth_token_secret = 'bork' | |
config.auth_method = :oauth | |
end | |
Tire.index 'twitter' do | |
delete | |
create :mappings => { | |
:tweet => { | |
:properties => { | |
:id => { :type => 'integer', :index => 'not_analyzed' }, | |
:user => { | |
:properties => { | |
:name => { :type => 'string', :index => 'not_analyzed' }, | |
:lang => { :type => 'string', :index => 'not_analyzed' }, | |
} | |
}, | |
:place => { | |
:properties => { | |
:name => { :type => 'string', :index => 'not_analyzed' }, | |
:type => { :type => 'string', :index => 'not_analyzed' }, | |
:country_name => { :type => 'string', :index => 'not_analyzed' }, | |
:country_code => { :type => 'string', :index => 'not_analyzed' }, | |
} | |
} | |
} | |
} | |
} | |
end | |
i=0 | |
tweets = Array.new | |
TweetStream::Client.new.on_error { |error| | |
puts error | |
}.sample do |status| | |
# The status object is a special Hash with | |
# method access to its keys. | |
# puts "#{i} #{status.text}" | |
i += 1 | |
if tweets.length < throttle | |
tweet = { | |
:id => i, | |
:type => 'tweet', | |
:created_at => status.created_at.iso8601, | |
:text => status.text, | |
:user => { | |
:name => status.user.screen_name, | |
:created_at => status.user.created_at.iso8601, | |
:lang => status.user.lang | |
} | |
} | |
if !status.place.nil? | |
tweet[:place] = { | |
:name => status.place.full_name, | |
:type => status.place.place_type, | |
:country_code => status.place.country_code, | |
:country_name => status.place.country | |
} | |
end | |
tweets.push(tweet) | |
end | |
if i % bulk_limit == 0 | |
Tire.index 'twitter' do | |
import tweets | |
tweets.clear | |
end | |
end | |
if i > total_limit | |
i = 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment