reorganized as a module
authorDominique Hazaël-Massieux <dom@w3.org>
Wed, 31 Aug 2011 15:37:29 +0200
changeset 183 ff93e10d4604
parent 182 659a8ba1c9f5
child 184 086d070c5721
reorganized as a module
node.js/twitter.js
--- a/node.js/twitter.js	Wed Aug 31 00:07:26 2011 +0200
+++ b/node.js/twitter.js	Wed Aug 31 15:37:29 2011 +0200
@@ -2,6 +2,7 @@
 var EventEmitter = require('events').EventEmitter;
 
 function loadTwitterListPage(owner, slug, cursor, users, callback) {
+    // Rate limited to 150 / hour, beware!
    var request = http.get({host: 'api.twitter.com', path:'/1/lists/members.json?slug=' + slug + '&owner_screen_name=' + owner + '&skip_status=1&cursor=' + cursor}, function (response) {
        response.setEncoding('utf8');
        var twitterDataJSON = "", twitterData;
@@ -21,35 +22,54 @@
    });
 }
 
-loadTwitterListPage('t', 'sf', -1, [], function (users) {
-    var twitterIds = [];
-    console.log(users);
-    for (u in users) {
-	twitterIds.push(users[u].id);
-    }
-    var stream = http.request({host: 'stream.twitter.com', path:'/1/statuses/filter.json', 'method': 'POST'}, function (res) {
-	res.setEncoding('utf8');
-	var incompleteChunk = "";
-	res.on('data', function (chunk) {
-	    incompleteChunk += chunk;
-	    incompleteChunk = incompleteChunk.trim();
-	    if (incompleteChunk) {
-		console.log(chunk);
-		try {
-		    var tweet = JSON.parse(incompleteChunk);
-		    console.log(tweet.text);
-		    incompleteChunk = "";
-		} catch (err) {
-		    console.log(err);
-		}
+exports.listTwitterIds = function(list_owner, list_slug, callback) {
+    loadTwitterListPage(
+	options.owner_name,
+	options.list_slug,
+	    -1,
+	[], 
+	function (users) {
+	    var twitterIds = [];
+	    console.log(users);
+	    for (u in users) {
+		twitterIds.push(users[u].id);
 	    }
-	});
-	res.on('end', function () {
-	    console.log("terminated with error " + res.statusCode);
+	    callback(twitterIds);
+    
 	});
-    });
+};
+
+exports.listenToTweets = function(twitter_ids, twitter_auth)  {
+    var emitter = new EventEmitter();
+    var stream = http.request(
+	{host: 'stream.twitter.com', path:'/1/statuses/filter.json', 'method': 'POST'}, 
+	function (res) {
+	    res.setEncoding('utf8');
+	    var incompleteChunk = "";
+	    res.on(
+		'data',
+		function (chunk) {
+		    incompleteChunk += chunk;
+		    incompleteChunk = incompleteChunk.trim();
+		    if (incompleteChunk) {
+			try {
+			    var tweet = JSON.parse(incompleteChunk);
+			    emitter.emit("tweet", tweet);
+			    incompleteChunk = "";
+			} catch (err) {
+			    console.log(err);
+			}
+		    }
+		});
+	    res.on(
+		'end',
+		function () {
+		    console.log("Twitter stream terminated with error " + res.statusCode);
+		});
+	}
+    );
     stream.setHeader("Content-Type", "application/x-www-form-urlencoded");
-    stream.setHeader("Authorization", 'Basic ' + new Buffer('dontcallmedom' + ':' + '@@@').toString('base64'));
-    stream.write("follow=" + twitterIds.join(","));
+    stream.setHeader("Authorization", 'Basic ' + new Buffer(options.twitter_auth.name + ':' + options.twitter_auth.password).toString('base64'));
+    stream.write("follow=" + twitter_ids.join(","));
     stream.end();
-});
+};