Skip to content

Instantly share code, notes, and snippets.

@drnic
Forked from mloughran/README.md
Created October 1, 2010 22:18
Show Gist options
  • Save drnic/606965 to your computer and use it in GitHub Desktop.
Save drnic/606965 to your computer and use it in GitHub Desktop.
twitter.json

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.

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'
GEM
remote: http://rubygems.org/
specs:
addressable (2.2.1)
em-http-request (0.2.13)
addressable (>= 2.0.0)
eventmachine (>= 0.12.9)
em-websocket (0.1.4)
addressable (>= 2.1.1)
eventmachine (>= 0.12.9)
eventmachine (0.12.10)
json (1.4.6)
PLATFORMS
ruby
DEPENDENCIES
em-http-request (= 0.2.13)
em-websocket (= 0.1.4)
eventmachine (= 0.12.10)
json (= 1.4.6)
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
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
}
}
}
{"username":"USERNAME","password":"PASSWORD"}
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
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment