Skip to content

Instantly share code, notes, and snippets.

@meritt
Created April 9, 2012 11:19
Show Gist options
  • Save meritt/2342953 to your computer and use it in GitHub Desktop.
Save meritt/2342953 to your computer and use it in GitHub Desktop.
Twitter Streaming API on CoffeeScript
var socket = io.connect('http://127.0.0.1:8080');
socket.on('connect', function() {
socket.on('message', function(tweet) {
var tweet = JSON.parse(tweet), content = '';
content = '<div class="name">' + tweet.name + '</div>';
content += '<div class="text">' + tweet.text + '</div>';
$('#tweets').prepend(content);
});
});
window.onbeforeunload = function() {
socket.disconnect();
};
https = require 'https'
request = https.get
headers: Authorization: 'Basic ' + encode argv.name + ':' + argv.password
host: 'stream.twitter.com'
port: 443
path: "/1.1/statuses/filter.json?include_entities=true&track=" + encodeURIComponent argv.hash
method: 'GET'
request.on 'response', (response) ->
response.setEncoding 'utf8'
body = ''
response.on 'data', (chunk) ->
body += chunk
newline = body.indexOf "\r"
if newline isnt -1
message = body.slice 0, newline
tweet = JSON.parse message
console.log tweet
body = body.slice newline + 1
@meritt
Copy link
Author

meritt commented Apr 9, 2012

Подробнее о том как сделать онлайн Twitter трансляцию с помощью Node.js, Twitter Streaming API и Websockets: blog.simonenko.su

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment