Skip to content

Instantly share code, notes, and snippets.

@moderation
Created January 29, 2012 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save moderation/1700651 to your computer and use it in GitHub Desktop.
Save moderation/1700651 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var util = require('util'),
http = require('http'),
events = require('events'),
oauth = require('oauth').OAuth,
fs = require("fs"),
crypto = require("crypto"),
ws = require('websocket-server');
// Authentication Headers for Twitter
var consumer_key = 'INSERT KEY',
consumer_secret = 'INSERT KEY',
access_token = 'INSERT KEY',
access_token_secret = 'INSERT KEY';
var oa = new oauth("https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token", consumer_key, consumer_secret, "1.0A", "INSERT CALLBACK URL", "HMAC-SHA1");
var req = oa.get("https://userstream.twitter.com/2/user.json?replies=all", access_token, access_token_secret),
stream = new events.EventEmitter(),
buffer = '',
end = '\r\n',
friends = '',
first = true;
var server = ws.createServer({
debug: false,
version: "auto"
});
stream.addListener("data", function (chunk) {
"use strict";
var blob; // util.debug(chunk);
buffer += chunk;
if (buffer.indexOf(end) !== -1) {
while (buffer.indexOf(end) !== -1) {
blob = buffer.slice(0, buffer.indexOf(end));
buffer = buffer.slice(buffer.indexOf(end) + end.length);
if (blob.length > 0) {
stream.emit('line', blob);
}
}
}
});
stream.addListener("line", function (blob) {
"use strict";
var json;
try {
json = JSON.parse(blob);
} catch (e) {
stream.emit('json-error', e, blob);
}
if (json) {
if (first) {
friends = blob;
util.log('####FRIENDS: ' + friends);
first = false;
} else {
stream.emit("json", json); //util.debug(blob);
server.broadcast(blob); //return stream;
util.log('####WRITE: ' + blob);
}
}
});
req.addListener('response', function (resp) {
"use strict";
util.log("STATUS: " + resp.statusCode);
util.log("HEADER: " + JSON.stringify(resp.headers));
resp.setEncoding('utf8');
resp.addListener('data', function (chunk) { // util.debug(chunk);
stream.emit("data", chunk);
});
resp.addListener('end', function () {
stream.emit('end');
});
});
req.end();
// Handle WebSocket Requests
server.on("listening", function () {
"use strict";
util.log("Listening for connections.");
});
server.on("connection", function (conn) {
function log(data) {
util.log((+new Date()) + " \033[0;32m<" + conn.id + "> " + data.toString() + "\033[0m");
}
log("connected");
server.broadcast(friends); // send friends list to the client on connection
conn.on("message", function(message) {
if (message === 'close') {
log('[-] close requested');
conn.close();
} else {
log('[+] ', (new Buffer(message)).inspect());
log('[+] ', message);
server.broadcast(conn.id+": "+message);
}
});
conn.on("close", function() {
log('[*] close');
});
});
server.on("disconnect", function(conn){
"use strict";
util.log('[*] disconnected');
});
// Handle HTTP Requests:
server.on("request", function (req, res) {
"use strict";
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('This is, infact a websocket server, but we can do http!\n');
});
server.listen(8000);
<!DOCTYPE html>
<head>
<meta charset=utf-8>
<title>@hartley userstream</title>
<link rel=stylesheet href=twitter.css media=all>
<script src=//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js></script><!--<script src=//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js></script>-->
<script src=embedly.min.js></script>
<script src=twitter.js></script>
<link href="//fonts.googleapis.com/css?family=PT+Sans" rel=stylesheet type=text/css>
</head>
<body>
<section id=tweets> <h1>tweets</h1> </section>
<section id=conversations><h1>conversations</h1></section>
<section id=retweets> <h1>retweets</h1> </section>
<section id=favorites> <h1>favorites</h1> </section>
<section id=replies> <h1>replies</h1> </section>
</body>
body { font-family: 'PT Sans', 'Droid Sans', Helvetica, Sans-Serif; width: 2340px; }
section { display: block; width: 461px; float: left; border-left: 1px dotted #dcdcdc; }
a { text-decoration: none; }
img { border: 0; }
h1 { font-size: 20px; padding-left: 15px; color: #dcdcdc; margin-top: 2px !important; margin-bottom: 2px !important; letter-spacing: 5px; }
#tweets, #retweets, #favorites, #conversations, #replies { font-size: 14px; padding: 0; margin: 0; }
#tweets a, #retweets a, #favorites a, #conversations a, #replies a { color: #2277bb; }
#tweets .meta a, #retweets .meta a, #favorites .meta a, #conversations .meta a, #replies .meta a { color: #999; background: #f7f7f7; }
.content { clear: left; min-height: 48px; padding: 9px; line-height: 1.2; position: relative; }
.content:hover { background-color: #F7F7F7; }
.vcard { float: left; }
.hentry { margin-left: 58px; margin-right: 29px; }
.meta { color: #dcdcdc; font-size: 11px; margin: 3px 0 0; }
a.at { background: #fcfebf; }
a.ln { background: #ffe6ff; }
a.ha { background: #e6ffff; }
a.ex { background: #ccff00 !important; }
.conv { background-image: url(conversation.png); background-repeat: no-repeat; background-position: left center; padding-left: 20px; margin: auto 3px auto 5px; }
.retw { background-image: url(retweet.png); background-repeat: no-repeat; background-position: left center; padding-left: 20px; margin: auto 3px auto 5px; }
.veri { background-image: url(verified.png); background-repeat: no-repeat; background-position: left center; padding-left: 15px; margin: auto 3px auto 5px; }
.fvrt { background-image: url(star14.png); background-repeat: no-repeat; background-position: left center; padding-left: 20px; margin: auto 3px auto 5px; }
.prvt { background-image: url(lock.png); background-repeat: no-repeat; background-position: left center; padding-left: 15px; margin: auto 3px auto 5px; }
.geol { background-image: url(location.png); background-repeat: no-repeat; background-position: left center; padding-left: 15px; margin: auto 3px auto 5px; }
iframe, embed, object { width: 350px !important; margin-top: 10px; border: 0; padding: 0; margin: 0; }
.embed { font-size: 12px; }
img[src$="48"] { display: none; }
.embed, .embed > a > img, .embed > img { max-width: 350px; border: 0; }
.thumb { float: left; margin: 5px 5px 5px 0; }
.loc { margin-top: 2px; color: green; background: #f7f7f7; }
.fn { height: 48px; width: 48px; border: 0; }
.meta img { margin-top: 5px; }
.gmap { width: 350px; height: 160px; }
.embedly_foursquare_map, .embedly_foursquare_stat { display: none; }
<!DOCTYPE html>
<head>
<meta charset=utf-8>
<title>@hartley userstream</title>
<link rel=stylesheet href=twitter.css media=all>
<script src=//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js></script><!--<script src=//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js></script>-->
<script src=embedly.min.js></script>
<script src=twitter.js></script>
<link href="//fonts.googleapis.com/css?family=PT+Sans" rel=stylesheet type=text/css>
</head>
<body>
<section id=tweets> <h1>tweets</h1> </section>
<section id=conversations><h1>conversations</h1></section>
<section id=retweets> <h1>retweets</h1> </section>
<section id=favorites> <h1>favorites</h1> </section>
<section id=replies> <h1>replies</h1> </section>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment