Created
April 9, 2012 11:19
-
-
Save meritt/2342953 to your computer and use it in GitHub Desktop.
Twitter Streaming API on CoffeeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Подробнее о том как сделать онлайн Twitter трансляцию с помощью Node.js, Twitter Streaming API и Websockets: blog.simonenko.su