Play with the Twitter Stream API

10 Nov 2011

Concepts

Products

Methods

Streaming API vs REST API

Pros:

Cons:

Example

require 'eventmachine'
require 'em-http'
require 'json'

usage = "#{$0} <username> <password> <keyword>"
abort usage unless username = ARGV.shift
abort usage unless password = ARGV.shift

URL = 'https://stream.twitter.com/1/statuses/sample.json'

EM.run do

    http = EventMachine::HttpRequest.new(URL).get({
        :head => { 'Authorization' => [ username, password ] }
    })

    buffer = ''
    http.stream do |chunk|
        buffer += chunk
        while line = buffer.slice!(/.+\r\n/)
            tweet = JSON.parse(line)
            puts tweet['text']
        end
    end
end
class TwitterStream
    URL = 'https://stream.twitter.com/1/statuses/filter.json'

    def initialize(username, password, keyword)
        @callbacks = []

        buffer = ''
        EventMachine::HttpRequest.new(URL).post({
            :head => { 'Authorization' => [ username, password ]},
            :query => { 'track' => keyword }
        }).stream do |chunk|
            buffer += chunk
            while line = buffer.slice!(/.+\r\n/)
                tweet = JSON.parse(line)
                @callbacks.each { |callback| callback.call(tweet) }
            end
        end
    end

    def ontweet(&block)
        @callbacks << block
    end

end
require 'eventmachine'
require 'em-http'
require 'em-websocket'
require 'json'

usage = "#{$0} <username> <password> <keyword>"
abort usage unless username = ARGV.shift
abort usage unless password = ARGV.shift
abort usage unless keyword  = ARGV.shift

EM.run do

    websocket_connections = []
    EM::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
        ws.onopen { websocket_connections << ws }
        ws.onclose { websocket_connections.delete(ws) }
    end

    stream = TwitterStream.new(username, password, keyword)
    stream.ontweet do |tweet|
        websocket_connections.each do |socket|
            socket.send(JSON.generate(tweet))
        end
    end
end
var map = new google.maps.Map(document.getElementById("map_canvas"), {
    zoom: 3,
    center: new google.maps.LatLng(25, 0),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var addTweet = function(tweet) {
    if (tweet.geo && tweet.geo.coordinates) {
        var latitude = tweet.geo.coordinates[0];
        var longitude = tweet.geo.coordinates[1];
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(latitude, longitude),
            map: map,
            title: tweet.text
        });
    }
}

var socket = new WebSocket('ws://0.0.0.0:8080/');

socket.onmessage = function(message) {
    var tweet = JSON.parse(message.data);
    addTweet(tweet);
}

More infos