Monday, May 14, 2012

Using the async node.js library to get twitter content in a parallel

Quick test: setup the node.js to pull the contents from twitter in a parallel, and parse the json data back,
  If search, http://search.twitter.com/search.json?q=lakers&rpp=5&include_entities=true&result_type=mixed, will show lakes news. 
  Here is one quick code to search 5 NBA teams, and get the text back.
 


var async=require("async");var http=require("http"); var fs=require("fs");

function getwebPageContents(keyword,callback)
{
    var client=http.get({"host":"search.twitter.com","port":"80",
        "path":"/search.json?q=" + keyword + "&rpp=5&include_entities=true&result_type=mixed"
    }, function(data)
    {   
        var p="";
        data.on("data",function(chunk)
        {
            p+=chunk;
        } );
        data.on("end",function()
        {
            callback(null, p);
        })
    })
}

async.parallel([
    function(callback){
            getwebPageContents("lal",callback)
    },
    function(callback){
            getwebPageContents("lac",callback)
    },
     function(callback){
            getwebPageContents("okc",callback)
    },
    function(callback){
            getwebPageContents("mia",callback)
    }, function(callback){
            getwebPageContents("phi",callback)
    }
], function(e,data)
{
    
    for(p in data)
    {
        console.log( "result- " +p +  "  " +JSON.parse(data[p]).results[0].text);
    }
    
});


you will get some data, like this,
image

No comments:

 
Locations of visitors to this page