node.js/twitter.js
author Dominqique Hazael-Massieux <dom@w3.org>
Wed, 31 Aug 2011 00:07:26 +0200
changeset 182 659a8ba1c9f5
child 183 ff93e10d4604
permissions -rw-r--r--
experimenting with twitter access
182
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     1
var http = require('http');
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     2
var EventEmitter = require('events').EventEmitter;
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     3
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     4
function loadTwitterListPage(owner, slug, cursor, users, callback) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     5
   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) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     6
       response.setEncoding('utf8');
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     7
       var twitterDataJSON = "", twitterData;
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     8
       response.on('data', function (chunk) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
     9
	   twitterDataJSON = twitterDataJSON + chunk;
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    10
       });
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    11
       response.on('end', function () {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    12
	   console.log(response.statusCode + JSON.stringify(response.headers));
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    13
	   twitterData = JSON.parse(twitterDataJSON);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    14
	   users = users.concat(twitterData.users);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    15
	   if (twitterData.next_cursor) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    16
	       loadTwitterListPage(owner, slug, twitterData.next_cursor, users, callback);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    17
	   } else {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    18
	       callback(users);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    19
	   }
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    20
       });
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    21
   });
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    22
}
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    23
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    24
loadTwitterListPage('t', 'sf', -1, [], function (users) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    25
    var twitterIds = [];
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    26
    console.log(users);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    27
    for (u in users) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    28
	twitterIds.push(users[u].id);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    29
    }
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    30
    var stream = http.request({host: 'stream.twitter.com', path:'/1/statuses/filter.json', 'method': 'POST'}, function (res) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    31
	res.setEncoding('utf8');
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    32
	var incompleteChunk = "";
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    33
	res.on('data', function (chunk) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    34
	    incompleteChunk += chunk;
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    35
	    incompleteChunk = incompleteChunk.trim();
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    36
	    if (incompleteChunk) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    37
		console.log(chunk);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    38
		try {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    39
		    var tweet = JSON.parse(incompleteChunk);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    40
		    console.log(tweet.text);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    41
		    incompleteChunk = "";
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    42
		} catch (err) {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    43
		    console.log(err);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    44
		}
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    45
	    }
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    46
	});
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    47
	res.on('end', function () {
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    48
	    console.log("terminated with error " + res.statusCode);
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    49
	});
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    50
    });
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    51
    stream.setHeader("Content-Type", "application/x-www-form-urlencoded");
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    52
    stream.setHeader("Authorization", 'Basic ' + new Buffer('dontcallmedom' + ':' + '@@@').toString('base64'));
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    53
    stream.write("follow=" + twitterIds.join(","));
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    54
    stream.end();
659a8ba1c9f5 experimenting with twitter access
Dominqique Hazael-Massieux <dom@w3.org>
parents:
diff changeset
    55
});