Twitter Site Stream using EventMachine
January 20th, 2011
I’ve spend some time to try to use Twitter Site Streaming API with event machine, so here is just a small snipet of code on how to do it. In fact it’s pretty simple
Just go to your app page on twitter ( http://www.twitter.com/apps ) and go to your application. Get the access token here. You will find also a button “get my access token” where you can get the oauth access token and access token secret.
With these, you will be able to sign the request
require 'rubygems'
require 'eventmachine'
require 'em-http'
require 'json'
require 'oauth'
require 'oauth/client/em_http'
# Edit in your details.
CONSUMER_KEY = "<put your consumer secret key here>"
CONSUMER_SECRET = "<put your consumer key here>"
ACCESS_TOKEN = "<put your access token here>"
ACCESS_TOKEN_SECRET = "<put your access token secrethere>"
def twitter_oauth_consumer
@twitter_oauth_consumer ||= OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
end
def twitter_oauth_access_token
@twitter_oauth_access_token ||= OAuth::AccessToken.new(twitter_oauth_consumer, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
end
EventMachine.run do
# now, let's subscribe to twitter site stream
# check informaiton on twitter site
# here we are followig to user that have signed to our app...
toFollow=[17590452,2071231]
http = EventMachine::HttpRequest.new('https://betastream.twitter.com/2b/site.json'
).post(:body=>{"follow"=>toFollow.join(",")},
:head => {"Content-Type" => "application/x-www-form-urlencoded"},
:timeout => -1) do |client|
twitter_oauth_consumer.sign!(client, twitter_oauth_access_token)
end
buffer = ""
http.stream do |chunk|
buffer += chunk
while line = buffer.slice!(/.+\r?\n/)
puts "handling a new event:"+line
end
end
http.errback { puts "oops" }
http.disconnect { puts "oops, dropped connection?" }
end
For this, I use the eventmachine plugins for ruby, as well as the oauth and em-http plugins.
Please note that you must you https with twitter in order for this to work.
Entry Filed under: Uncategorized
3 Comments Add your own
1. Tweets that mention TomSo&hellip | January 21st, 2011 at 11:56 am
[...] This post was mentioned on Twitter by Thomas Landspurg, idigiwiz. idigiwiz said: RT @tomsoft: http://blog.landspurg.net/twitter-site-stream-using-eventmachine/ #eventmachine [...]
2. Tomash | September 29th, 2011 at 9:55 am
Why not just use gem “twitter-stream”? Does it have some hideous bugs?
(becaue I could have stumbled upon one, probably :/ )
3. TomSoft | September 29th, 2011 at 10:24 am
Good question, I’ve tried to used it, but fail but not exactly remember why! I think the authentifciation was a problem, and at the end I had to do it by myself
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed