Skip to content

Instantly share code, notes, and snippets.

@BenChirlinn
Created August 13, 2012 18:01
Show Gist options
  • Save BenChirlinn/3342775 to your computer and use it in GitHub Desktop.
Save BenChirlinn/3342775 to your computer and use it in GitHub Desktop.
Node Cron with HTTP chained requests and request example
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var cronJob = require('cron').CronJob;
var UpdateSocial = require('./socialupdate.js').UpdateSocial;
var mydb = new Db('node_mongo_social', new Server('localhost', 27017, {auto_reconnect: true}, {}));
mydb.open(function(){});
// Init
console.log("Starting cron");
var UpdateSocial = new UpdateSocial(mydb);
var job = new cronJob({
cronTime:'0 0 */3 * * *',
onTick: function() {
console.log("Starting updates…");
// Daisychained social updates
UpdateSocial.updateVoice(
function(mess, addthis) {
UpdateSocial.addResults(
function(mess, cb){
if (cb) {
cb();
} else {
UpdateSocial.updateInstagram(
function(mess, addthis) {
UpdateSocial.addResults(
function(mess, cb){
if (cb) {
cb();
} else {
UpdateSocial.updateTwitter(
function(mess, addthis) {
UpdateSocial.addResults(
function(mess, cb){
if (cb) {
cb();
} else {}
},
addthis
);
}
);
}
},
addthis
);
});
}
},
addthis
);
}
);
console.log("Cron job ran at " + new Date());
},
start: true
});
UpdateSocial.prototype.updateTwitter = function(callback) {
this.getCollection(function(error, social_collection) {
if( error ) callback(error)
else {
// Should check most recent post of this type here to ensure none are missed
var twitpath = '/1/statuses/user_timeline.json?screen_name=themechanism&include_entities=true&exclude_replies=true';
var queryResponse = "";
var options = {
host: 'api.twitter.com',
path: twitpath
};
http.get(options, function(response) {
//console.log('Tweets recieved: ' + response.statusCode);
response.on('data', function(chunk) {
queryResponse += chunk.toString();
});
response.on('end', function (chunk) {
var tweets = JSON.parse(queryResponse);
var pros = new Array();
for (t in tweets) {
var tweet = tweets[t];
//console.log('Tweet processing…' + tweet.id);
var tweetPros = {
title: tweet.user.screen_name,
body: tweet.text,
link: 'https://twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id_str,
api_type: 'Twitter',
api_id: tweet.id_str,
api_date: new Date(tweet.created_at),
likes: tweet.retweet_count.toString(),
promoted: false,
author: {"name": tweet.user.name, "slug": tweet.user.screen_name, "img": tweet.user.profile_image_url},
created_at: new Date()
};
if (tweet.entities.media && tweet.entities.media[0]) {
tweetPros.img = tweet.entities.media[0].media_url;
}
if (tweet.entities.urls && tweet.entities.urls[0]) {
tweetPros.extlinks = [];
for (t in tweet.entities.urls) {
tweetPros.extlinks.push(tweet.entities.urls[t].url);
}
}
if (tweet.geo && tweet.geo.type == "Point") {
tweetPros.geolat = tweet.geo.coordinates[0];
tweetPros.geolon = tweet.geo.coordinates[1];
}
pros.push(tweetPros);
}
callback("Adding tweets", pros);
});
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment