Created
March 7, 2013 11:23
-
-
Save schatteleyn/5107403 to your computer and use it in GitHub Desktop.
Twit problem
This file contains hidden or 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 express = require('express'); | |
var mongo = require('mongodb'); | |
var fs = require('fs'); | |
var jade = require('jade'); | |
var Twit = require('twit'); | |
var io = require('socket.io').listen(1200); | |
var app = express(); | |
app.engine('html', jade.__express); | |
app.set('view engine', 'html'); | |
app.set('views', __dirname + '/views'); | |
app.set('database','tweets'); | |
app.configure(function(){ | |
app.set('port', '1339'); | |
}); | |
GLOBAL.__env = app.get('env'); | |
app.use("/public", express.static(__dirname + '/public')); | |
var client = new mongo.Db(app.get('database'), new mongo.Server('127.0.0.1', 27017, {auto_reconnect: true, 'poolSize': 10})); | |
client.open(function (error, client) { | |
if(error) { | |
console.log("MongoDB failed"); | |
console.log(error); | |
return; | |
} | |
console.log("MongoDB client opened"); | |
}); | |
var T = new Twit({ | |
consumer_key: '...', | |
consumer_secret: '...', | |
access_token: '...', | |
access_token_secret: '...' | |
}) | |
require("./TweetApi.js")(app, client); | |
fs.readdirSync("./jobs").forEach(function(file) { | |
require("./jobs/" + file)(client, T, io); | |
}); |
This file contains hidden or 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
// ./jobs//jobs/TweetsCrawl.js | |
var CronJob = require('cron').CronJob; | |
module.exports = function(client, T, io){ | |
var personalSpace = {}; | |
personalSpace.tweetCount = null; | |
personalSpace.twitterStream = null; | |
personalSpace.cronjobCallbackFactory = function cronjobCallbackFactory() { | |
'use strict'; | |
var daSpace = this; // Often also written as "var that = this;" | |
return function cronjobCallback() { | |
// If the function has been called already, time to save the statistic | |
if (daSpace.tweetCount !== null) { | |
/*client.collection("TweetsNumber").insert({ | |
Date: new Date(), | |
CrawledTweets: daSpace.tweetCount, | |
Channel: "someChannel" | |
});*/ | |
console.log(daSpace.tweetCount + " tweets saved in DB"); | |
// Be polite and tell twitter you stop the stream for now. ;-) | |
daSpace.twitterStream.stop(); | |
} else { | |
// The function has been called the first time, so we initalize the | |
// counter | |
daSpace.tweetCount = 0; | |
} | |
// start the stream | |
daSpace.twitterStream = T.stream( | |
'statuses/filter', | |
{track: '#MentionYourFavoriteFollower'} | |
); | |
// bind the 'tweet' event | |
daSpace.twitterStream.on('tweet', function onTweet(tweet) { | |
console.log(tweet); | |
// this function has also access to the daSpace [=== personalSpace] | |
// object | |
daSpace.tweetCount += 1; | |
}); | |
daSpace.twitterStream.on('disconnect', function (disconnectMessage) { | |
console.log('DISCONNECTED'); | |
console.log(disconnectMessage); | |
}) | |
.on('connect', function (request) { | |
console.log('CONNECTED'); | |
console.log(request); | |
}) | |
.on('error', function (err) { | |
console.log('ERROR'); | |
console.log(err); | |
}); | |
}; | |
}; | |
new CronJob('*/10 * * * * *', personalSpace.cronjobCallbackFactory(), function cronStops() {console.log('cronJob has stopped!')}, null, true, "Europe/Paris"); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment