public
Last active — forked from mloughran/README.md

  • Download Gist
.gitignore
1
twitter.json
Gemfile
Ruby
1 2 3 4 5 6
source 'http://rubygems.org'
 
gem 'eventmachine', '0.12.10'
gem 'em-websocket', '0.1.4'
gem 'em-http-request', '0.2.13'
gem 'json', '1.4.6'
README.md
Markdown

Getting started

First add your twitter username and password, then:

bundle install
ruby server [optional term]
open websocket.html

You should see some tweets appear. If not take a look at the javascript console.

language_detector.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
require 'em-http'
require 'json'
 
class LanguageDetector
URL = "http://www.google.com/uds/GlangDetect"
include EM::Deferrable
def initialize(text)
request = EM::HttpRequest.new(URL).get({
:query => {:v => "1.0", :q => text}
})
# This is called if the request completes successfully (whatever the code)
request.callback {
begin
if request.response_header.status == 200
info = JSON.parse(request.response)["responseData"]
if info['isReliable']
self.succeed(info['language'])
else
self.fail("Language could not be reliably determined")
end
else
self.fail("Call to fetch language failed")
end
rescue
puts request.response
self.fail
end
}
# This is called if the request totally failed
request.errback {
self.fail("Error making API call")
}
end
end
server.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
require 'rubygems'
require 'eventmachine'
require 'em-websocket'
require 'twitter_stream'
require 'language_detector'
 
 
unless File.exist?(auth_file = File.dirname(__FILE__) + "/twitter.json")
puts 'Getting started guide.'
puts 'Create twitter.json file:'
puts ' cp twitter.json.example twitter.json'
exit
end
 
auth = JSON.parse(File.read(auth_file))
TWITTER_USERNAME = auth['username']
TWITTER_PASSWORD = auth['password']
TWITTER_TERM = ARGV[0] || 'twitter'
 
EM.run {
websocket_connections = []
EM::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen {
puts "Websocket connection opened"
websocket_connections << ws
}
ws.onclose {
puts "Websocket connection closed"
websocket_connections.delete(ws)
}
end
stream = TwitterStream.new(TWITTER_USERNAME, TWITTER_PASSWORD, TWITTER_TERM)
stream.ontweet { |tweet|
LanguageDetector.new(tweet).callback { |lang|
puts "New tweet in #{lang}: #{tweet}"
websocket_connections.each do |socket|
socket.send(JSON.generate({
:lang => lang,
:tweet => tweet
}))
end
}
}
}
twitter.json.example
1
{"username":"USERNAME","password":"PASSWORD"}
twitter_stream.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
require 'em-http'
require 'json'
 
class TwitterStream
URL = 'http://stream.twitter.com/1/statuses/filter.json'
def initialize(username, password, term)
@username, @password = username, password
@term = term
@callbacks = []
@buffer = ""
listen
end
def ontweet(&block)
@callbacks << block
end
private
def listen
http = EventMachine::HttpRequest.new(URL).post({
:head => { 'Authorization' => [@username, @password] },
:query => { "track" => @term }
})
http.stream do |chunk|
@buffer += chunk
process_buffer
end
end
def process_buffer
while line = @buffer.slice!(/.+\r\n/)
tweet = JSON.parse(line)
 
@callbacks.each { |c| c.call(tweet['text']) }
end
end
end
websocket.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<!DOCTYPE html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
var container = $('.container');
var addTweetToPage = function(language, tweet) {
var id = 'tweet' + language;
if ($('#'+id).length == 0) {
container.append('<div id="'+id+'"><h1>'+language+'</h1></div>')
}
$('#'+id).append('<div class="tweet">'+tweet+'</div>');
};
var socket = new WebSocket('ws://0.0.0.0:8080/');
 
socket.onopen = function() {
console.log("Socket opened");
};
 
socket.onmessage = function(message) {
var info = JSON.parse(message.data);
console.log(info.lang, info.tweet);
addTweetToPage(info.lang, info.tweet);
};
 
socket.onclose = function() {
console.log("Socket closed");
};
})
</script>
</head>
 
<body>
<div class="container"></div>
</body>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.