Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored to remove tweets.js and add /api/streamTweets endpoint

  • Loading branch information...
commit 82de19590657da5466c93cb7cf84e33f625412a4 1 parent 23c2e8c
Brendan Nee authored
Showing with 45 additions and 47 deletions.
  1. +1 −4 boot.js
  2. +8 −1 config.js
  3. +36 −4 routes/index.js
  4. +0 −38 tweets.js
5 boot.js
View
@@ -1,6 +1,5 @@
var config = require('./config')
- , routes = require('./routes')
- , tweets = require('./tweets');
+ , routes = require('./routes');
module.exports = function boot(app){
@@ -8,8 +7,6 @@ module.exports = function boot(app){
routes(app);
- tweets(app);
-
return app;
}
9 config.js
View
@@ -10,7 +10,8 @@ var express = require('express')
, host = process.env['MONGO_NODE_HOST'] || options.mongo_node_host
, port = process.env['MONGO_NODE_PORT'] || options.mongo_node_port
, db = (port) ? mongoose.connect(host, dbName, port) : mongoose.connect(host, dbName);
-
+
+
module.exports = function(app){
app.configure(function(){
this.use(express.cookieParser())
@@ -21,6 +22,12 @@ module.exports = function(app){
.use(express.static(__dirname + '/public'))
.set('db', db)
.set('options', options)
+ .set('twit', new twitter({
+ consumer_key: options.consumer_key,
+ consumer_secret: options.consumer_secret,
+ access_token_key: options.access_token_key,
+ access_token_secret: options.access_token_secret
+ }));
});
// Dev
40 routes/index.js
View
@@ -7,9 +7,12 @@ require('../models/Probability');
var async = require('async')
- , _ = require('underscore');
+ , _ = require('underscore')
+ twitter = require('ntwitter');
module.exports = function routes(app){
+
+ /* Socket IO */
var io = require('socket.io').listen(app)
, Tweet = app.set('db').model('Tweet')
@@ -17,7 +20,9 @@ module.exports = function routes(app){
, NotEnglish = app.set('db').model('NotEnglish')
, Interesting = app.set('db').model('Interesting')
, NotInteresting = app.set('db').model('NotInteresting')
- , Probability = app.set('db').model('Probability');
+ , Probability = app.set('db').model('Probability')
+ , options = app.set('options')
+ , twit = app.set('twit');
io.sockets.on('connection', function (socket) {
getTweetToClassify();
@@ -51,9 +56,11 @@ module.exports = function routes(app){
}
});
}
-
});
+
+ /* Routes */
+
app.get('/api/getTweets', function(req, res){
Tweet.find()
.limit(100)
@@ -90,6 +97,27 @@ module.exports = function routes(app){
});
+ app.get('/api/streamTweets', function(req, res){
+
+ twit.stream('statuses/sample', function(stream) {
+ console.log('Getting Tweet stream for 60 seconds');
+ var tweetCount = 0;
+ stream.on('data', function (data) {
+ if(data.text.charAt(0) != '@'){
+ var tweet = new Tweet(data);
+ tweet.save();
+ tweetCount++;
+ if(tweetCount % 50 === 0){
+ console.log(tweetCount + ' tweets collected');
+ }
+ }
+ });
+
+ //disconnect after 1 minute of tweets
+ setTimeout(stream.destroy, 60000);
+ });
+ });
+
app.get('/process', function(req, res){
async.series([
@@ -146,7 +174,11 @@ module.exports = function routes(app){
function splitWords(tweet){
//remove all username
- tweet = tweet.replace(/@([A-Za-z0-9_]+)/g,"")
+ tweet = tweet.replace(/@([A-Za-z0-9_]+)/g,"");
+
+ //remove all hashtags
+ tweet = tweet.replace(/#([A-Za-z0-9_]+)/g,"");
+
//remove all URLs
tweet = tweet.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g,"");
38 tweets.js
View
@@ -1,38 +0,0 @@
-
-var twitter = require('ntwitter');
-
-require('./models/Tweet');
-
-module.exports = function(app){
-
- var options = app.set('options')
- , Tweet = app.set('db').model('Tweet');
-
- var twit = new twitter({
- consumer_key: options.consumer_key,
- consumer_secret: options.consumer_secret,
- access_token_key: options.access_token_key,
- access_token_secret: options.access_token_secret
- });
-
- twit.stream('statuses/sample', function(stream) {
-
- console.log('Getting Tweet stream for 60 seconds');
- var tweetCount = 0;
- stream.on('data', function (data) {
- if(data.text.charAt(0) != '@'){
- var tweet = new Tweet(data);
- tweet.save();
-
- tweetCount++;
- if(tweetCount % 50 === 0){
- console.log(tweetCount + ' tweets collected');
- }
- }
- });
-
- //disconnect after 1 minute of tweets
- setTimeout(stream.destroy, 60000);
- });
-
-}
Please sign in to comment.
Something went wrong with that request. Please try again.