Skip to content

Instantly share code, notes, and snippets.

@mneedham
Created March 21, 2010 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mneedham/339579 to your computer and use it in GitHub Desktop.
Save mneedham/339579 to your computer and use it in GitHub Desktop.
twitter-server.js
var sys = require('sys'), http = require('http'), twitter = require('./twitter'), couch = require("./node-couch");
Array.prototype.each = function(fn) {
for (var i = 0; i < this.length; i++) {
var item = this[i];
fn.call(null, item);
}
return this;
};
var couchDB = couch.CouchDB.db("twitter", "5984", "127.0.0.1");
var server = http.createServer(function (req, res) {
couchDB.view("application/sort-by-id", {
descending : true,
success : function(response) {
twitter.query("friends_timeline", {
username : "username",
password : "password",
since_id : response.rows[0].value.id,
count: 200,
callback : function(tweets) {
tweets.each(function(tweet) {
couchDB.saveDoc(tweet, {
success : function(doc) {
sys.log("Document " + doc._id + " successfully saved")
},
error : function() {
sys.log("Document failed to save");
}
});
});
}
});
},
error : function() {
sys.log("Failed to retrieve documents");
}
});
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('OK');
res.close();
});
server.listen(8001);
var ping = http.createClient(8001, "127.0.0.1");
setInterval(function() {
var request = ping.request("GET", "/");
request.close();
}, 60000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment