Skip to content

Instantly share code, notes, and snippets.

@dervalp
Created November 25, 2012 12:46
Show Gist options
  • Select an option

  • Save dervalp/4143353 to your computer and use it in GitHub Desktop.

Select an option

Save dervalp/4143353 to your computer and use it in GitHub Desktop.
Find Word
module.exports = function(){
var mongoose = require('mongoose')
, _ = require("underscore")
, twitter = require('ntwitter')
, User = require('./model')(mongoose).user
, Action = require('./model')(mongoose).action;
mongoose.connect('mongodb://mongohq.com:10001/scocialcrawler');
var twit = new twitter({
consumer_key: 'xxx',
consumer_secret: 'xxx',
access_token_key: 'xx',
access_token_secret: 'xx'
});
var extractTweet = function(tweet) {
if(tweet.text.indexOf("#test") > -1) {
var value = tweet.text.replace("#test","");
return value;
//from there do whatever you want with the text or picture
//as it would be 3rd party call the AJAX call.
}
};
var findAndExtractTweet = function(user_id, action, cb) {
twit
.verifyCredentials(function (err, data) {})
.getUserTimeline({ user_id : user_id /*, count: "2"*/}, function (err, tweets) {
/*extract the first tweet id*/
console.log(_.first(tweets).id);
var content = _.compact(_.map(tweets, function(){
if(tweet.text.indexOf("#"+action.word) > -1) {
var value = tweet.text.replace("#"+action.word,"");
return value;
//from there do whatever you want with the text or picture
//as it would be 3rd party call the AJAX call.
}
}));
console.log(content);
/*
save the content using mongoose
save the first id to use since_id
*/
cb();
});
};
var andWhileIGotUserDo = function(action, callback) {
//we will get the user
//while user has page - goes ont
var pageNumber = 1;
var resultsPerPage = 50;
while(pageNumber != 0) {
var skipFrom = (pageNumber * resultsPerPage) - resultsPerPage;
var query = user.model.find({}).sort('datecreated', 1).skip(skipFrom).limit(resultsPerPage);
query.exec(function(err, res) {
if (err) {
throw "err"
} else {
if(res.length < 50) {
pageNumber = 0;
}
callback(res, action);
}
});
}
};
var whileIGotApp = function(extractUser, extractAppropriateAction) {
console.log("Fetch Action");
Action.model.find().exec(function(err, actions){
console.log("I get actions");
_.each(actions, function(action){
console.log("for each action");
extractUser(action, extractAppropriateAction);
})
})
};
var extractFromTwitter = function(users, action){
_.each(users, function(user_id) {
findAndExtractTweet(user_id, action, function(){
//do something
});
});
};
return {
start: function(cb) {
console.log("starting");
whileIGotApp(andWhileIGotUserDo, extractFromTwitter);
if(cb) {cb() };
}
};
}();
@alex-seville
Copy link
Copy Markdown

I like the idea.

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