added getTwitterId function
authorDominqique Hazael-Massieux <dom@w3.org>
Thu, 01 Sep 2011 11:32:41 +0200
changeset 208 d815783a4272
parent 207 268cfb438bce
child 209 96dd338b6730
added getTwitterId function
node.js/twitter.js
--- a/node.js/twitter.js	Thu Sep 01 11:32:14 2011 +0200
+++ b/node.js/twitter.js	Thu Sep 01 11:32:41 2011 +0200
@@ -43,6 +43,32 @@
 	});
 };
 
+exports.getTwitterId = function(screen_name, callback) {
+   var request = http.get(
+       {host: 'api.twitter.com', path:'/1/users/lookup.json?screen_name=' + screen_name},
+       function (response) {
+	   response.setEncoding('utf8');
+	   var twitterDataJSON = "", twitterData;
+	   response.on('data', function (chunk) {
+	       twitterDataJSON = twitterDataJSON + chunk;
+	   });
+	   response.on(
+	       'end',
+	       function () {
+		   if (response.statusCode != 200){
+  		       console.log("Looking twitter user id failed: " + response.statusCode + JSON.stringify(response.headers));		   
+		   } else {
+		       try {
+			   twitterData = JSON.parse(twitterDataJSON);
+			   callback(twitterData[0].id);
+		       } catch (err) {
+			   console.log(err);
+		       }
+		   }
+	       });
+       });
+};
+
 exports.listenToTweets = function(emitter, twitter_ids, twitter_auth)  {
     var stream = https.request(
 	{host: 'stream.twitter.com', path:'/1/statuses/filter.json', 'method': 'POST'},