Skip to content

Instantly share code, notes, and snippets.

@vanrysss
Created January 13, 2015 07:07
Show Gist options
  • Save vanrysss/b2fc5f591cc73f890d7a to your computer and use it in GitHub Desktop.
Save vanrysss/b2fc5f591cc73f890d7a to your computer and use it in GitHub Desktop.
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
Twit = require('twit'),
io = require('socket.io').listen(server);
var _ = require('underscore');
//server.listen(process.env.PORT || 8000);
server.listen(8000);
// routing
// Tell node to load node-twitter-stream.html when the browser requests /
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
// Tell node to serve the CSS file when requested
app.get('/style.css', function(req, res) {
res.sendFile(__dirname + '/style.css');
});
//portland city limits for twitter
var portland = ['-122.7729357', '45.428982', '-122.4811972', '45.5799702'];
var tweetQueue = [];
var Tw = new Twit({
consumer_key: "",
consumer_secret: "",
access_token: "",
access_token_secret: ""
});
var stream = Tw.stream('statuses/filter', {
locations: portland,
language: 'en'
});
io.on('connection', function(socket) {
setInterval(rateLimitedEmit, 10000);
console.log('new connection');
socket.on('disconnect', function() {
console.log('user disconnected');
});
});
// When a Tweet is recieved:
stream.on('tweet', function(tweet) {
if (tweet.geo) {
if (parseFloat(tweet.geo.coordinates[0]) < portland[3] && parseFloat(tweet.geo.coordinates[0]) > portland[1]) {
if (parseFloat(tweet.geo.coordinates[1]) < portland[2] && parseFloat(tweet.geo.coordinates[1]) > portland[0]) {
// Makes a link the Tweet clickable
tweetQueue.push(tweet);
console.log("tweet added to Q: " + tweetQueue.length);
}
}
}
});
var rateLimitedEmit = function() {
if(tweetQueue.length >1) {
var turl = tweetQueue[0].text.match(/(http|https|ftp):\/\/[^\s]*/i);
if (turl !== null) {
turl = tweetQueue[0].text.replace(turl[0], '<a href="' + turl[0] + '" target="new">' + turl[0] + '</a>');
} else {
turl = tweetQueue[0].text;
}
console.log("emitting tweet");
io.emit('stream', turl, tweetQueue[0].user.screen_name);
tweetQueue.shift();
}
};
@vanrysss
Copy link
Author

tweet added to Q: 2
emitting tweet
tweet added to Q: 2
tweet added to Q: 3
tweet added to Q: 4
emitting tweet
tweet added to Q: 4
emitting tweet
emitting tweet
emitting tweet
tweet added to Q: 2
emitting tweet
tweet added to Q: 2
tweet added to Q: 3
emitting tweet
emitting tweet
tweet added to Q: 2
tweet added to Q: 3
tweet added to Q: 4
emitting tweet
emitting tweet
emitting tweet
tweet added to Q: 2
emitting tweet
tweet added to Q: 2

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