Created
January 17, 2011 22:54
-
-
Save Sedward/783655 to your computer and use it in GitHub Desktop.
consumer streaming tweets and stuff them into indextank
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
class TwitterFetcher | |
def perform | |
api = IndexTank::Client.new(ENV['INDEXTANK_API_URL'] || index_tank_url) | |
index = api.indexes 'idx' | |
streaming_url = 'http://stream.twitter.com/1/statuses/sample.json' | |
EventMachine.run do | |
http = EventMachine::HttpRequest.new(streaming_url).get :head => { 'Authorization' => [ TWITTER_USERNAME, TWITTER_PASSWORD ] } | |
buffer = "" | |
http.stream do |chunk| | |
buffer += chunk | |
while line = buffer.slice!(/.+\r?\n/) | |
tweet = JSON.parse(line) | |
if tweet['text'] | |
index.document(tweet["id"].to_s).add({:text => tweet["text"]}) | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment