Advertise

Saturday, 8 September 2012

Get Your tweets



Install location : after download file,please put it at your server folder.like htdocs(MAMP and WAMP).

What I want to Do ?

this app is simple web app to retrieve recent tweets from someone.
  1. User need to type user ID (default : Obama)
  2. click "get Your tweet" button"
  3. retrieve recent tweet

What I ignore ?

  1. Page function
  2. Auto Update

Code focus

//BarackObama is user id you enter
//callback=updateTweets is function we call to handle json data 
"http://twitter.com/statuses/user_timeline/BarackObama.json?callback=updateTweets";

Data we Retrieve(json) fragment

"in_reply_to_status_id":null,
    "in_reply_to_user_id_str":null,
    "id_str":"244873182916526080",
    "retweet_count":373,
    "favorited":false,
    "truncated":false,
    "source":"\u003Ca href=\"http:\/\/www.barackobama.com\/\" rel=\"nofollow\"\u003EObama for America\u003C\/a\u003E",
    "created_at":"Sun Sep 09 19:01:30 +0000 2012",
    "possibly_sensitive_editable":true,
    "id":244873182916526080,
    "in_reply_to_screen_name":null,
    "text":"Easy: Pick out your favorite bumper sticker and we\u2019ll get it in the mail. http:\/\/t.co\/H4h9qc6e"

Hot to handle data

//tweet. + data you want to
//tweet.id_str  = 244873182916526080
 var data = tweet.text.replace("\"", "'");
 var date = tweet.created_at ;     

Back to Our code

$(function(){
    $("#tweet").toggle();

   function getInput(){
       $("#get").click(function(){
           
        //retreive data from $("input:text")
        var id = $("input:text").val();  
        
        //apply id to src => create complete address for sending request
        var src = "http://twitter.com/statuses/user_timeline/"+id+".json?callback=updateTweets";
        
        // create new script element and apply to <
<body>
        var script =  '<
<script src='+src+'><
</script>';                    
        $(script).appendTo('body');
        
        // append user id to paragraph
        var name = "<
<h2>"+id+"<
</h2>";
        $(name).appendTo("#name");
        $("#tweet").toggle();
        $("#input").toggle();
        
    });
   }      
  getInput();
});// end ready

function updateTweets(tweets) {
    
    var tweetsSelection = $("#tweet");    
    for (var i = 0; i <
< tweets.length; i++) {        
        var tweet = tweets[i];                
        var data = tweet.text.replace("\"", "'");
        var date = tweet.created_at ;    
   var row = "<tr><td>"+date+"</td><td>"+data+"</td></tr>";            
        tweetsSelection.append(row);     
    }
}

0 comments:

Post a Comment